Commit 2a506a2465889d324b2cf25379c47ec7d9df938f
1 parent
222c700a
Exists in
master
and in
21 other branches
Boost exact matches for partial matching
Showing
3 changed files
with
27 additions
and
2 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/query.rb
... | ... | @@ -271,7 +271,21 @@ module Searchkick |
271 | 271 | 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) } |
272 | 272 | end |
273 | 273 | |
274 | - queries.concat(qs.map { |q| {match_type => {field => q}} }) | |
274 | + # boost exact matches more | |
275 | + if field =~ /\.word_(start|middle|end)\z/ && searchkick_options[:word] != false | |
276 | + queries << { | |
277 | + bool: { | |
278 | + must: { | |
279 | + bool: { | |
280 | + should: qs.map { |q| {match_type => {field => q}} } | |
281 | + } | |
282 | + }, | |
283 | + should: {match_type => {field.sub(/\.word_(start|middle|end)\z/, ".analyzed") => qs.first}} | |
284 | + } | |
285 | + } | |
286 | + else | |
287 | + queries.concat(qs.map { |q| {match_type => {field => q}} }) | |
288 | + end | |
275 | 289 | end |
276 | 290 | |
277 | 291 | payload = { | ... | ... |
test/autocomplete_test.rb
... | ... | @@ -56,6 +56,16 @@ class AutocompleteTest < Minitest::Test |
56 | 56 | assert_search "dark grey", ["Dark Grey"], fields: [{name: :word_start}] |
57 | 57 | end |
58 | 58 | |
59 | + def test_word_start_exact | |
60 | + store_names ["Back Scratcher", "Backpack"] | |
61 | + assert_order "back", ["Back Scratcher", "Backpack"], fields: [{name: :word_start}] | |
62 | + end | |
63 | + | |
64 | + def test_word_start_exact_martin | |
65 | + store_names ["Martina", "Martin"] | |
66 | + assert_order "martin", ["Martin", "Martina"], fields: [{name: :word_start}] | |
67 | + end | |
68 | + | |
59 | 69 | # TODO find a better place |
60 | 70 | |
61 | 71 | def test_exact | ... | ... |