Commit 0ecdbebdf6d4eb18c45220b40b981e79c8667ed0
1 parent
1216d683
Exists in
master
and in
5 other branches
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,8 +227,11 @@ module Searchkick | ||
227 | # results can have different types | 227 | # results can have different types |
228 | results = {} | 228 | results = {} |
229 | 229 | ||
230 | + grouped_hits = hits.group_by { |hit, _| hit["_index"] } | ||
231 | + | ||
232 | + # determine models | ||
230 | index_models = {} | 233 | index_models = {} |
231 | - hits.group_by { |hit, _| hit["_index"] }.each do |index, index_hits| | 234 | + grouped_hits.each do |index, index_hits| |
232 | klasses = | 235 | klasses = |
233 | if @klass | 236 | if @klass |
234 | [@klass] | 237 | [@klass] |
@@ -238,9 +241,12 @@ module Searchkick | @@ -238,9 +241,12 @@ module Searchkick | ||
238 | end | 241 | end |
239 | raise Searchkick::Error, "Unknown model for index: #{index}" unless klasses.any? | 242 | raise Searchkick::Error, "Unknown model for index: #{index}" unless klasses.any? |
240 | index_models[index] = klasses | 243 | index_models[index] = klasses |
244 | + end | ||
241 | 245 | ||
246 | + # fetch results | ||
247 | + grouped_hits.each do |index, index_hits| | ||
242 | results[index] = {} | 248 | results[index] = {} |
243 | - klasses.each do |klass| | 249 | + index_models[index].each do |klass| |
244 | results[index].merge!(results_query(klass, index_hits).to_a.index_by { |r| r.id.to_s }) | 250 | results[index].merge!(results_query(klass, index_hits).to_a.index_by { |r| r.id.to_s }) |
245 | end | 251 | end |
246 | end | 252 | end |