Commit 56341623b976117e37e460a1cb0664954a527a0d

Authored by Roy Tay
1 parent 91244626

Disable cutoff_frequency when misspellings is false because high frequency terms…

… are then not matched
Showing 2 changed files with 27 additions and 5 deletions   Show diff stats
lib/searchkick/query.rb
... ... @@ -96,11 +96,6 @@ module Searchkick
96 96 }
97 97  
98 98 if field == "_all" || field.end_with?(".analyzed")
99   - shared_options[:cutoff_frequency] = 0.001 unless operator == "and"
100   - qs.concat [
101   - shared_options.merge(boost: 10 * factor, analyzer: "searchkick_search"),
102   - shared_options.merge(boost: 10 * factor, analyzer: "searchkick_search2")
103   - ]
104 99 misspellings = options.key?(:misspellings) ? options[:misspellings] : options[:mispellings] # why not?
105 100 if misspellings != false
106 101 edit_distance = (misspellings.is_a?(Hash) && (misspellings[:edit_distance] || misspellings[:distance])) || 1
... ... @@ -110,6 +105,11 @@ module Searchkick
110 105 shared_options.merge(fuzziness: edit_distance, max_expansions: 3, analyzer: "searchkick_search2").merge(transpositions)
111 106 ]
112 107 end
  108 + shared_options[:cutoff_frequency] = 0.001 unless operator == "and" || misspellings == false
  109 + qs.concat [
  110 + shared_options.merge(boost: 10 * factor, analyzer: "searchkick_search"),
  111 + shared_options.merge(boost: 10 * factor, analyzer: "searchkick_search2")
  112 + ]
113 113 elsif field.end_with?(".exact")
114 114 f = field.split(".")[0..-2].join(".")
115 115 queries << {match: {f => shared_options.merge(analyzer: "keyword")}}
... ...
test/sql_test.rb
... ... @@ -244,6 +244,28 @@ class TestSql &lt; Minitest::Test
244 244 assert_search "aaaa", ["aabb"], misspellings: {distance: 2}
245 245 end
246 246  
  247 + def test_misspellings_fields_operator
  248 + store [
  249 + {name: "red", color: "red"},
  250 + {name: "blue", color: "blue"},
  251 + {name: "cyan", color: "blue green"},
  252 + {name: "magenta", color: "red blue"},
  253 + {name: "green", color: "green"}
  254 + ]
  255 + assert_search "red blue", ["red", "blue", "cyan", "magenta"], operator: "or", fields: ["color"], misspellings: false
  256 + end
  257 +
  258 + def test_fields_operator
  259 + store [
  260 + {name: "red", color: "red"},
  261 + {name: "blue", color: "blue"},
  262 + {name: "cyan", color: "blue green"},
  263 + {name: "magenta", color: "red blue"},
  264 + {name: "green", color: "green"}
  265 + ]
  266 + assert_search "red blue", ["red", "blue", "cyan", "magenta"], operator: "or", fields: ["color"]
  267 + end
  268 +
247 269 def test_fields
248 270 store [
249 271 {name: "red", color: "light blue"},
... ...