Commit bc11e4139245d0e95f040d40d9bd157edcc196fd
1 parent
471ec8aa
Exists in
master
and in
21 other branches
Add option to force search types
Showing
2 changed files
with
19 additions
and
1 deletions
Show diff stats
lib/searchkick/search.rb
... | ... | @@ -350,7 +350,7 @@ module Searchkick |
350 | 350 | payload[:fields] = [] if load |
351 | 351 | |
352 | 352 | tire_options = {load: load, payload: payload, size: per_page, from: offset} |
353 | - tire_options[:type] = document_type if self != searchkick_klass | |
353 | + tire_options[:type] = handle_types(options[:types]) | |
354 | 354 | search = Tire::Search::Search.new(index_name, tire_options) |
355 | 355 | begin |
356 | 356 | response = search.json |
... | ... | @@ -377,5 +377,11 @@ module Searchkick |
377 | 377 | Searchkick::Results.new(response, search.options.merge(term: term)) |
378 | 378 | end |
379 | 379 | |
380 | + private | |
381 | + def handle_types(types) | |
382 | + return types.map(&:document_type) if types | |
383 | + document_type if self != searchkick_klass | |
384 | + end | |
385 | + | |
380 | 386 | end |
381 | 387 | end | ... | ... |
test/inheritance_test.rb
... | ... | @@ -25,6 +25,18 @@ class TestInheritance < Minitest::Unit::TestCase |
25 | 25 | assert_equal 2, Animal.search("bear").size |
26 | 26 | end |
27 | 27 | |
28 | + def test_force_one_type | |
29 | + store_names ["Green Bear"], Dog | |
30 | + store_names ["Blue Bear"], Cat | |
31 | + assert_equal ["Blue Bear"], Animal.search("bear", types: [Cat]).map(&:name) | |
32 | + end | |
33 | + | |
34 | + def test_force_multiple_types | |
35 | + store_names ["Green Bear"], Dog | |
36 | + store_names ["Blue Bear"], Cat | |
37 | + assert_equal ["Green Bear", "Blue Bear"], Animal.search("bear", types: [Dog, Cat]).map(&:name) | |
38 | + end | |
39 | + | |
28 | 40 | def test_child_autocomplete |
29 | 41 | store_names ["Max"], Cat |
30 | 42 | store_names ["Mark"], Dog | ... | ... |