Skip to content

Commit e62979b

Browse files
committed
Allow passing collections to scope multi search
1 parent 5450df2 commit e62979b

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

lib/meilisearch/rails/multi_search.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def multi_search(searches)
1818

1919
def normalize(options, index_target)
2020
options
21-
.except(:class_name)
21+
.except(:class_name, :collection)
2222
.merge!(index_uid: index_uid_from_target(index_target))
2323
end
2424

lib/meilisearch/rails/multi_search/result.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def initialize(searches, raw_results)
1414
target
1515
end
1616

17-
@results[target] = results_class ? load_results(results_class, result) : result['hits']
17+
@results[target] = results_class ? load_results(results_class, result, collection: search_options[:collection]) : result['hits']
1818

1919
@metadata[target] = result.except('hits')
2020
end
@@ -69,7 +69,7 @@ def to_h
6969

7070
private
7171

72-
def load_results(klass, result)
72+
def load_results(klass, result, collection: klass)
7373
pk_method = klass.ms_primary_key_method
7474
pk_method = pk_method.in if Utilities.mongo_model?(klass)
7575

@@ -78,7 +78,7 @@ def load_results(klass, result)
7878
hits_by_id =
7979
result['hits'].index_by { |hit| hit[condition_key.to_s] }
8080

81-
records = klass.where(condition_key => hits_by_id.keys)
81+
records = collection.where(condition_key => hits_by_id.keys)
8282

8383
if records.respond_to? :in_order_of
8484
records.in_order_of(condition_key, hits_by_id.keys).each do |record|

spec/multi_search_spec.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,17 @@ def reset_indexes
180180
MeiliSearch::Rails.configuration[:pagination_backend] = nil
181181
end
182182
end
183+
184+
context 'with collections' do
185+
it 'fetches items from the given collection' do
186+
results = MeiliSearch::Rails.multi_search(
187+
Product.index.uid => { q: 'palm', class_name: 'Product', collection: Product.where('tags LIKE "%terrible%"') },
188+
Color => { q: 'bl', collection: Color.where(short_name: 'bla') }
189+
)
190+
191+
expect(results).to contain_exactly(
192+
black, palm_pixi_plus
193+
)
194+
end
195+
end
183196
end

0 commit comments

Comments
 (0)