Commit 899ce33a121f26c56878f0291fb46a024e158567

Authored by Andrew
1 parent dd1d3724

Fixed with_hit and with_highlights when records in search index do not exist in database

Showing 2 changed files with 12 additions and 9 deletions   Show diff stats
CHANGELOG.md
... ... @@ -3,6 +3,7 @@
3 3 - Added `:inline` as alias for `true` for `callbacks` and `mode` options
4 4 - Friendlier error message for bad mapping with partial matches
5 5 - Warn when records in search index do not exist in database
  6 +- Fixed `with_hit` and `with_highlights` when records in search index do not exist in database
6 7 - Fixed error with highlights and match all
7 8  
8 9 ## 3.0.3
... ...
lib/searchkick/results.rb
... ... @@ -16,7 +16,11 @@ module Searchkick
16 16 end
17 17  
18 18 def results
19   - @results ||= begin
  19 + @results ||= with_hit.map(&:first)
  20 + end
  21 +
  22 + def with_hit
  23 + @with_hit ||= begin
20 24 if options[:load]
21 25 # results can have different types
22 26 results = {}
... ... @@ -38,8 +42,8 @@ module Searchkick
38 42 end
39 43 end
40 44 end
41   - result
42   - end.compact
  45 + [result, hit]
  46 + end.select { |v| v[0] }
43 47  
44 48 if results.size != hits.size
45 49 warn "[searchkick] WARNING: Records in search index do not exist in database"
... ... @@ -65,7 +69,7 @@ module Searchkick
65 69 end
66 70  
67 71 result["id"] ||= result["_id"] # needed for legacy reasons
68   - HashWrapper.new(result)
  72 + [HashWrapper.new(result), hit]
69 73 end
70 74 end
71 75 end
... ... @@ -179,10 +183,6 @@ module Searchkick
179 183 end
180 184 end
181 185  
182   - def with_hit
183   - results.zip(hits)
184   - end
185   -
186 186 def highlights(multiple: false)
187 187 hits.map do |hit|
188 188 hit_highlights(hit, multiple: multiple)
... ... @@ -190,7 +190,9 @@ module Searchkick
190 190 end
191 191  
192 192 def with_highlights(multiple: false)
193   - results.zip(highlights(multiple: multiple))
  193 + with_hit do |result, hit|
  194 + [result, hit_highlights(hit, multiple: multiple)]
  195 + end
194 196 end
195 197  
196 198 def misspellings?
... ...