Commit 40103178ef7cab1f6e0655e95344b43339609440

Authored by Andrew
1 parent c3056795

More intuitive per-field misspellings with partial matches - #1194

lib/searchkick/query.rb
... ... @@ -281,10 +281,10 @@ module Searchkick
281 281 prefix_length = (misspellings.is_a?(Hash) && misspellings[:prefix_length]) || 0
282 282 default_max_expansions = @misspellings_below ? 20 : 3
283 283 max_expansions = (misspellings.is_a?(Hash) && misspellings[:max_expansions]) || default_max_expansions
284   - misspellings_fields = misspellings.is_a?(Hash) && misspellings.key?(:fields) && misspellings[:fields].map { |f| f.is_a?(Hash) ? f.to_a.join(".") : "#{f}.#{@match_suffix}" }
  284 + misspellings_fields = misspellings.is_a?(Hash) && misspellings.key?(:fields) && misspellings[:fields].map(&:to_s)
285 285  
286 286 if misspellings_fields
287   - missing_fields = misspellings_fields - fields
  287 + missing_fields = misspellings_fields - fields.map { |f| base_field(f) }
288 288 if missing_fields.any?
289 289 raise ArgumentError, "All fields in per-field misspellings must also be specified in fields option"
290 290 end
... ... @@ -324,7 +324,7 @@ module Searchkick
324 324 exclude_analyzer = nil
325 325 exclude_field = field
326 326  
327   - field_misspellings = misspellings && (!misspellings_fields || misspellings_fields.include?(field))
  327 + field_misspellings = misspellings && (!misspellings_fields || misspellings_fields.include?(base_field(field)))
328 328  
329 329 if field == "_all" || field.end_with?(".analyzed")
330 330 shared_options[:cutoff_frequency] = 0.001 unless operator.to_s == "and" || field_misspellings == false
... ... @@ -1007,6 +1007,10 @@ module Searchkick
1007 1007 end
1008 1008 end
1009 1009  
  1010 + def base_field(k)
  1011 + k.sub(/\.(analyzed|word_start|word_middle|word_end|text_start|text_middle|text_end|exact)\z/, "")
  1012 + end
  1013 +
1010 1014 def below52?
1011 1015 Searchkick.server_below?("5.2.0")
1012 1016 end
... ...
test/misspellings_test.rb
... ... @@ -100,6 +100,6 @@ class MisspellingsTest < Minitest::Test
100 100  
101 101 def test_misspellings_field_word_start
102 102 store_names ["Sriracha"]
103   - assert_search "siracha", ["Sriracha"], fields: [{name: :word_middle}], misspellings: {fields: [{name: :word_middle}]}
  103 + assert_search "siracha", ["Sriracha"], fields: [{name: :word_middle}], misspellings: {fields: [:name]}
104 104 end
105 105 end
... ...