Commit 6ee2d2270a4c474fe289fd5ca45154ca8ad47628
1 parent
36028aeb
Exists in
master
and in
18 other branches
Made exclude option work with match all
Showing
3 changed files
with
31 additions
and
9 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/query.rb
... | ... | @@ -250,7 +250,7 @@ module Searchkick |
250 | 250 | if fields != ["_all"] |
251 | 251 | query[:more_like_this][:fields] = fields |
252 | 252 | end |
253 | - elsif all | |
253 | + elsif all && !options[:exclude] | |
254 | 254 | query = { |
255 | 255 | match_all: {} |
256 | 256 | } |
... | ... | @@ -372,13 +372,22 @@ module Searchkick |
372 | 372 | end |
373 | 373 | end |
374 | 374 | |
375 | - payload = { | |
376 | - dis_max: { | |
377 | - queries: queries | |
375 | + # all + exclude option | |
376 | + if all | |
377 | + query = { | |
378 | + match_all: {} | |
379 | + } | |
380 | + | |
381 | + should = [] | |
382 | + else | |
383 | + payload = { | |
384 | + dis_max: { | |
385 | + queries: queries | |
386 | + } | |
378 | 387 | } |
379 | - } | |
380 | 388 | |
381 | - should.concat(set_conversions) | |
389 | + should.concat(set_conversions) | |
390 | + end | |
382 | 391 | |
383 | 392 | query = payload |
384 | 393 | end |
... | ... | @@ -487,9 +496,7 @@ module Searchkick |
487 | 496 | ["_all"] |
488 | 497 | elsif all && default_match == :phrase |
489 | 498 | ["_all.phrase"] |
490 | - elsif term == "*" | |
491 | - [] | |
492 | - elsif default_match == :exact | |
499 | + elsif term != "*" && default_match == :exact | |
493 | 500 | raise ArgumentError, "Must specify fields to search" |
494 | 501 | else |
495 | 502 | [default_match == :word ? "*.analyzed" : "*.#{default_match}"] | ... | ... |
test/match_test.rb
... | ... | @@ -202,6 +202,17 @@ class MatchTest < Minitest::Test |
202 | 202 | assert_search "butter", ["Butter Tub"], exclude: "peanut butter" |
203 | 203 | end |
204 | 204 | |
205 | + def test_exclude_match_all | |
206 | + store_names ["Butter"] | |
207 | + assert_search "*", [], exclude: "butter" | |
208 | + end | |
209 | + | |
210 | + def test_exclude_match_all_fields | |
211 | + store_names ["Butter"] | |
212 | + assert_search "*", [], fields: [:name], exclude: "butter" | |
213 | + assert_search "*", ["Butter"], fields: [:color], exclude: "butter" | |
214 | + end | |
215 | + | |
205 | 216 | # other |
206 | 217 | |
207 | 218 | def test_all | ... | ... |