Commit 46d7eb1b854e7a60048427770de7eed8be828258
Exists in
master
and in
21 other branches
Merge branch 'picocandy-misspellings-prefix-length'
Showing
4 changed files
with
29 additions
and
2 deletions
Show diff stats
CHANGELOG.md
README.md
... | ... | @@ -1009,6 +1009,18 @@ Reindex all models - Rails only |
1009 | 1009 | rake searchkick:reindex:all |
1010 | 1010 | ``` |
1011 | 1011 | |
1012 | +Turn on misspellings after a certain number of characters | |
1013 | + | |
1014 | +```ruby | |
1015 | +Product.search "api", misspellings: {prefix_length: 2} # api, apt, no ahi | |
1016 | +``` | |
1017 | + | |
1018 | +**Note:** With this option, if the query length is the same as `prefix_length`, misspellings are turned off | |
1019 | + | |
1020 | +```ruby | |
1021 | +Product.search "ah", misspellings: {prefix_length: 2} # ah, no aha | |
1022 | +``` | |
1023 | + | |
1012 | 1024 | ## Large Data Sets |
1013 | 1025 | |
1014 | 1026 | For large data sets, check out [Keeping Elasticsearch in Sync](https://www.found.no/foundation/keeping-elasticsearch-in-sync/). Searchkick will make this easy in the future. | ... | ... |
lib/searchkick/query.rb
... | ... | @@ -100,9 +100,10 @@ module Searchkick |
100 | 100 | if misspellings != false |
101 | 101 | edit_distance = (misspellings.is_a?(Hash) && (misspellings[:edit_distance] || misspellings[:distance])) || 1 |
102 | 102 | transpositions = (misspellings.is_a?(Hash) && misspellings[:transpositions] == true) ? {fuzzy_transpositions: true} : {} |
103 | + prefix_length = (misspellings.is_a?(Hash) && misspellings[:prefix_length]) || 0 | |
103 | 104 | qs.concat [ |
104 | - shared_options.merge(fuzziness: edit_distance, max_expansions: 3, analyzer: "searchkick_search").merge(transpositions), | |
105 | - shared_options.merge(fuzziness: edit_distance, max_expansions: 3, analyzer: "searchkick_search2").merge(transpositions) | |
105 | + shared_options.merge(fuzziness: edit_distance, prefix_length: prefix_length, max_expansions: 3, analyzer: "searchkick_search").merge(transpositions), | |
106 | + shared_options.merge(fuzziness: edit_distance, prefix_length: prefix_length, max_expansions: 3, analyzer: "searchkick_search2").merge(transpositions) | |
106 | 107 | ] |
107 | 108 | end |
108 | 109 | shared_options[:cutoff_frequency] = 0.001 unless operator == "and" || misspellings == false | ... | ... |
test/sql_test.rb
... | ... | @@ -244,6 +244,18 @@ class TestSql < Minitest::Test |
244 | 244 | assert_search "aaaa", ["aabb"], misspellings: {distance: 2} |
245 | 245 | end |
246 | 246 | |
247 | + def test_misspellings_prefix_length | |
248 | + store_names ["ap", "api", "apt", "any", "nap", "ah", "ahi"] | |
249 | + assert_search "ap", ["ap"], misspellings: {prefix_length: 2} | |
250 | + assert_search "api", ["ap", "api", "apt"], misspellings: {prefix_length: 2} | |
251 | + end | |
252 | + | |
253 | + def test_misspellings_prefix_length_operator | |
254 | + store_names ["ap", "api", "apt", "any", "nap", "ah", "aha"] | |
255 | + assert_search "ap ah", ["ap", "ah"], operator: "or", misspellings: {prefix_length: 2} | |
256 | + assert_search "api ahi", ["ap", "api", "apt", "ah", "aha"], operator: "or", misspellings: {prefix_length: 2} | |
257 | + end | |
258 | + | |
247 | 259 | def test_misspellings_fields_operator |
248 | 260 | store [ |
249 | 261 | {name: "red", color: "red"}, | ... | ... |