Commit d1fa932bcc4840b04d89ac471e8a51a1184dbb75
1 parent
6f69dddc
Exists in
master
and in
19 other branches
Added support for passing fields to suggest option - fixes #932
Showing
3 changed files
with
23 additions
and
6 deletions
Show diff stats
CHANGELOG.md
lib/searchkick/query.rb
... | ... | @@ -447,7 +447,7 @@ module Searchkick |
447 | 447 | set_aggregations(payload) if options[:aggs] |
448 | 448 | |
449 | 449 | # suggestions |
450 | - set_suggestions(payload) if options[:suggest] | |
450 | + set_suggestions(payload, options[:suggest]) if options[:suggest] | |
451 | 451 | |
452 | 452 | # highlight |
453 | 453 | set_highlights(payload, fields) if options[:highlight] |
... | ... | @@ -575,12 +575,20 @@ module Searchkick |
575 | 575 | payload[:indices_boost] = indices_boost |
576 | 576 | end |
577 | 577 | |
578 | - def set_suggestions(payload) | |
579 | - suggest_fields = (searchkick_options[:suggest] || []).map(&:to_s) | |
578 | + def set_suggestions(payload, suggest) | |
579 | + suggest_fields = nil | |
580 | 580 | |
581 | - # intersection | |
582 | - if options[:fields] | |
583 | - suggest_fields &= options[:fields].map { |v| (v.is_a?(Hash) ? v.keys.first : v).to_s.split("^", 2).first } | |
581 | + if suggest.is_a?(Array) | |
582 | + suggest_fields = suggest | |
583 | + elsif !klass | |
584 | + raise ArgumentError, "Must pass fields to suggest option" | |
585 | + else | |
586 | + suggest_fields = (searchkick_options[:suggest] || []).map(&:to_s) | |
587 | + | |
588 | + # intersection | |
589 | + if options[:fields] | |
590 | + suggest_fields &= options[:fields].map { |v| (v.is_a?(Hash) ? v.keys.first : v).to_s.split("^", 2).first } | |
591 | + end | |
584 | 592 | end |
585 | 593 | |
586 | 594 | if suggest_fields.any? | ... | ... |
test/suggest_test.rb
... | ... | @@ -67,6 +67,11 @@ class SuggestTest < Minitest::Test |
67 | 67 | assert_suggest "How Big is a Tigre Shar", "how big is a tiger shark", fields: [{"name^2" => :word_start}] |
68 | 68 | end |
69 | 69 | |
70 | + def test_multiple_models | |
71 | + store_names ["Great White Shark", "Hammerhead Shark", "Tiger Shark"] | |
72 | + assert_equal "how big is a tiger shark", Searchkick.search("How Big is a Tigre Shar", suggest: [:name]).suggestions.first | |
73 | + end | |
74 | + | |
70 | 75 | protected |
71 | 76 | |
72 | 77 | def assert_suggest(term, expected, options = {}) | ... | ... |