Commit a72f58056c469ba413a9040a56906bb25cbfddcf

Authored by Andrew Kane
1 parent 2f16c8da

Added multi search

lib/searchkick.rb
@@ -138,6 +138,16 @@ module Searchkick @@ -138,6 +138,16 @@ module Searchkick
138 query.execute 138 query.execute
139 end 139 end
140 end 140 end
  141 +
  142 + def self.multi_search(queries)
  143 + if queries.any?
  144 + responses = client.msearch(body: queries.flat_map { |q| [q.params.except(:body), q.body] })["responses"]
  145 + queries.each_with_index do |query, i|
  146 + query.send(:handle_response, responses[i])
  147 + end
  148 + end
  149 + true
  150 + end
141 end 151 end
142 152
143 # TODO find better ActiveModel hook 153 # TODO find better ActiveModel hook
lib/searchkick/query.rb
@@ -129,7 +129,7 @@ module Searchkick @@ -129,7 +129,7 @@ module Searchkick
129 match_suffix: @match_suffix, 129 match_suffix: @match_suffix,
130 highlighted_fields: @highlighted_fields || [] 130 highlighted_fields: @highlighted_fields || []
131 } 131 }
132 - Searchkick::Results.new(searchkick_klass, response, opts) 132 + @execute = Searchkick::Results.new(searchkick_klass, response, opts)
133 end 133 end
134 134
135 def reindex_command 135 def reindex_command
test/multi_search_test.rb 0 → 100644
@@ -0,0 +1,13 @@ @@ -0,0 +1,13 @@
  1 +require_relative "test_helper"
  2 +
  3 +class MultiSearchTest < Minitest::Test
  4 + def test_basic
  5 + store_names ["Product A"]
  6 + store_names ["Store A"], Store
  7 + products = Product.search("*", execute: false)
  8 + stores = Store.search("*", execute: false)
  9 + Searchkick.multi_search([products, stores])
  10 + assert_equal ["Product A"], products.map(&:name)
  11 + assert_equal ["Store A"], stores.map(&:name)
  12 + end
  13 +end