Commit 695021af58ff4c581a52a2413aa8857e661eabea
1 parent
b888bfe2
Exists in
master
and in
19 other branches
No longer require fields when _all field is missing
Showing
5 changed files
with
29 additions
and
14 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/query.rb
... | ... | @@ -241,6 +241,9 @@ module Searchkick |
241 | 241 | analyzer: "searchkick_search2" |
242 | 242 | } |
243 | 243 | } |
244 | + if fields.all? { |f| f.start_with?("*.") } | |
245 | + raise ArgumentError, "Must specify fields to search" | |
246 | + end | |
244 | 247 | if fields != ["_all"] |
245 | 248 | payload[:more_like_this][:fields] = fields |
246 | 249 | end |
... | ... | @@ -330,7 +333,11 @@ module Searchkick |
330 | 333 | qs.concat qs.map { |q| q.except(:cutoff_frequency).merge(fuzziness: edit_distance, prefix_length: prefix_length, max_expansions: max_expansions, boost: factor).merge(transpositions) } |
331 | 334 | end |
332 | 335 | |
333 | - q2 = qs.map { |q| {match_type => {field => q}} } | |
336 | + if field.start_with?("*.") | |
337 | + q2 = qs.map { |q| {multi_match: q.merge(fields: [field], type: match_type == :match_phrase ? "phrase" : "best_fields")} } | |
338 | + else | |
339 | + q2 = qs.map { |q| {match_type => {field => q}} } | |
340 | + end | |
334 | 341 | |
335 | 342 | # boost exact matches more |
336 | 343 | if field =~ /\.word_(start|middle|end)\z/ && searchkick_options[:word] != false |
... | ... | @@ -351,14 +358,25 @@ module Searchkick |
351 | 358 | if options[:exclude] |
352 | 359 | must_not = |
353 | 360 | Array(options[:exclude]).map do |phrase| |
354 | - { | |
355 | - match_phrase: { | |
356 | - exclude_field => { | |
361 | + if field.start_with?("*.") | |
362 | + { | |
363 | + multi_match: { | |
364 | + fields: [field], | |
357 | 365 | query: phrase, |
358 | - analyzer: exclude_analyzer | |
366 | + analyzer: exclude_analyzer, | |
367 | + type: "phrase" | |
359 | 368 | } |
360 | 369 | } |
361 | - } | |
370 | + else | |
371 | + { | |
372 | + match_phrase: { | |
373 | + exclude_field => { | |
374 | + query: phrase, | |
375 | + analyzer: exclude_analyzer | |
376 | + } | |
377 | + } | |
378 | + } | |
379 | + end | |
362 | 380 | end |
363 | 381 | |
364 | 382 | queries_to_add = [{ |
... | ... | @@ -530,7 +548,7 @@ module Searchkick |
530 | 548 | elsif term == "*" |
531 | 549 | [] |
532 | 550 | else |
533 | - raise ArgumentError, "Must specify fields to search" | |
551 | + [default_match == :word ? "*.analyzed" : "*.#{default_match}"] | |
534 | 552 | end |
535 | 553 | [boost_fields, fields] |
536 | 554 | end | ... | ... |
test/match_test.rb
test/similar_test.rb
... | ... | @@ -3,7 +3,7 @@ require_relative "test_helper" |
3 | 3 | class SimilarTest < Minitest::Test |
4 | 4 | def test_similar |
5 | 5 | store_names ["Annie's Naturals Organic Shiitake & Sesame Dressing"] |
6 | - assert_search "Annie's Naturals Shiitake & Sesame Vinaigrette", ["Annie's Naturals Organic Shiitake & Sesame Dressing"], similar: true | |
6 | + assert_search "Annie's Naturals Shiitake & Sesame Vinaigrette", ["Annie's Naturals Organic Shiitake & Sesame Dressing"], similar: true, fields: [:name] | |
7 | 7 | end |
8 | 8 | |
9 | 9 | def test_fields |
... | ... | @@ -13,7 +13,7 @@ class SimilarTest < Minitest::Test |
13 | 13 | |
14 | 14 | def test_order |
15 | 15 | store_names ["Lucerne Milk Chocolate Fat Free", "Clover Fat Free Milk"] |
16 | - assert_order "Lucerne Fat Free Chocolate Milk", ["Lucerne Milk Chocolate Fat Free", "Clover Fat Free Milk"], similar: true | |
16 | + assert_order "Lucerne Fat Free Chocolate Milk", ["Lucerne Milk Chocolate Fat Free", "Clover Fat Free Milk"], similar: true, fields: [:name] | |
17 | 17 | end |
18 | 18 | |
19 | 19 | def test_limit | ... | ... |
test/test_helper.rb
... | ... | @@ -417,7 +417,6 @@ class Product |
417 | 417 | word_middle: [:name], |
418 | 418 | word_end: [:name], |
419 | 419 | highlight: [:name], |
420 | - searchable: [:name, :color], | |
421 | 420 | filterable: [:name, :color, :description], |
422 | 421 | similarity: "BM25", |
423 | 422 | match: ENV["MATCH"] ? ENV["MATCH"].to_sym : nil |
... | ... | @@ -448,7 +447,6 @@ end |
448 | 447 | |
449 | 448 | class Store |
450 | 449 | searchkick \ |
451 | - default_fields: elasticsearch_below60? ? nil : [:name], | |
452 | 450 | routing: true, |
453 | 451 | merge_mappings: true, |
454 | 452 | mappings: { |
... | ... | @@ -470,7 +468,6 @@ end |
470 | 468 | |
471 | 469 | class Region |
472 | 470 | searchkick \ |
473 | - default_fields: elasticsearch_below60? ? nil : [:name], | |
474 | 471 | geo_shape: { |
475 | 472 | territory: {tree: "quadtree", precision: "10km"} |
476 | 473 | } |
... | ... | @@ -488,7 +485,6 @@ end |
488 | 485 | |
489 | 486 | class Speaker |
490 | 487 | searchkick \ |
491 | - default_fields: elasticsearch_below60? ? nil : [:name], | |
492 | 488 | conversions: ["conversions_a", "conversions_b"] |
493 | 489 | |
494 | 490 | attr_accessor :conversions_a, :conversions_b, :aisle |
... | ... | @@ -504,7 +500,6 @@ end |
504 | 500 | |
505 | 501 | class Animal |
506 | 502 | searchkick \ |
507 | - default_fields: elasticsearch_below60? ? nil : [:name], | |
508 | 503 | inheritance: !elasticsearch_below60?, |
509 | 504 | text_start: [:name], |
510 | 505 | suggest: [:name], | ... | ... |