Commit c8e929cf by Rocky Duan

api for search

parent fae470fc
......@@ -22,3 +22,4 @@ Gemfile.lock
.*.swp
.*.swo
.*.swm
*.pid
source :rubygems
gem 'pry'
gem 'pry-nav'
gem 'debugger'
gem 'bundler'
......@@ -27,6 +29,11 @@ gem 'delayed_job_mongoid'
gem 'mongoid-tree', :git => 'git@github.com:dementrock/mongoid-tree.git', :branch => "mongoid-2.0"
gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git'
gem 'sunspot'
gem 'sunspot_solr'
gem 'sunspot_mongoid'
group :test do
gem 'rspec'
gem 'rack-test', :require => "rack/test"
......
......@@ -16,7 +16,9 @@ task :environment do
end
CommentService.config = YAML.load_file("config/application.yml")
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
Dir[File.join(File.dirname(__FILE__),'models', '**', '*.rb')].each {|file| require file}
end
namespace :test do
......@@ -105,7 +107,7 @@ namespace :db do
{title: "We can probably make this better", body: "Let's do it"},
{title: "I don't know where to start", body: "Can anyone help me?"},
{title: "I'm here!", body: "Haha I'm the first one who discovered this"},
{title: "I need five threads but I don't know where to put here", body: "So I'll just leave it this way"},
{title: "I need five threads but I don't know what to put here", body: "So I'll just leave it this way"},
]
COMMENT_BODY_SEEDS = [
......@@ -176,3 +178,41 @@ namespace :db do
end
end
# copied from https://github.com/sunspot/sunspot/blob/master/sunspot_solr/lib/sunspot/solr/tasks.rb
namespace :sunspot do
namespace :solr do
desc 'Start the Solr instance'
task :start => :environment do
case RUBY_PLATFORM
when /w(in)?32$/, /java$/
abort("This command is not supported on #{RUBY_PLATFORM}. " +
"Use rake sunspot:solr:run to run Solr in the foreground.")
end
Sunspot::Solr::Server.new.start
puts "Successfully started Solr ..."
end
desc 'Run the Solr instance in the foreground'
task :run => :environment do
Sunspot::Solr::Server.new.run
end
desc 'Stop the Solr instance'
task :stop => :environment do
case RUBY_PLATFORM
when /w(in)?32$/, /java$/
abort("This command is not supported on #{RUBY_PLATFORM}. " +
"Use rake sunspot:solr:run to run Solr in the foreground.")
end
Sunspot::Solr::Server.new.stop
puts "Successfully stopped Solr ..."
end
end
end
......@@ -18,6 +18,7 @@ Mongoid.load!("config/mongoid.yml")
Mongoid.logger.level = Logger::INFO
Dir[File.dirname(__FILE__) + '/models/*.rb'].each {|file| require file}
Dir[File.dirname(__FILE__) + '/lib/**/*.rb'].each {|file| require file}
delete '/api/v1/:commentable_id/threads' do |commentable_id|
Commentable.find(commentable_id).comment_threads.destroy_all
......@@ -136,6 +137,24 @@ delete '/api/v1/users/:user_id/subscriptions' do |user_id|
user.unsubscribe(source).to_hash.to_json
end
# GET /api/v1/search
# params:
# text: text to search for
# commentable_id: search within a commentable
#
get '/api/v1/search' do
if params["text"]
CommentThread.solr_search do
fulltext(params["text"])
if params["commentable_id"]
with(:commentable_id, params["commentable_id"])
end
end.results.map(&:to_hash).to_json
else
{}.to_json
end
end
if env.to_s == "development"
get '/api/v1/clean' do
Comment.delete_all
......
......@@ -11,6 +11,16 @@ class CommentThread < Content
field :course_id, type: String, index: true
field :commentable_id, type: String, index: true
include Sunspot::Mongoid
searchable do
text :title, boost: 5.0, stored: true, more_like_this: true
text :body, stored: true, more_like_this: true
string :course_id
string :commentable_id
string :author_id
end
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true, autosave: true
has_many :comments, dependent: :destroy, autosave: true# Use destroy to envoke callback on the top-level comments TODO async
......@@ -47,6 +57,15 @@ class CommentThread < Content
doc
end
def self.search_text(text, commentable_id=nil)
self.solr_search do
fulltext(text)
if commentable_id
with(:commentable_id, commentable_id)
end
end
end
private
def generate_notifications
if subscribers or (author.followers if author)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment