diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f00773..11bd28a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 1.2.2 [unreleased] + +- Added support for phrase matches + ## 1.2.1 - Added `multi_search` method diff --git a/README.md b/README.md index 13da462..52f5b6f 100644 --- a/README.md +++ b/README.md @@ -247,6 +247,12 @@ Available options are: User.search params[:q], fields: [{email: :exact}, :name] ``` ++### Phrase Matches [master] + +```ruby +User.search "fresh honey", match: :phrase +``` + ### Language Searchkick defaults to English for stemming. To change this, use: diff --git a/lib/searchkick/query.rb b/lib/searchkick/query.rb index 15193cd..1d65bc9 100644 --- a/lib/searchkick/query.rb +++ b/lib/searchkick/query.rb @@ -238,6 +238,14 @@ module Searchkick boost: 10 * factor } + match_type = + if field.end_with?(".phrase") + field = field.sub(/\.phrase\z/, ".analyzed") + :match_phrase + else + :match + end + if field == "_all" || field.end_with?(".analyzed") shared_options[:cutoff_frequency] = 0.001 unless operator == "and" || misspellings == false qs.concat [ @@ -256,7 +264,7 @@ 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 - queries.concat(qs.map { |q| {match: {field => q}} }) + queries.concat(qs.map { |q| {match_type => {field => q}} }) end payload = { diff --git a/test/match_test.rb b/test/match_test.rb index a70caea..3463439 100644 --- a/test/match_test.rb +++ b/test/match_test.rb @@ -191,6 +191,11 @@ class MatchTest < Minitest::Test assert_search "ben & jerrys", ["Ben and Jerry's"] end + def test_phrase + store_names ["Fresh Honey", "Honey Fresh"] + assert_search "fresh honey", ["Fresh Honey"], match: :phrase + end + def test_unsearchable store [ {name: "Unsearchable", description: "Almond"} -- libgit2 0.21.0