Commit 5950aafce5cdea4ed6a01ae998c48255339d9b64
1 parent
61b9102a
Exists in
master
and in
21 other branches
Added support for phrase matches - closes #412
Showing
4 changed files
with
24 additions
and
1 deletions
Show diff stats
CHANGELOG.md
README.md
... | ... | @@ -247,6 +247,12 @@ Available options are: |
247 | 247 | User.search params[:q], fields: [{email: :exact}, :name] |
248 | 248 | ``` |
249 | 249 | |
250 | ++### Phrase Matches [master] | |
251 | + | |
252 | +```ruby | |
253 | +User.search "fresh honey", match: :phrase | |
254 | +``` | |
255 | + | |
250 | 256 | ### Language |
251 | 257 | |
252 | 258 | Searchkick defaults to English for stemming. To change this, use: | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -238,6 +238,14 @@ module Searchkick |
238 | 238 | boost: 10 * factor |
239 | 239 | } |
240 | 240 | |
241 | + match_type = | |
242 | + if field.end_with?(".phrase") | |
243 | + field = field.sub(/\.phrase\z/, ".analyzed") | |
244 | + :match_phrase | |
245 | + else | |
246 | + :match | |
247 | + end | |
248 | + | |
241 | 249 | if field == "_all" || field.end_with?(".analyzed") |
242 | 250 | shared_options[:cutoff_frequency] = 0.001 unless operator == "and" || misspellings == false |
243 | 251 | qs.concat [ |
... | ... | @@ -256,7 +264,7 @@ module Searchkick |
256 | 264 | 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) } |
257 | 265 | end |
258 | 266 | |
259 | - queries.concat(qs.map { |q| {match: {field => q}} }) | |
267 | + queries.concat(qs.map { |q| {match_type => {field => q}} }) | |
260 | 268 | end |
261 | 269 | |
262 | 270 | payload = { | ... | ... |
test/match_test.rb
... | ... | @@ -191,6 +191,11 @@ class MatchTest < Minitest::Test |
191 | 191 | assert_search "ben & jerrys", ["Ben and Jerry's"] |
192 | 192 | end |
193 | 193 | |
194 | + def test_phrase | |
195 | + store_names ["Fresh Honey", "Honey Fresh"] | |
196 | + assert_search "fresh honey", ["Fresh Honey"], match: :phrase | |
197 | + end | |
198 | + | |
194 | 199 | def test_unsearchable |
195 | 200 | store [ |
196 | 201 | {name: "Unsearchable", description: "Almond"} | ... | ... |