Commit fe668554b4abcdea4e098ef0e486ef7beeb0ab7f

Authored by Andrew Kane
1 parent 5916f32d

Fixed multi-index searches - closes #192

CHANGELOG.md
1 1 ## 0.7.3 [unreleased]
2 2  
  3 +- Fixed multi-index searches
3 4 - Fixed suggestions for partial matches
4 5 - Added `offset` and `length` for improved pagination
5 6  
... ...
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
... ...