Commit 08730918 by Rocky Duan

Merge branch 'elasticsearch'

parents 5ca7c413 87c93097
...@@ -29,10 +29,6 @@ gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git' ...@@ -29,10 +29,6 @@ gem 'voteable_mongo', :git => 'git@github.com:dementrock/voteable_mongo.git'
gem 'mongoid_taggable', :git => 'git@github.com:dementrock/mongoid_taggable.git' gem 'mongoid_taggable', :git => 'git@github.com:dementrock/mongoid_taggable.git'
gem 'mongoid_magic_counter_cache', :git => 'git@github.com:dementrock/mongoid-magic-counter-cache.git' gem 'mongoid_magic_counter_cache', :git => 'git@github.com:dementrock/mongoid-magic-counter-cache.git'
gem 'sunspot'
gem 'sunspot_solr'
gem 'sunspot_mongoid', :git => 'git@github.com:dementrock/sunspot_mongoid.git'
gem 'kaminari', :require => 'kaminari/sinatra', :git => 'git@github.com:dementrock/kaminari.git' gem 'kaminari', :require => 'kaminari/sinatra', :git => 'git@github.com:dementrock/kaminari.git'
gem 'faker' gem 'faker'
...@@ -40,6 +36,8 @@ gem 'faker' ...@@ -40,6 +36,8 @@ gem 'faker'
gem 'rdiscount' gem 'rdiscount'
gem 'nokogiri' gem 'nokogiri'
gem 'tire'
group :test do group :test do
gem 'rspec' gem 'rspec'
gem 'rack-test', :require => "rack/test" gem 'rack-test', :require => "rack/test"
......
...@@ -198,6 +198,8 @@ namespace :db do ...@@ -198,6 +198,8 @@ namespace :db do
User.delete_all User.delete_all
Notification.delete_all Notification.delete_all
Subscription.delete_all Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
beginning_time = Time.now beginning_time = Time.now
...@@ -216,11 +218,6 @@ namespace :db do ...@@ -216,11 +218,6 @@ namespace :db do
generate_comments_for("lab_1") generate_comments_for("lab_1")
generate_comments_for("lab_2") generate_comments_for("lab_2")
puts "reindexing solr..."
CommentThread.remove_all_from_index!
Sunspot.index!(CommentThread.all.to_a)
end_time = Time.now end_time = Time.now
puts "Number of comments generated: #{Comment.count}" puts "Number of comments generated: #{Comment.count}"
......
...@@ -51,17 +51,27 @@ get "#{api_prefix}/search/threads" do ...@@ -51,17 +51,27 @@ get "#{api_prefix}/search/threads" do
else else
page = (params["page"] || 1).to_i page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i per_page = (params["per_page"] || 20).to_i
search = CommentThread.solr_search do tags = params["tags"].split /,/ if params["tags"]
fulltext(params["text"]) if params["text"]
with(:commentable_id, params["commentable_id"]) if params["commentable_id"] search = CommentThread.tire.search page: page, per_page: per_page do |search|
with(:course_id, params["course_id"]) if params["course_id"] if params["text"]
with(:tags).all_of(params["tags"].split /,/) if params["tags"] search.query do |query|
paginate :page => page, :per_page => per_page query.text(:_all, params["text"])
order_by(sort_key, sort_order) if sort_key && sort_order end
search.highlight({title: { number_of_fragments: 0 } } , {body: { number_of_fragments: 0 } }, options: { tag: "<highlight>" })
end
search.filter :bool, :must => tags.map{ |tag| { :term => { :tags_array => tag } } } if params["tags"]
search.filter(:term, commentable_id: params["commentable_id"]) if params["commentable_id"]
search.filter(:term, course_id: params["course_id"]) if params["course_id"]
search.sort {|sort| sort.by sort_key, sort_order} if sort_key && sort_order #TODO should have search option 'auto sort or sth'
end end
num_pages = [1, (search.total / per_page.to_f).ceil].max
num_pages = search.total_pages
{ {
collection: search.results.map{|t| t.to_hash(recursive: bool_recursive)}, collection: search.results.map{|t| CommentThread.search_result_to_hash(t, recursive: bool_recursive)},
num_pages: num_pages, num_pages: num_pages,
page: page, page: page,
}.to_json }.to_json
......
...@@ -17,27 +17,24 @@ class CommentThread < Content ...@@ -17,27 +17,24 @@ class CommentThread < Content
field :at_position_list, type: Array, default: [] field :at_position_list, type: Array, default: []
field :last_activity_at, type: Time field :last_activity_at, type: Time
include Sunspot::Mongoid include Tire::Model::Search
searchable do include Tire::Model::Callbacks
text :title, boost: 5.0, stored: true, more_like_this: true
text :body, stored: true, more_like_this: true mapping do
text :tags do indexes :title, type: :string, analyzer: :snowball, boost: 5.0, stored: true, term_vector: :with_positions_offsets
tags_array.join " " indexes :body, type: :string, analyzer: :snowball, stored: true, term_vector: :with_positions_offsets
end indexes :tags_in_text, type: :string, as: 'tags_array', index: :analyzed
indexes :tags_array, type: :string, as: 'tags_array', index: :not_analyzed, included_in_all: false
time :created_at indexes :created_at, type: :date, included_in_all: false
time :updated_at indexes :updated_at, type: :date, included_in_all: false
time :last_activity_at indexes :last_activity_at, type: :date, included_in_all: false
integer :comment_count
integer :votes_point do indexes :comment_count, type: :integer, included_in_all: false
votes_point indexes :votes_point, type: :integer, as: 'votes_point', included_in_all: false
end
string :course_id indexes :course_id, type: :string, index: :not_analyzed, incldued_in_all: false
string :commentable_id indexes :commentable_id, type: :string, index: :not_analyzed, incldued_in_all: false
string :author_id indexes :author_id, type: :string, as: 'author_id', index: :not_analyzed, incldued_in_all: false
string :tags, multiple: true do
tags_array
end
end end
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true#, autosave: true belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true#, autosave: true
...@@ -57,7 +54,6 @@ class CommentThread < Content ...@@ -57,7 +54,6 @@ class CommentThread < Content
before_create :set_last_activity_at before_create :set_last_activity_at
before_update :set_last_activity_at before_update :set_last_activity_at
def self.new_dumb_thread(options={}) def self.new_dumb_thread(options={})
c = self.new c = self.new
c.title = options[:title] || "title" c.title = options[:title] || "title"
...@@ -102,15 +98,6 @@ class CommentThread < Content ...@@ -102,15 +98,6 @@ class CommentThread < Content
doc doc
end 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
def self.tag_name_valid?(tag) def self.tag_name_valid?(tag)
!!(tag =~ RE_TAG) !!(tag =~ RE_TAG)
end end
......
...@@ -17,7 +17,7 @@ describe "app" do ...@@ -17,7 +17,7 @@ describe "app" do
thread2.tags = [ai, ml, random2].join "," thread2.tags = [ai, ml, random2].join ","
thread2.save thread2.save
Sunspot.commit sleep 1
get "/api/v1/search/threads", tags: [ai, ml].join(",") get "/api/v1/search/threads", tags: [ai, ml].join(",")
last_response.should be_ok last_response.should be_ok
......
...@@ -34,6 +34,8 @@ def init_without_subscriptions ...@@ -34,6 +34,8 @@ def init_without_subscriptions
User.delete_all User.delete_all
Notification.delete_all Notification.delete_all
Subscription.delete_all Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
commentable = Commentable.new("question_1") commentable = Commentable.new("question_1")
...@@ -105,6 +107,9 @@ def init_with_subscriptions ...@@ -105,6 +107,9 @@ def init_with_subscriptions
Notification.delete_all Notification.delete_all
Subscription.delete_all Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
user1 = create_test_user(1) user1 = create_test_user(1)
user2 = create_test_user(2) user2 = create_test_user(2)
......
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