Commit 89502e6ba5fdea27fc5d0b7597ba150acb65501f
1 parent
5de399d4
Exists in
master
and in
5 other branches
Added missing_ids method to results - #1452
Showing
3 changed files
with
18 additions
and
8 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/results.rb
... | ... | @@ -22,6 +22,8 @@ module Searchkick |
22 | 22 | # TODO return enumerator like with_score |
23 | 23 | def with_hit |
24 | 24 | @with_hit ||= begin |
25 | + @missing_ids = [] | |
26 | + | |
25 | 27 | if options[:load] |
26 | 28 | # results can have different types |
27 | 29 | results = {} |
... | ... | @@ -42,8 +44,6 @@ module Searchkick |
42 | 44 | end |
43 | 45 | end |
44 | 46 | |
45 | - missing_ids = [] | |
46 | - | |
47 | 47 | # sort |
48 | 48 | results = |
49 | 49 | hits.map do |hit| |
... | ... | @@ -58,12 +58,12 @@ module Searchkick |
58 | 58 | end |
59 | 59 | [result, hit] |
60 | 60 | end.select do |result, hit| |
61 | - missing_ids << hit["_id"] unless result | |
61 | + @missing_ids << hit["_id"] unless result | |
62 | 62 | result |
63 | 63 | end |
64 | 64 | |
65 | - if missing_ids.any? | |
66 | - Searchkick.warn("Records in search index do not exist in database: #{missing_ids.join(", ")}") | |
65 | + if @missing_ids.any? | |
66 | + Searchkick.warn("Records in search index do not exist in database: #{@missing_ids.join(", ")}") | |
67 | 67 | end |
68 | 68 | |
69 | 69 | results |
... | ... | @@ -92,6 +92,11 @@ module Searchkick |
92 | 92 | end |
93 | 93 | end |
94 | 94 | |
95 | + def missing_ids | |
96 | + with_hit | |
97 | + @missing_ids | |
98 | + end | |
99 | + | |
95 | 100 | def suggestions |
96 | 101 | if response["suggest"] |
97 | 102 | response["suggest"].values.flat_map { |v| v.first["options"] }.sort_by { |o| -o["score"] }.map { |o| o["text"] }.uniq | ... | ... |
test/search_test.rb
... | ... | @@ -37,12 +37,16 @@ class SearchTest < Minitest::Test |
37 | 37 | assert_equal ["Dollar Tree"], products.map(&:name) |
38 | 38 | end |
39 | 39 | |
40 | - def test_record_not_found | |
40 | + def test_missing_ids | |
41 | 41 | store_names ["Product A", "Product B"] |
42 | - Product.where(name: "Product A").delete_all | |
42 | + product = Product.find_by(name: "Product A") | |
43 | + product.delete | |
43 | 44 | assert_output nil, /\[searchkick\] WARNING: Records in search index do not exist in database/ do |
44 | - assert_search "product", ["Product B"] | |
45 | + result = Product.search("product") | |
46 | + assert_equal ["Product B"], result.map(&:name) | |
47 | + assert_equal [product.id.to_s], result.missing_ids | |
45 | 48 | end |
49 | + assert_equal [], Product.search("product", load: false).missing_ids | |
46 | 50 | ensure |
47 | 51 | Product.reindex |
48 | 52 | end | ... | ... |