Commit fe668554b4abcdea4e098ef0e486ef7beeb0ab7f
1 parent
5916f32d
Exists in
master
and in
21 other branches
Fixed multi-index searches - closes #192
Showing
3 changed files
with
23 additions
and
7 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/results.rb
... | ... | @@ -16,14 +16,21 @@ module Searchkick |
16 | 16 | def results |
17 | 17 | @results ||= begin |
18 | 18 | if options[:load] |
19 | - hit_ids = hits.map{|hit| hit["_id"] } | |
20 | - records = klass | |
21 | - if options[:includes] | |
22 | - records = records.includes(options[:includes]) | |
19 | + # results can have different types | |
20 | + results = {} | |
21 | + | |
22 | + hits.group_by{|hit, i| hit["_type"] }.each do |type, grouped_hits| | |
23 | + records = type.camelize.constantize | |
24 | + if options[:includes] | |
25 | + records = records.includes(options[:includes]) | |
26 | + end | |
27 | + results[type] = records.find(grouped_hits.map{|hit| hit["_id"] }) | |
28 | + end | |
29 | + | |
30 | + # sort | |
31 | + hits.map do |hit| | |
32 | + results[hit["_type"]].find{|r| r.id.to_s == hit["_id"].to_s } | |
23 | 33 | end |
24 | - records = records.find(hit_ids) | |
25 | - hit_ids = hit_ids.map(&:to_s) | |
26 | - records.sort_by{|r| hit_ids.index(r.id.to_s) } | |
27 | 34 | else |
28 | 35 | hits.map do |hit| |
29 | 36 | result = hit.except("_source").merge(hit["_source"]) | ... | ... |
test/inheritance_test.rb
... | ... | @@ -62,4 +62,12 @@ class TestInheritance < Minitest::Unit::TestCase |
62 | 62 | assert_equal ["tiger"], Animal.search("tige", fields: [:name], suggest: true).suggestions.sort |
63 | 63 | end |
64 | 64 | |
65 | + # TODO move somewhere better | |
66 | + | |
67 | + def test_multiple_indices | |
68 | + store_names ["Product A"] | |
69 | + store_names ["Product B"], Animal | |
70 | + assert_search "product", ["Product A", "Product B"], index_name: [Product.searchkick_index.name, Animal.searchkick_index.name], conversions: false | |
71 | + end | |
72 | + | |
65 | 73 | end | ... | ... |