Skip to content

Commit e1d210e

Browse files
committed
Add index_name argument to multi_search
Allows for naming the group of results differently from the name of the index being searched, therefore allowing the same index to be searched multiple times with different options.
1 parent 5c408f1 commit e1d210e

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

lib/meilisearch/rails/multi_search.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ module Rails
55
class << self
66
def multi_search(searches)
77
search_parameters = searches.map do |(index_target, options)|
8+
index_target = options.delete(:index_name) || index_target
9+
810
paginate(options) if pagination_enabled?
911
normalize(options, index_target)
1012
end

lib/meilisearch/rails/multi_search/result.rb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@ def initialize(searches, raw_results)
77
@results = {}
88
@metadata = {}
99

10-
searches.zip(raw_results['results']).each do |(index_target, search_options), result|
11-
index_target = search_options[:class_name].constantize if search_options[:class_name]
10+
searches.zip(raw_results['results']).each do |(target, search_options), result|
11+
results_class = if search_options[:class_name]
12+
search_options[:class_name].constantize
13+
elsif target.instance_of?(Class)
14+
target
15+
end
1216

13-
@results[index_target] = case index_target
14-
when String, Symbol
15-
result['hits']
16-
else
17-
load_results(index_target, result)
18-
end
17+
@results[target] = results_class ? load_results(results_class, result) : result['hits']
1918

20-
@metadata[index_target] = result.except('hits')
19+
@metadata[target] = result.except('hits')
2120
end
2221
end
2322

spec/multi_search_spec.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
require 'spec_helper'
2+
require 'support/models/book'
3+
require 'support/models/product'
4+
require 'support/models/color'
25

36
describe 'multi-search' do
47
def reset_indexes
@@ -43,6 +46,65 @@ def reset_indexes
4346
end
4447
end
4548

49+
context 'with arbitrary keys' do
50+
context 'when index_name is not present' do
51+
it 'assumes key is index and errors' do
52+
expect do
53+
MeiliSearch::Rails.multi_search(
54+
'test_group' => { q: 'Steve' }
55+
)
56+
end.to raise_error(MeiliSearch::ApiError)
57+
end
58+
end
59+
60+
context 'when :index_name is present' do
61+
it 'searches the correct index' do
62+
results = MeiliSearch::Rails.multi_search(
63+
'books' => { q: 'Steve', index_name: Book.index.uid },
64+
'products' => { q: 'palm', index_name: Product.index.uid, limit: 1 },
65+
'colors' => { q: 'bl', index_name: Color.index.uid }
66+
)
67+
68+
expect(results).to contain_exactly(
69+
a_hash_including('author' => 'Walter Isaacson', 'name' => 'Steve Jobs'),
70+
a_hash_including('name' => 'palm pixi plus'),
71+
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
72+
a_hash_including('name' => 'black', 'short_name' => 'bla')
73+
)
74+
end
75+
76+
it 'allows searching the same index n times' do
77+
index_name = Color.index.uid
78+
79+
results = MeiliSearch::Rails.multi_search(
80+
'dark_colors' => { q: 'black', index_name: index_name },
81+
'bright_colors' => { q: 'blue', index_name: index_name },
82+
'nature_colors' => { q: 'green', index_name: index_name }
83+
)
84+
85+
expect(results).to contain_exactly(
86+
a_hash_including('name' => 'blue', 'short_name' => 'blu'),
87+
a_hash_including('name' => 'black', 'short_name' => 'bla'),
88+
a_hash_including('name' => 'green', 'short_name' => 'gre')
89+
)
90+
end
91+
92+
context 'when :class_name is also present' do
93+
it 'loads results from the correct models' do
94+
results = MeiliSearch::Rails.multi_search(
95+
'books' => { q: 'Steve', index_name: Book.index.uid, class_name: 'Book' },
96+
'products' => { q: 'palm', limit: 1, index_name: Product.index.uid, class_name: 'Product' },
97+
'colors' => { q: 'bl', index_name: Color.index.uid, class_name: 'Color' }
98+
)
99+
100+
expect(results).to contain_exactly(
101+
steve_jobs, palm_pixi_plus, blue, black
102+
)
103+
end
104+
end
105+
end
106+
end
107+
46108
context 'with index name keys' do
47109
it 'returns hashes' do
48110
results = MeiliSearch::Rails.multi_search(

0 commit comments

Comments
 (0)