Commit 6ee2d2270a4c474fe289fd5ca45154ca8ad47628

Authored by Andrew
1 parent 36028aeb

Made exclude option work with match all

  1 +## 3.1.1 [unreleased]
  2 +
  3 +- Made `exclude` option work with match all
  4 +
1 ## 3.1.0 5 ## 3.1.0
2 6
3 - Added `:inline` as alias for `true` for `callbacks` and `mode` options 7 - Added `:inline` as alias for `true` for `callbacks` and `mode` options
lib/searchkick/query.rb
@@ -250,7 +250,7 @@ module Searchkick @@ -250,7 +250,7 @@ module Searchkick
250 if fields != ["_all"] 250 if fields != ["_all"]
251 query[:more_like_this][:fields] = fields 251 query[:more_like_this][:fields] = fields
252 end 252 end
253 - elsif all 253 + elsif all && !options[:exclude]
254 query = { 254 query = {
255 match_all: {} 255 match_all: {}
256 } 256 }
@@ -372,13 +372,22 @@ module Searchkick @@ -372,13 +372,22 @@ module Searchkick
372 end 372 end
373 end 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 query = payload 392 query = payload
384 end 393 end
@@ -487,9 +496,7 @@ module Searchkick @@ -487,9 +496,7 @@ module Searchkick
487 ["_all"] 496 ["_all"]
488 elsif all && default_match == :phrase 497 elsif all && default_match == :phrase
489 ["_all.phrase"] 498 ["_all.phrase"]
490 - elsif term == "*"  
491 - []  
492 - elsif default_match == :exact 499 + elsif term != "*" && default_match == :exact
493 raise ArgumentError, "Must specify fields to search" 500 raise ArgumentError, "Must specify fields to search"
494 else 501 else
495 [default_match == :word ? "*.analyzed" : "*.#{default_match}"] 502 [default_match == :word ? "*.analyzed" : "*.#{default_match}"]
test/match_test.rb
@@ -202,6 +202,17 @@ class MatchTest < Minitest::Test @@ -202,6 +202,17 @@ class MatchTest < Minitest::Test
202 assert_search "butter", ["Butter Tub"], exclude: "peanut butter" 202 assert_search "butter", ["Butter Tub"], exclude: "peanut butter"
203 end 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 # other 216 # other
206 217
207 def test_all 218 def test_all