Commit efb67565650e2cf88d93245b35db133cd49bf78e

Authored by Andrew Kane
1 parent 85b7b4cb

Fixed exclude case for exact match

Showing 2 changed files with 10 additions and 2 deletions   Show diff stats
lib/searchkick/query.rb
@@ -284,6 +284,7 @@ module Searchkick @@ -284,6 +284,7 @@ module Searchkick
284 shared_options[:operator] = operator if match_type == :match 284 shared_options[:operator] = operator if match_type == :match
285 285
286 exclude_analyzer = nil 286 exclude_analyzer = nil
  287 + exclude_field = field
287 288
288 if field == "_all" || field.end_with?(".analyzed") 289 if field == "_all" || field.end_with?(".analyzed")
289 shared_options[:cutoff_frequency] = 0.001 unless operator == "and" || misspellings == false 290 shared_options[:cutoff_frequency] = 0.001 unless operator == "and" || misspellings == false
@@ -295,6 +296,8 @@ module Searchkick @@ -295,6 +296,8 @@ module Searchkick
295 elsif field.end_with?(".exact") 296 elsif field.end_with?(".exact")
296 f = field.split(".")[0..-2].join(".") 297 f = field.split(".")[0..-2].join(".")
297 queries_to_add << {match: {f => shared_options.merge(analyzer: "keyword")}} 298 queries_to_add << {match: {f => shared_options.merge(analyzer: "keyword")}}
  299 + exclude_field = f
  300 + exclude_analyzer = "keyword"
298 else 301 else
299 analyzer = field =~ /\.word_(start|middle|end)\z/ ? "searchkick_word_search" : "searchkick_autocomplete_search" 302 analyzer = field =~ /\.word_(start|middle|end)\z/ ? "searchkick_word_search" : "searchkick_autocomplete_search"
300 qs << shared_options.merge(analyzer: analyzer) 303 qs << shared_options.merge(analyzer: analyzer)
@@ -323,12 +326,12 @@ module Searchkick @@ -323,12 +326,12 @@ module Searchkick
323 queries_to_add.concat(q2) 326 queries_to_add.concat(q2)
324 end 327 end
325 328
326 - if options[:exclude] && exclude_analyzer 329 + if options[:exclude]
327 must_not = 330 must_not =
328 options[:exclude].map do |phrase| 331 options[:exclude].map do |phrase|
329 { 332 {
330 match_phrase: { 333 match_phrase: {
331 - field => { 334 + exclude_field => {
332 query: phrase, 335 query: phrase,
333 analyzer: exclude_analyzer 336 analyzer: exclude_analyzer
334 } 337 }
test/match_test.rb
@@ -176,6 +176,11 @@ class MatchTest &lt; Minitest::Test @@ -176,6 +176,11 @@ class MatchTest &lt; Minitest::Test
176 assert_search "butter", [], exclude: ["peanut butter"], match: :exact 176 assert_search "butter", [], exclude: ["peanut butter"], match: :exact
177 end 177 end
178 178
  179 + def test_exclude_same_exact
  180 + store_names ["Butter Tub", "Peanut Butter Tub"]
  181 + assert_search "Butter Tub", [], exclude: ["Butter Tub"], match: :exact
  182 + end
  183 +
179 def test_exclude_egg_word_start 184 def test_exclude_egg_word_start
180 store_names ["eggs", "eggplant"] 185 store_names ["eggs", "eggplant"]
181 assert_search "egg", ["eggs"], exclude: ["eggplant"], match: :word_start 186 assert_search "egg", ["eggs"], exclude: ["eggplant"], match: :word_start