Commit 02df47fb by jimabramson

more improvements for thread list queries

* use Content model (to avoid querying mongo with "$in")
* add indexes for non-default sorts
* streamline api code
parent 95643ded
get "#{APIPREFIX}/threads" do # retrieve threads by course get "#{APIPREFIX}/threads" do # retrieve threads by course
#if a group id is sent, then process the set of threads with that group id or with no group id #if a group id is sent, then process the set of threads with that group id or with no group id
threads = Content.where(_type:"CommentThread", course_id: params["course_id"])
if params["group_id"] if params["group_id"]
threads = CommentThread.any_of( threads = threads.any_of(
{:course_id => params["course_id"],:group_id => params[:group_id]}, {:group_id => params[:group_id].to_i},
{:course_id => params["course_id"],:group_id.exists => false}, {:group_id.exists => false},
) )
else
threads = CommentThread.where(course_id: params["course_id"])
#else process them all
end end
handle_threads_query(threads) handle_threads_query(threads)
end end
......
...@@ -4,13 +4,12 @@ delete "#{APIPREFIX}/:commentable_id/threads" do |commentable_id| ...@@ -4,13 +4,12 @@ delete "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
end end
get "#{APIPREFIX}/:commentable_id/threads" do |commentable_id| get "#{APIPREFIX}/:commentable_id/threads" do |commentable_id|
threads = Content.where(_type:"CommentThread", commentable_id: commentable_id)
if params["group_id"] if params["group_id"]
threads = CommentThread.any_of( threads = threads.any_of(
{:commentable_id => commentable_id, :group_id => params[:group_id]}, {:group_id => params[:group_id].to_i},
{:commentable_id => commentable_id, :group_id.exists => false}, {:group_id.exists => false},
) )
else
threads = commentable.comment_threads
end end
handle_threads_query(threads) handle_threads_query(threads)
end end
......
...@@ -24,7 +24,6 @@ class CommentThread < Content ...@@ -24,7 +24,6 @@ class CommentThread < Content
field :pinned, type: Boolean field :pinned, type: Boolean
index({author_id: 1, course_id: 1}) index({author_id: 1, course_id: 1})
index({_type: 1, course_id: 1, pinned: -1, created_at: -1})
include Tire::Model::Search include Tire::Model::Search
include Tire::Model::Callbacks include Tire::Model::Callbacks
......
...@@ -8,6 +8,10 @@ class Content ...@@ -8,6 +8,10 @@ class Content
field :historical_abuse_flaggers, type: Array, default: [] #preserve abuse flaggers after a moderator unflags field :historical_abuse_flaggers, type: Array, default: [] #preserve abuse flaggers after a moderator unflags
field :author_username, type: String, default: nil field :author_username, type: String, default: nil
index({_type: 1, course_id: 1, pinned: -1, created_at: -1 }, {background: true} )
index({_type: 1, course_id: 1, pinned: -1, comment_count: -1, created_at: -1}, {background: true})
index({_type: 1, course_id: 1, pinned: -1, "votes.point" => -1, created_at: -1}, {background: true})
index({comment_thread_id: 1, sk: 1}, {sparse: true}) index({comment_thread_id: 1, sk: 1}, {sparse: true})
index({comment_thread_id: 1, endorsed: 1}, {sparse: true}) index({comment_thread_id: 1, endorsed: 1}, {sparse: true})
......
db.contents.ensureIndex({_type: 1, course_id: 1, pinned: -1, comment_count: -1, created_at: -1}, {background: true})
db.contents.ensureIndex({_type: 1, course_id: 1, pinned: -1, "votes.point": -1, created_at: -1}, {background: true})
db.contents.dropIndex({_type: 1, course_id: 1, pinned: -1, comment_count: -1, created_at: -1})
db.contents.dropIndex({_type: 1, course_id: 1, pinned: -1, "votes.point": -1, created_at: -1})
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