Commit 818fa573 by Rocky Duan

caching & benchmark for search query

parent da977fb7
......@@ -39,6 +39,8 @@ gem 'nokogiri'
gem 'tire'
gem 'tire-contrib'
gem 'dalli'
gem 'rest-client'
group :test do
......
......@@ -30,16 +30,16 @@ get "#{APIPREFIX}/search/threads" do
per_page: per_page,
}
results = CommentThread.perform_search(params, options).results
results = CommentThread.perform_search(params, options)
if page > results.total_pages #TODO find a better way for this
results = CommentThread.perform_search(params, options.merge(page: results.total_pages)).results
if page > results[:total_pages] #TODO find a better way for this
results = CommentThread.perform_search(params, options.merge(page: results[:total_pages]))
end
num_pages = results.total_pages
num_pages = results[:total_pages]
page = [num_pages, [1, page].max].min
{
collection: results.map{|t| CommentThread.search_result_to_hash(t, recursive: bool_recursive)},
collection: results[:result_ids].map{|id| CommentThread.search_result_id_to_hash(id, recursive: bool_recursive)},
num_pages: num_pages,
page: page,
}.to_json
......
......@@ -9,15 +9,17 @@ require 'tire/queries/more_like_this'
env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index
environment = env_arg || ENV["SINATRA_ENV"] || "development"
RACK_ENV = environment
RACK_ENV = environment
module CommentService
class << self; attr_accessor :config; end
API_VERSION = 'v1'
API_PREFIX = "/api/#{API_VERSION}"
end
CommentService.config = YAML.load_file("config/application.yml")
set :cache, Dalli::Client.new
CommentService.config = YAML.load_file("config/application.yml").with_indifferent_access
Mongoid.load!("config/mongoid.yml", environment)
Mongoid.logger.level = Logger::INFO
......@@ -44,7 +46,7 @@ require './api/users'
require './api/votes'
require './api/notifications_and_subscriptions'
if environment.to_s == "development"
if RACK_ENV.to_s == "development"
get "#{APIPREFIX}/clean" do
[Delayed::Backend::Mongoid::Job, Comment, CommentThread, User, Notification, Subscription, Activity].each(&:delete_all).each(&:remove_indexes).each(&:create_indexes)
{}.to_json
......
level_limit: 3
cache_enabled: true
cache_timeout:
threads_search: 60
......@@ -88,7 +88,7 @@ namespace :benchmark do
x.report("searching threads in a course") do
COURSE_THREAD_QUERY.times do
query_params = { course_id: "1", text: "token#{rand(10)} token#{rand(10)} token#{rand(10)}", sort_key: sort_keys.sample, sort_order: sort_order, page: 1 + rand(10), per_page: 5 }
query_params = { course_id: "1", text: "token#{rand(10)} token#{rand(10)}", sort_key: sort_keys.sample, sort_order: sort_order, page: 1 + rand(10), per_page: 5 }
RestClient.get "#{PREFIX}/search/threads", params: query_params
end
end
......
......@@ -75,14 +75,14 @@ class CommentThread < Content
c
end
def self.search_result_to_hash(result, params={})
def self.search_result_id_to_hash(id, params={})
comment_thread = self.find(result.id)
highlight = result.highlight || {}
highlighted_body = (highlight[:body] || []).first || comment_thread.body
highlighted_title = (highlight[:title] || []).first || comment_thread.title
find(result.id).to_hash(params).merge(highlighted_body: highlighted_body, highlighted_title: highlighted_title)
find(id).to_hash(params).merge(highlighted_body: highlighted_body, highlighted_title: highlighted_title)
end
def self.perform_search(params, options={})
......@@ -90,6 +90,12 @@ class CommentThread < Content
per_page = options[:per_page] || 20
sort_key = options[:sort_key]
sort_order = options[:sort_order]
if CommentService.config[:cache_enabled]
memcached_key = "threads_search_#{params.merge(options).hash}"
results = Sinatra::Application.cache.get(memcached_key)
return results if results
puts "cache miss"
end
search = Tire::Search::Search.new 'comment_threads'
search.query {|query| query.text :_all, params["text"]} if params["text"]
search.highlight({title: { number_of_fragments: 0 } } , {body: { number_of_fragments: 0 } }, options: { tag: "<highlight>" })
......@@ -100,7 +106,14 @@ class CommentThread < Content
search.size per_page
search.from per_page * (page - 1)
search
results = {
result_ids: search.results.map(&:id),
total_pages: search.results.total_pages,
}
if CommentService.config[:cache_enabled]
Sinatra::Application.cache.set(memcached_key, results, CommentService.config[:cache_timeout][:threads_search].to_i)
end
results
end
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