Commit fe1cf63b9a27482be2214d5e1d8b0b38adea54ae

Authored by Andrew Kane
1 parent 9a69a9fe

Use missing_hits instead of missing_ids

CHANGELOG.md
1 1 ## 4.4.2 (unreleased)
2 2  
3   -- Added `missing_ids` method to results
  3 +- Added `missing_hits` method to results
4 4 - Fixed issue with `like` and special characters
5 5  
6 6 ## 4.4.1 (2020-06-24)
... ...
lib/searchkick/results.rb
... ... @@ -22,15 +22,15 @@ module Searchkick
22 22 # TODO return enumerator like with_score
23 23 def with_hit
24 24 @with_hit ||= begin
25   - if missing_ids.any?
26   - Searchkick.warn("Records in search index do not exist in database: #{missing_ids.join(", ")}")
  25 + if missing_hits.any?
  26 + Searchkick.warn("Records in search index do not exist in database: #{missing_hits.map { |v| v["_id"] }.join(", ")}")
27 27 end
28   - with_hit_and_missing_ids[0]
  28 + with_hit_and_missing_hits[0]
29 29 end
30 30 end
31 31  
32   - def missing_ids
33   - @missing_ids ||= with_hit_and_missing_ids[1]
  32 + def missing_hits
  33 + @missing_hits ||= with_hit_and_missing_hits[1]
34 34 end
35 35  
36 36 def suggestions
... ... @@ -219,9 +219,9 @@ module Searchkick
219 219  
220 220 private
221 221  
222   - def with_hit_and_missing_ids
223   - @with_hit_and_missing_ids ||= begin
224   - missing_ids = []
  222 + def with_hit_and_missing_hits
  223 + @with_hit_and_missing_hits ||= begin
  224 + missing_hits = []
225 225  
226 226 if options[:load]
227 227 # results can have different types
... ... @@ -257,7 +257,7 @@ module Searchkick
257 257 end
258 258 [result, hit]
259 259 end.select do |result, hit|
260   - missing_ids << hit["_id"] unless result
  260 + missing_hits << hit unless result
261 261 result
262 262 end
263 263 else
... ... @@ -284,7 +284,7 @@ module Searchkick
284 284 end
285 285 end
286 286  
287   - [results, missing_ids]
  287 + [results, missing_hits]
288 288 end
289 289 end
290 290  
... ...
test/search_test.rb
... ... @@ -37,16 +37,16 @@ class SearchTest &lt; Minitest::Test
37 37 assert_equal ["Dollar Tree"], products.map(&:name)
38 38 end
39 39  
40   - def test_missing_ids
  40 + def test_missing_hits
41 41 store_names ["Product A", "Product B"]
42 42 product = Product.find_by(name: "Product A")
43 43 product.delete
44 44 assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do
45 45 result = Product.search("product")
46 46 assert_equal ["Product B"], result.map(&:name)
47   - assert_equal [product.id.to_s], result.missing_ids
  47 + assert_equal [product.id.to_s], result.missing_hits.map { |v| v["_id"] }
48 48 end
49   - assert_equal [], Product.search("product", load: false).missing_ids
  49 + assert_empty Product.search("product", load: false).missing_hits
50 50 ensure
51 51 Product.reindex
52 52 end
... ...