Commit 0ecdbebdf6d4eb18c45220b40b981e79c8667ed0

Authored by Andrew Kane
1 parent 1216d683

Improved code

Showing 1 changed file with 8 additions and 2 deletions   Show diff stats
lib/searchkick/results.rb
... ... @@ -227,8 +227,11 @@ module Searchkick
227 227 # results can have different types
228 228 results = {}
229 229  
  230 + grouped_hits = hits.group_by { |hit, _| hit["_index"] }
  231 +
  232 + # determine models
230 233 index_models = {}
231   - hits.group_by { |hit, _| hit["_index"] }.each do |index, index_hits|
  234 + grouped_hits.each do |index, index_hits|
232 235 klasses =
233 236 if @klass
234 237 [@klass]
... ... @@ -238,9 +241,12 @@ module Searchkick
238 241 end
239 242 raise Searchkick::Error, "Unknown model for index: #{index}" unless klasses.any?
240 243 index_models[index] = klasses
  244 + end
241 245  
  246 + # fetch results
  247 + grouped_hits.each do |index, index_hits|
242 248 results[index] = {}
243   - klasses.each do |klass|
  249 + index_models[index].each do |klass|
244 250 results[index].merge!(results_query(klass, index_hits).to_a.index_by { |r| r.id.to_s })
245 251 end
246 252 end
... ...