Commit 53bebb76555cf9747cadfce4421ad4153b101ead

Authored by Roy Tay
1 parent 892e5f39

Add prefix_length option for misspellings

Showing 2 changed files with 15 additions and 2 deletions   Show diff stats
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"},
... ...