Commit c44af90b by Rocky Duan

store the entire result

parent 0f44adf2
...@@ -32,14 +32,14 @@ get "#{APIPREFIX}/search/threads" do ...@@ -32,14 +32,14 @@ get "#{APIPREFIX}/search/threads" do
results = CommentThread.perform_search(params, options) results = CommentThread.perform_search(params, options)
if page > results[:total_pages] #TODO find a better way for this if page > results.total_pages #TODO find a better way for this
results = CommentThread.perform_search(params, options.merge(page: results[:total_pages])) results = CommentThread.perform_search(params, options.merge(page: results.total_pages))
end end
num_pages = results[:total_pages] num_pages = results.total_pages
page = [num_pages, [1, page].max].min page = [num_pages, [1, page].max].min
{ {
collection: results[:result_ids].map{|id| CommentThread.search_result_id_to_hash(id, recursive: bool_recursive)}, collection: 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
......
...@@ -75,14 +75,14 @@ class CommentThread < Content ...@@ -75,14 +75,14 @@ class CommentThread < Content
c c
end end
def self.search_result_id_to_hash(id, params={}) def self.search_result_to_hash(result, params={})
comment_thread = self.find(result.id) comment_thread = self.find(result.id)
highlight = result.highlight || {} highlight = result.highlight || {}
highlighted_body = (highlight[:body] || []).first || comment_thread.body highlighted_body = (highlight[:body] || []).first || comment_thread.body
highlighted_title = (highlight[:title] || []).first || comment_thread.title highlighted_title = (highlight[:title] || []).first || comment_thread.title
find(id).to_hash(params).merge(highlighted_body: highlighted_body, highlighted_title: highlighted_title) comment_thread.to_hash(params).merge(highlighted_body: highlighted_body, highlighted_title: highlighted_title)
end end
def self.perform_search(params, options={}) def self.perform_search(params, options={})
...@@ -109,14 +109,10 @@ class CommentThread < Content ...@@ -109,14 +109,10 @@ class CommentThread < Content
search.size per_page search.size per_page
search.from per_page * (page - 1) search.from per_page * (page - 1)
results = {
result_ids: search.results.map(&:id),
total_pages: search.results.total_pages,
}
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, search.results, CommentService.config[:cache_timeout][:threads_search].to_i)
end end
results search.results
end end
def activity_since(from_time=nil) def activity_since(from_time=nil)
......
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