Commit d4b3b2c6850719a5d6f27deaa7c8e8397b4b7f7c
1 parent
02df0da3
Exists in
master
and in
21 other branches
Return multiple suggestions
Showing
3 changed files
with
5 additions
and
7 deletions
Show diff stats
README.md
... | ... | @@ -259,11 +259,9 @@ Reindex and search with: |
259 | 259 | |
260 | 260 | ```ruby |
261 | 261 | products = Product.search "peantu butta", suggest: true |
262 | -products.suggestion # peanut butter | |
262 | +products.suggestions # ["peanut butter"] | |
263 | 263 | ``` |
264 | 264 | |
265 | -Returns `nil` when there are no suggestions. | |
266 | - | |
267 | 265 | ### Facets |
268 | 266 | |
269 | 267 | ```ruby | ... | ... |
lib/searchkick/results.rb
... | ... | @@ -3,9 +3,9 @@ module Searchkick |
3 | 3 | |
4 | 4 | # TODO use all fields |
5 | 5 | # return nil suggestion if term does not change |
6 | - def suggestion | |
6 | + def suggestions | |
7 | 7 | if @response["suggest"] |
8 | - @response["suggest"].values.first.first["options"].first["text"] rescue nil | |
8 | + @response["suggest"].values.first.first["options"].map{|s| s["text"] } rescue [] | |
9 | 9 | else |
10 | 10 | raise "Pass `suggest: true` to the search method for suggestions" |
11 | 11 | end | ... | ... |
test/suggest_test.rb
... | ... | @@ -18,13 +18,13 @@ class TestSuggest < Minitest::Unit::TestCase |
18 | 18 | end |
19 | 19 | |
20 | 20 | def test_without_option |
21 | - assert_raises(RuntimeError){ Product.search("hi").suggestion } | |
21 | + assert_raises(RuntimeError){ Product.search("hi").suggestions } | |
22 | 22 | end |
23 | 23 | |
24 | 24 | protected |
25 | 25 | |
26 | 26 | def assert_suggest(term, expected) |
27 | - assert_equal expected, Product.search(term, suggest: true).suggestion | |
27 | + assert_equal expected, Product.search(term, suggest: true).suggestions.first | |
28 | 28 | end |
29 | 29 | |
30 | 30 | end | ... | ... |