Commit 854138782d7f52aaa831dec848c2145e52b67c3f

Authored by Andrew Kane
1 parent bce93a14

Added misspellings? method to results - closes #807

1 ## 2.0.3 [unreleased] 1 ## 2.0.3 [unreleased]
2 2
3 - Added `async` option to `reindex` [experimental] 3 - Added `async` option to `reindex` [experimental]
  4 +- Added `misspellings?` method to results
4 5
5 ## 2.0.2 6 ## 2.0.2
6 7
lib/searchkick/query.rb
@@ -34,6 +34,7 @@ module Searchkick @@ -34,6 +34,7 @@ module Searchkick
34 # prevent Ruby warnings 34 # prevent Ruby warnings
35 @type = nil 35 @type = nil
36 @routing = nil 36 @routing = nil
  37 + @misspellings = false
37 @misspellings_below = nil 38 @misspellings_below = nil
38 @highlighted_fields = nil 39 @highlighted_fields = nil
39 40
@@ -107,7 +108,8 @@ module Searchkick @@ -107,7 +108,8 @@ module Searchkick
107 includes: options[:includes], 108 includes: options[:includes],
108 json: !@json.nil?, 109 json: !@json.nil?,
109 match_suffix: @match_suffix, 110 match_suffix: @match_suffix,
110 - highlighted_fields: @highlighted_fields || [] 111 + highlighted_fields: @highlighted_fields || [],
  112 + misspellings: @misspellings
111 } 113 }
112 114
113 if options[:debug] 115 if options[:debug]
@@ -256,6 +258,9 @@ module Searchkick @@ -256,6 +258,9 @@ module Searchkick
256 prefix_length = (misspellings.is_a?(Hash) && misspellings[:prefix_length]) || 0 258 prefix_length = (misspellings.is_a?(Hash) && misspellings[:prefix_length]) || 0
257 default_max_expansions = @misspellings_below ? 20 : 3 259 default_max_expansions = @misspellings_below ? 20 : 3
258 max_expansions = (misspellings.is_a?(Hash) && misspellings[:max_expansions]) || default_max_expansions 260 max_expansions = (misspellings.is_a?(Hash) && misspellings[:max_expansions]) || default_max_expansions
  261 + @misspellings = true
  262 + else
  263 + @misspellings = false
259 end 264 end
260 265
261 fields.each do |field| 266 fields.each do |field|
lib/searchkick/results.rb
@@ -184,6 +184,10 @@ module Searchkick @@ -184,6 +184,10 @@ module Searchkick
184 @response["hits"]["hits"] 184 @response["hits"]["hits"]
185 end 185 end
186 186
  187 + def misspellings?
  188 + @options[:misspellings]
  189 + end
  190 +
187 private 191 private
188 192
189 def results_query(records, hits) 193 def results_query(records, hits)
test/misspellings_test.rb
@@ -39,8 +39,18 @@ class MisspellingsTest < Minitest::Test @@ -39,8 +39,18 @@ class MisspellingsTest < Minitest::Test
39 assert_search "abc", ["abc", "abd"], misspellings: {below: 2} 39 assert_search "abc", ["abc", "abd"], misspellings: {below: 2}
40 end 40 end
41 41
  42 + def test_misspellings_below_unmet_result
  43 + store_names ["abc", "abd", "aee"]
  44 + assert Product.search("abc", misspellings: {below: 2}).misspellings?
  45 + end
  46 +
42 def test_misspellings_below_met 47 def test_misspellings_below_met
43 store_names ["abc", "abd", "aee"] 48 store_names ["abc", "abd", "aee"]
44 assert_search "abc", ["abc"], misspellings: {below: 1} 49 assert_search "abc", ["abc"], misspellings: {below: 1}
45 end 50 end
  51 +
  52 + def test_misspellings_below_met_result
  53 + store_names ["abc", "abd", "aee"]
  54 + assert !Product.search("abc", misspellings: {below: 1}).misspellings?
  55 + end
46 end 56 end