Skip to content

Commit 9ed0e0c

Browse files
meili-bors[bot]Nymuxyzoellnix
authored
Merge #567
567: Federated search added r=brunoocasali a=Nymuxyzo # Pull Request ## Related issue Fixes #558 ## What does this PR do? - Federated search added ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Co-authored-by: André <[email protected]> Co-authored-by: ellnix <[email protected]>
2 parents 9123ebb + 01a293b commit 9ed0e0c

File tree

3 files changed

+78
-4
lines changed

3 files changed

+78
-4
lines changed

.code-samples.meilisearch.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,3 +712,8 @@ distinct_attribute_guide_distinct_parameter_1: |-
712712
client.index('products').search('white shirt', {
713713
distinct: 'sku'
714714
})
715+
multi_search_federated_1: |-
716+
client.multi_search(
717+
queries: [{ index_uid: 'movies', q: 'batman' }, { index_uid: 'comics', q: 'batman' }],
718+
federation: {}
719+
)

lib/meilisearch/multi_search.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,20 @@
22

33
module Meilisearch
44
module MultiSearch
5-
def multi_search(data)
6-
body = Utils.transform_attributes(data)
5+
# Performs search on one or more indexes
6+
#
7+
# @param [Hash] federation_options
8+
# - `limit`: number of results in the merged list
9+
# - `offset`: number of results to skip in the merged list
10+
def multi_search(data = nil, queries: [], federation: nil)
11+
Utils.soft_deprecate('multi_search([])', 'multi_search(queries: [])') if data
712

8-
http_post '/multi-search', queries: body
13+
queries += data if data
14+
15+
queries = Utils.transform_attributes(queries)
16+
federation = Utils.transform_attributes(federation)
17+
18+
http_post '/multi-search', queries: queries, federation: federation
919
end
1020
end
1121
end

spec/meilisearch/client/multi_search_spec.rb

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
end
88

99
it 'does a custom search with two different indexes' do
10-
response = client.multi_search([
10+
response = client.multi_search(queries: [
1111
{ index_uid: 'books', q: 'prince' },
1212
{ index_uid: 'movies', q: 'prince' }
1313
])
@@ -16,4 +16,63 @@
1616
expect(response['results'][0]['estimatedTotalHits']).to eq(0)
1717
expect(response['results'][1]['estimatedTotalHits']).to eq(0)
1818
end
19+
20+
context 'when passed a positional argument' do
21+
before { allow(Meilisearch::Utils).to receive(:soft_deprecate).and_return(nil) }
22+
23+
it 'does a custom search with two different indexes' do
24+
response = client.multi_search([
25+
{ index_uid: 'books', q: 'prince' },
26+
{ index_uid: 'movies', q: 'prince' }
27+
])
28+
29+
expect(response['results'].count).to eq(2)
30+
expect(response['results'][0]['estimatedTotalHits']).to eq(0)
31+
expect(response['results'][1]['estimatedTotalHits']).to eq(0)
32+
end
33+
34+
it 'warns about deprecation' do
35+
client.multi_search([
36+
{ index_uid: 'books', q: 'prince' },
37+
{ index_uid: 'movies', q: 'prince' }
38+
])
39+
40+
expect(Meilisearch::Utils).to have_received(:soft_deprecate)
41+
.with('multi_search([])', a_string_including('queries'))
42+
end
43+
end
44+
45+
it 'does a federated search with two different indexes' do
46+
client.index('books').add_documents(
47+
[
48+
{ id: 1, title: 'Harry Potter and the Philosophers Stone' },
49+
{ id: 2, title: 'War and Peace' },
50+
{ id: 5, title: 'Harry Potter and the Deathly Hallows' }
51+
]
52+
)
53+
54+
client.index('movies').add_documents(
55+
[
56+
{ id: 1, title: 'Harry Potter and the Philosophers Stone' },
57+
{ id: 2, title: 'Lord of the Rings' },
58+
{ id: 4, title: 'Harry Potter and the Order of the Phoenix' }
59+
]
60+
).await
61+
62+
response = client.multi_search(queries: [
63+
{ index_uid: 'books', q: 'Harry Potter' },
64+
{ index_uid: 'movies', q: 'Harry Potter' }
65+
],
66+
federation: {
67+
limit: 3,
68+
offset: 1
69+
})
70+
71+
expect(response['limit']).to eq(3)
72+
expect(response['offset']).to eq(1)
73+
74+
hits = response['hits']
75+
expect(hits.size).to be 3
76+
expect(hits.first).to have_key('_federation')
77+
end
1978
end

0 commit comments

Comments
 (0)