class ElasticQueue::Results
Attributes
instantiated_queue_items[R]
Public Class Methods
new(queue, search_results, query_options)
click to toggle source
# File lib/elastic_queue/results.rb, line 8 def initialize(queue, search_results, query_options) @queue = queue @instantiated_queue_items = instantiate_queue_items(search_results) @start = query_options.page @per_page = query_options.per_page @total = search_results[:hits][:total] end
Public Instance Methods
paginate()
click to toggle source
# File lib/elastic_queue/results.rb, line 16 def paginate WillPaginate::Collection.create(@start, @per_page, @total) do |pager| pager.replace(@instantiated_queue_items) end end
Private Instance Methods
fetch_records(grouped_results)
click to toggle source
take a hash of { model_name: [ids] } and return a list of records
# File lib/elastic_queue/results.rb, line 45 def fetch_records(grouped_results) records = [] grouped_results.each do |model, ids| klass = model.to_s.camelize.constantize if @queue.eager_loads && @queue.eager_loads[model] records += klass.includes(@queue.eager_loads[model]).find_all_by_id(ids) else records += klass.find_all_by_id(ids) end end records end
group_sorted_results(search_results)
click to toggle source
group the results by { class_name: [ids] } and save their sorted order
# File lib/elastic_queue/results.rb, line 31 def group_sorted_results(search_results) grouped_results = {} sort_order = {} search_results[:hits][:hits].each_with_index do |result, index| model = result[:_source][:model].to_sym model_id = result[:_source][:id] sort_order["#{model}_#{model_id}"] = index # save the sort order grouped_results[model] ||= [] grouped_results[model] << model_id end [grouped_results, sort_order] end
instantiate_queue_items(search_results)
click to toggle source
# File lib/elastic_queue/results.rb, line 24 def instantiate_queue_items(search_results) grouped_results, sort_order = group_sorted_results(search_results) records = fetch_records(grouped_results) sort_records(records, sort_order) end
sort_records(records, sort_order)
click to toggle source
# File lib/elastic_queue/results.rb, line 58 def sort_records(records, sort_order) records.sort do |a, b| sort_order["#{a.class.name.underscore}_#{a.id}"] <=> sort_order["#{b.class.name.underscore}_#{b.id}"] end end