Commit 689c28c22ec52f651d654c06c2166d8284885197

Authored by Andrew Kane
1 parent 305e7994

Fixed error when suggestions empty

CHANGELOG.md
  1 +## 4.0.1 [unreleased]
  2 +
  3 +- Fixed error when suggestions empty
  4 +
1 5 ## 4.0.0
2 6  
3 7 - Added support for Elasticsearch 7
... ...
lib/searchkick/query.rb
... ... @@ -127,7 +127,8 @@ module Searchkick
127 127 term: term,
128 128 scope_results: options[:scope_results],
129 129 total_entries: options[:total_entries],
130   - index_mapping: @index_mapping
  130 + index_mapping: @index_mapping,
  131 + suggest: options[:suggest]
131 132 }
132 133  
133 134 if options[:debug]
... ...
lib/searchkick/results.rb
... ... @@ -90,7 +90,7 @@ module Searchkick
90 90 def suggestions
91 91 if response["suggest"]
92 92 response["suggest"].values.flat_map { |v| v.first["options"] }.sort_by { |o| -o["score"] }.map { |o| o["text"] }.uniq
93   - elsif options[:term] == "*"
  93 + elsif options[:suggest] || options[:term] == "*" # TODO remove 2nd term
94 94 []
95 95 else
96 96 raise "Pass `suggest: true` to the search method for suggestions"
... ...
test/suggest_test.rb
... ... @@ -21,6 +21,10 @@ class SuggestTest < Minitest::Test
21 21 assert_suggest "How to catch a big tiger shar", "how to catch a big tiger shark", fields: [:name]
22 22 end
23 23  
  24 + def test_empty
  25 + assert_suggest "hi", nil
  26 + end
  27 +
24 28 def test_without_option
25 29 store_names ["hi"] # needed to prevent ElasticsearchException - seed 668
26 30 assert_raises(RuntimeError) { Product.search("hi").suggestions }
... ...