Commit 6ddd730b by Your Name

merge comment and comment thread queries

parent 73c6d89e
...@@ -227,6 +227,11 @@ namespace :db do ...@@ -227,6 +227,11 @@ namespace :db do
Tire.index('comment_threads').delete Tire.index('comment_threads').delete
CommentThread.create_elasticsearch_index CommentThread.create_elasticsearch_index
Tire.index('comment_threads') { import CommentThread.all } Tire.index('comment_threads') { import CommentThread.all }
Tire.index('comments').delete
Comment.create_elasticsearch_index
Tire.index('comments') { import Comment.all }
end end
task :add_anonymous_to_peers => :environment do task :add_anonymous_to_peers => :environment do
......
...@@ -17,6 +17,15 @@ class Comment < Content ...@@ -17,6 +17,15 @@ class Comment < Content
field :at_position_list, type: Array, default: [] field :at_position_list, type: Array, default: []
index({author_id: 1, course_id: 1}) index({author_id: 1, course_id: 1})
include Tire::Model::Search
include Tire::Model::Callbacks
mapping do
indexes :body, type: :string, analyzer: :snowball, stored: true, term_vector: :with_positions_offsets
end
belongs_to :comment_thread, index: true belongs_to :comment_thread, index: true
belongs_to :author, class_name: "User", index: true belongs_to :author, class_name: "User", index: true
......
...@@ -44,6 +44,8 @@ class CommentThread < Content ...@@ -44,6 +44,8 @@ class CommentThread < Content
indexes :commentable_id, type: :string, index: :not_analyzed, included_in_all: false indexes :commentable_id, type: :string, index: :not_analyzed, included_in_all: false
indexes :author_id, type: :string, as: 'author_id', index: :not_analyzed, included_in_all: false indexes :author_id, type: :string, as: 'author_id', index: :not_analyzed, included_in_all: false
indexes :group_id, type: :integer, as: 'group_id', index: :not_analyzed, included_in_all: false indexes :group_id, type: :integer, as: 'group_id', index: :not_analyzed, included_in_all: false
indexes :id, :index => :not_analyzed
indexes :thread_id, :analyzer => :keyword, :as => "_id"
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
...@@ -109,23 +111,62 @@ class CommentThread < Content ...@@ -109,23 +111,62 @@ class CommentThread < Content
search.filter(:term, commentable_id: params["commentable_id"]) if params["commentable_id"] search.filter(:term, commentable_id: params["commentable_id"]) if params["commentable_id"]
search.filter(:terms, commentable_id: params["commentable_ids"]) if params["commentable_ids"] search.filter(:terms, commentable_id: params["commentable_ids"]) if params["commentable_ids"]
search.filter(:term, course_id: params["course_id"]) if params["course_id"] search.filter(:term, course_id: params["course_id"]) if params["course_id"]
if params["group_id"] if params["group_id"]
search.filter :or, [ search.filter :or, [
{:not => {:exists => {:field => :group_id}}}, {:not => {:exists => {:field => :group_id}}},
{:term => {:group_id => params["group_id"]}} {:term => {:group_id => params["group_id"]}}
] ]
end end
search.sort {|sort| sort.by sort_key, sort_order} if sort_key && sort_order #TODO should have search option 'auto sort or sth' search.sort {|sort| sort.by sort_key, sort_order} if sort_key && sort_order #TODO should have search option 'auto sort or sth'
search.size per_page search.size per_page
search.from per_page * (page - 1) search.from per_page * (page - 1)
results = search.results results = search.results
#if this is a search query, then also search the comments and harvest the matching comments
if params["text"]
#temp
params = {}
params["text"] = "test"
#end temp
search = Tire::Search::Search.new 'comments'
search.query {|query| query.text :_all, params["text"]} if params["text"]
search.size per_page
search.from per_page * (page - 1)
c_results = search.results
comment_ids = c_results.collect{|c| c.id}.uniq
comments = Comment.where(:id.in => comment_ids)
thread_ids = comments.collect{|c| c.comment_thread_id}
#now run one more search to harvest the threads and filter by group
search = Tire::Search::Search.new 'comment_threads'
search.filter(:term, :thread_id => thread_ids)
if params["group_id"]
search.filter :or, [
{:not => {:exists => {:field => :group_id}}},
{:term => {:group_id => params["group_id"]}}
]
end
results += search.results
end
if CommentService.config[:cache_enabled] if CommentService.config[:cache_enabled]
Sinatra::Application.cache.set(memcached_key, results, CommentService.config[:cache_timeout][:threads_search].to_i) Sinatra::Application.cache.set(memcached_key, results, CommentService.config[:cache_timeout][:threads_search].to_i)
end end
...@@ -170,13 +211,13 @@ class CommentThread < Content ...@@ -170,13 +211,13 @@ class CommentThread < Content
def to_hash(params={}) def to_hash(params={})
doc = as_document.slice(*%w[title body course_id anonymous anonymous_to_peers commentable_id created_at updated_at at_position_list closed]) doc = as_document.slice(*%w[title body course_id anonymous anonymous_to_peers commentable_id created_at updated_at at_position_list closed])
.merge("id" => _id, "user_id" => author.id, .merge("id" => _id, "user_id" => author.id,
"username" => author.username, "username" => author.username,
"votes" => votes.slice(*%w[count up_count down_count point]), "votes" => votes.slice(*%w[count up_count down_count point]),
"tags" => tags_array, "tags" => tags_array,
"type" => "thread", "type" => "thread",
"group_id" => group_id, "group_id" => group_id,
"endorsed" => endorsed?) "endorsed" => endorsed?)
if params[:recursive] if params[:recursive]
doc = doc.merge("children" => root_comments.map{|c| c.to_hash(recursive: true)}) doc = doc.merge("children" => root_comments.map{|c| c.to_hash(recursive: true)})
...@@ -198,8 +239,8 @@ class CommentThread < Content ...@@ -198,8 +239,8 @@ class CommentThread < Content
# unread count # unread count
if last_read_time if last_read_time
unread_count = self.comments.where( unread_count = self.comments.where(
:updated_at => {:$gte => last_read_time}, :updated_at => {:$gte => last_read_time},
:author_id => {:$ne => params[:user_id]}, :author_id => {:$ne => params[:user_id]},
).count ).count
read = last_read_time >= self.updated_at read = last_read_time >= self.updated_at
else else
...@@ -213,8 +254,8 @@ class CommentThread < Content ...@@ -213,8 +254,8 @@ class CommentThread < Content
end end
doc = doc.merge("unread_comments_count" => unread_count) doc = doc.merge("unread_comments_count" => unread_count)
.merge("read" => read) .merge("read" => read)
.merge("comments_count" => comments_count) .merge("comments_count" => comments_count)
doc doc
...@@ -224,7 +265,7 @@ class CommentThread < Content ...@@ -224,7 +265,7 @@ class CommentThread < Content
!!(tag =~ RE_TAG) !!(tag =~ RE_TAG)
end end
private private
RE_HEADCHAR = /[a-z0-9]/ RE_HEADCHAR = /[a-z0-9]/
RE_ENDONLYCHAR = /\+/ RE_ENDONLYCHAR = /\+/
......
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