Commit 432c3754e9ad3917e51440bcd07b313ae746a814
Exists in
master
and in
21 other branches
Merge branch 'misspellings-prefix-length' of https://github.com/picocandy/search…
…kick into picocandy-misspellings-prefix-length
Showing
3 changed files
with
27 additions
and
2 deletions
Show diff stats
README.md
... | ... | @@ -290,6 +290,18 @@ Product.search "mikl", misspellings: {transpositions: true} # milk |
290 | 290 | |
291 | 291 | This is planned to be the default in Searchkick 1.0. |
292 | 292 | |
293 | +For short queries, it may be helpful to turn on mispellings after a specified number of letters. | |
294 | + | |
295 | +```ruby | |
296 | +Product.search "api", misspellings: {prefix_length:2} # api, apt, not any | |
297 | +``` | |
298 | + | |
299 | +**Note:** If query length is the same as `prefix_length`, misspellings is automatically turned off. | |
300 | + | |
301 | +```ruby | |
302 | +Product.search "ah", misspellings: {prefix_length:2} # ah, not aha | |
303 | +``` | |
304 | + | |
293 | 305 | ### Indexing |
294 | 306 | |
295 | 307 | Control what data is indexed with the `search_data` method. Call `Product.reindex` after changing this method. | ... | ... |
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", "aha"] | |
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"}, | ... | ... |