diff --git a/README.md b/README.md index b6479e0..0293208 100644 --- a/README.md +++ b/README.md @@ -259,11 +259,9 @@ Reindex and search with: ```ruby products = Product.search "peantu butta", suggest: true -products.suggestion # peanut butter +products.suggestions # ["peanut butter"] ``` -Returns `nil` when there are no suggestions. - ### Facets ```ruby diff --git a/lib/searchkick/results.rb b/lib/searchkick/results.rb index 79b232b..27b9f60 100644 --- a/lib/searchkick/results.rb +++ b/lib/searchkick/results.rb @@ -3,9 +3,9 @@ module Searchkick # TODO use all fields # return nil suggestion if term does not change - def suggestion + def suggestions if @response["suggest"] - @response["suggest"].values.first.first["options"].first["text"] rescue nil + @response["suggest"].values.first.first["options"].map{|s| s["text"] } rescue [] else raise "Pass `suggest: true` to the search method for suggestions" end diff --git a/test/suggest_test.rb b/test/suggest_test.rb index a2a9bdb..0a5b756 100644 --- a/test/suggest_test.rb +++ b/test/suggest_test.rb @@ -18,13 +18,13 @@ class TestSuggest < Minitest::Unit::TestCase end def test_without_option - assert_raises(RuntimeError){ Product.search("hi").suggestion } + assert_raises(RuntimeError){ Product.search("hi").suggestions } end protected def assert_suggest(term, expected) - assert_equal expected, Product.search(term, suggest: true).suggestion + assert_equal expected, Product.search(term, suggest: true).suggestions.first end end -- libgit2 0.21.0