From 6ee2d2270a4c474fe289fd5ca45154ca8ad47628 Mon Sep 17 00:00:00 2001 From: Andrew Date: Thu, 17 May 2018 00:57:58 -0700 Subject: [PATCH] Made exclude option work with match all --- CHANGELOG.md | 4 ++++ lib/searchkick/query.rb | 25 ++++++++++++++++--------- test/match_test.rb | 11 +++++++++++ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50b012f..ae8ee1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.1.1 [unreleased] + +- Made `exclude` option work with match all + ## 3.1.0 - Added `:inline` as alias for `true` for `callbacks` and `mode` options diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index d89ed1c..82afcab 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -250,7 +250,7 @@ module Searchkick if fields != ["_all"] query[:more_like_this][:fields] = fields end - elsif all + elsif all && !options[:exclude] query = { match_all: {} } @@ -372,13 +372,22 @@ module Searchkick end end - payload = { - dis_max: { - queries: queries + # all + exclude option + if all + query = { + match_all: {} + } + + should = [] + else + payload = { + dis_max: { + queries: queries + } } - } - should.concat(set_conversions) + should.concat(set_conversions) + end query = payload end @@ -487,9 +496,7 @@ module Searchkick ["_all"] elsif all && default_match == :phrase ["_all.phrase"] - elsif term == "*" - [] - elsif default_match == :exact + elsif term != "*" && default_match == :exact raise ArgumentError, "Must specify fields to search" else [default_match == :word ? "*.analyzed" : "*.#{default_match}"] diff --git a/test/match_test.rb b/test/match_test.rb index 39519b1..89e149e 100644 --- a/test/match_test.rb +++ b/test/match_test.rb @@ -202,6 +202,17 @@ class MatchTest < Minitest::Test assert_search "butter", ["Butter Tub"], exclude: "peanut butter" end + def test_exclude_match_all + store_names ["Butter"] + assert_search "*", [], exclude: "butter" + end + + def test_exclude_match_all_fields + store_names ["Butter"] + assert_search "*", [], fields: [:name], exclude: "butter" + assert_search "*", ["Butter"], fields: [:color], exclude: "butter" + end + # other def test_all -- libgit2 0.21.0