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'
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 '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 'faker'
......@@ -40,6 +36,8 @@ gem 'faker'
gem 'rdiscount'
gem 'nokogiri'
gem 'tire'
group :test do
gem 'rspec'
gem 'rack-test', :require => "rack/test"
......
......@@ -198,6 +198,8 @@ namespace :db do
User.delete_all
Notification.delete_all
Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
beginning_time = Time.now
......@@ -216,11 +218,6 @@ namespace :db do
generate_comments_for("lab_1")
generate_comments_for("lab_2")
puts "reindexing solr..."
CommentThread.remove_all_from_index!
Sunspot.index!(CommentThread.all.to_a)
end_time = Time.now
puts "Number of comments generated: #{Comment.count}"
......
......@@ -51,17 +51,27 @@ get "#{api_prefix}/search/threads" do
else
page = (params["page"] || 1).to_i
per_page = (params["per_page"] || 20).to_i
search = CommentThread.solr_search do
fulltext(params["text"]) if params["text"]
with(:commentable_id, params["commentable_id"]) if params["commentable_id"]
with(:course_id, params["course_id"]) if params["course_id"]
with(:tags).all_of(params["tags"].split /,/) if params["tags"]
paginate :page => page, :per_page => per_page
order_by(sort_key, sort_order) if sort_key && sort_order
tags = params["tags"].split /,/ if params["tags"]
search = CommentThread.tire.search page: page, per_page: per_page do |search|
if params["text"]
search.query do |query|
query.text(:_all, params["text"])
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
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,
page: page,
}.to_json
......
......@@ -17,27 +17,24 @@ class CommentThread < Content
field :at_position_list, type: Array, default: []
field :last_activity_at, type: Time
include Sunspot::Mongoid
searchable do
text :title, boost: 5.0, stored: true, more_like_this: true
text :body, stored: true, more_like_this: true
text :tags do
tags_array.join " "
end
time :created_at
time :updated_at
time :last_activity_at
integer :comment_count
integer :votes_point do
votes_point
end
string :course_id
string :commentable_id
string :author_id
string :tags, multiple: true do
tags_array
end
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :title, type: :string, analyzer: :snowball, boost: 5.0, stored: true, term_vector: :with_positions_offsets
indexes :body, type: :string, analyzer: :snowball, stored: true, term_vector: :with_positions_offsets
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
indexes :created_at, type: :date, included_in_all: false
indexes :updated_at, type: :date, included_in_all: false
indexes :last_activity_at, type: :date, included_in_all: false
indexes :comment_count, type: :integer, included_in_all: false
indexes :votes_point, type: :integer, as: 'votes_point', included_in_all: false
indexes :course_id, type: :string, index: :not_analyzed, incldued_in_all: false
indexes :commentable_id, type: :string, index: :not_analyzed, incldued_in_all: false
indexes :author_id, type: :string, as: 'author_id', index: :not_analyzed, incldued_in_all: false
end
belongs_to :author, class_name: "User", inverse_of: :comment_threads, index: true#, autosave: true
......@@ -57,7 +54,6 @@ class CommentThread < Content
before_create :set_last_activity_at
before_update :set_last_activity_at
def self.new_dumb_thread(options={})
c = self.new
c.title = options[:title] || "title"
......@@ -102,15 +98,6 @@ 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
def self.tag_name_valid?(tag)
!!(tag =~ RE_TAG)
end
......
......@@ -17,7 +17,7 @@ describe "app" do
thread2.tags = [ai, ml, random2].join ","
thread2.save
Sunspot.commit
sleep 1
get "/api/v1/search/threads", tags: [ai, ml].join(",")
last_response.should be_ok
......
......@@ -34,6 +34,8 @@ def init_without_subscriptions
User.delete_all
Notification.delete_all
Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
commentable = Commentable.new("question_1")
......@@ -105,6 +107,9 @@ def init_with_subscriptions
Notification.delete_all
Subscription.delete_all
Tire.index 'comment_threads' do delete end
CommentThread.create_elasticsearch_index
user1 = create_test_user(1)
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