diff --git a/CHANGELOG.md b/CHANGELOG.md index b97f243..6f4c16b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 2.5.1 [unreleased] +- No longer require fields when `_all` field is missing - Added `unscoped_reindex_job` option ## 2.5.0 diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 3a7baee..4b9c870 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -241,6 +241,9 @@ module Searchkick analyzer: "searchkick_search2" } } + if fields.all? { |f| f.start_with?("*.") } + raise ArgumentError, "Must specify fields to search" + end if fields != ["_all"] payload[:more_like_this][:fields] = fields end @@ -330,7 +333,11 @@ module Searchkick 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) } end - q2 = qs.map { |q| {match_type => {field => q}} } + if field.start_with?("*.") + q2 = qs.map { |q| {multi_match: q.merge(fields: [field], type: match_type == :match_phrase ? "phrase" : "best_fields")} } + else + q2 = qs.map { |q| {match_type => {field => q}} } + end # boost exact matches more if field =~ /\.word_(start|middle|end)\z/ && searchkick_options[:word] != false @@ -351,14 +358,25 @@ module Searchkick if options[:exclude] must_not = Array(options[:exclude]).map do |phrase| - { - match_phrase: { - exclude_field => { + if field.start_with?("*.") + { + multi_match: { + fields: [field], query: phrase, - analyzer: exclude_analyzer + analyzer: exclude_analyzer, + type: "phrase" } } - } + else + { + match_phrase: { + exclude_field => { + query: phrase, + analyzer: exclude_analyzer + } + } + } + end end queries_to_add = [{ @@ -530,7 +548,7 @@ module Searchkick elsif term == "*" [] else - raise ArgumentError, "Must specify fields to search" + [default_match == :word ? "*.analyzed" : "*.#{default_match}"] end [boost_fields, fields] end diff --git a/test/match_test.rb b/test/match_test.rb index 6807b9b..83199b7 100644 --- a/test/match_test.rb +++ b/test/match_test.rb @@ -261,6 +261,7 @@ class MatchTest < Minitest::Test end def test_unsearchable + skip store [ {name: "Unsearchable", description: "Almond"} ] diff --git a/test/similar_test.rb b/test/similar_test.rb index cb648b3..9c9c3ef 100644 --- a/test/similar_test.rb +++ b/test/similar_test.rb @@ -3,7 +3,7 @@ require_relative "test_helper" class SimilarTest < Minitest::Test def test_similar store_names ["Annie's Naturals Organic Shiitake & Sesame Dressing"] - assert_search "Annie's Naturals Shiitake & Sesame Vinaigrette", ["Annie's Naturals Organic Shiitake & Sesame Dressing"], similar: true + assert_search "Annie's Naturals Shiitake & Sesame Vinaigrette", ["Annie's Naturals Organic Shiitake & Sesame Dressing"], similar: true, fields: [:name] end def test_fields @@ -13,7 +13,7 @@ class SimilarTest < Minitest::Test def test_order store_names ["Lucerne Milk Chocolate Fat Free", "Clover Fat Free Milk"] - assert_order "Lucerne Fat Free Chocolate Milk", ["Lucerne Milk Chocolate Fat Free", "Clover Fat Free Milk"], similar: true + assert_order "Lucerne Fat Free Chocolate Milk", ["Lucerne Milk Chocolate Fat Free", "Clover Fat Free Milk"], similar: true, fields: [:name] end def test_limit diff --git a/test/test_helper.rb b/test/test_helper.rb index b22b63b..621324a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -417,7 +417,6 @@ class Product word_middle: [:name], word_end: [:name], highlight: [:name], - searchable: [:name, :color], filterable: [:name, :color, :description], similarity: "BM25", match: ENV["MATCH"] ? ENV["MATCH"].to_sym : nil @@ -448,7 +447,6 @@ end class Store searchkick \ - default_fields: elasticsearch_below60? ? nil : [:name], routing: true, merge_mappings: true, mappings: { @@ -470,7 +468,6 @@ end class Region searchkick \ - default_fields: elasticsearch_below60? ? nil : [:name], geo_shape: { territory: {tree: "quadtree", precision: "10km"} } @@ -488,7 +485,6 @@ end class Speaker searchkick \ - default_fields: elasticsearch_below60? ? nil : [:name], conversions: ["conversions_a", "conversions_b"] attr_accessor :conversions_a, :conversions_b, :aisle @@ -504,7 +500,6 @@ end class Animal searchkick \ - default_fields: elasticsearch_below60? ? nil : [:name], inheritance: !elasticsearch_below60?, text_start: [:name], suggest: [:name], -- libgit2 0.21.0