Commit ef8f7acab6ccd861f384523ddeffb547748357f1
Committed by
Andrew Kane
1 parent
393da35e
Exists in
master
and in
19 other branches
Make multi_search work with misspellings_below option
Showing
3 changed files
with
16 additions
and
2 deletions
Show diff stats
lib/searchkick.rb
... | ... | @@ -104,8 +104,14 @@ module Searchkick |
104 | 104 | def self.multi_search(queries) |
105 | 105 | if queries.any? |
106 | 106 | responses = client.msearch(body: queries.flat_map { |q| [q.params.except(:body), q.body] })["responses"] |
107 | + | |
107 | 108 | queries.each_with_index do |query, i| |
108 | - query.handle_response(responses[i]) | |
109 | + if query.misspellings_below && responses[i]["hits"]["total"] < query.misspellings_below | |
110 | + query.send(:prepare) | |
111 | + query.execute | |
112 | + else | |
113 | + query.handle_response(responses[i]) | |
114 | + end | |
109 | 115 | end |
110 | 116 | end |
111 | 117 | queries | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -4,7 +4,7 @@ module Searchkick |
4 | 4 | |
5 | 5 | @@metric_aggs = [:avg, :cardinality, :max, :min, :sum] |
6 | 6 | |
7 | - attr_reader :klass, :term, :options | |
7 | + attr_reader :klass, :term, :options, :misspellings_below | |
8 | 8 | attr_accessor :body |
9 | 9 | |
10 | 10 | def_delegators :execute, :map, :each, :any?, :empty?, :size, :length, :slice, :[], :to_ary, | ... | ... |
test/multi_search_test.rb
... | ... | @@ -19,4 +19,12 @@ class MultiSearchTest < Minitest::Test |
19 | 19 | assert !products.error |
20 | 20 | assert stores.error |
21 | 21 | end |
22 | + | |
23 | + def test_misspellings_below_unmet | |
24 | + store_names ["Product A"] | |
25 | + store_names ["Store A"], Store | |
26 | + stores = Store.search("Stre A", misspellings: { below: 2 }, execute: false) | |
27 | + Searchkick.multi_search([stores]) | |
28 | + assert_equal ["Store A"], stores.map(&:name) | |
29 | + end | |
22 | 30 | end | ... | ... |