Commit fe1cf63b9a27482be2214d5e1d8b0b38adea54ae

Authored by Andrew Kane
1 parent 9a69a9fe

Use missing_hits instead of missing_ids

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