Skip to content

Commit 316e137

Browse files
Merge #1013
1013: Federated Search Addition r=sanders41 a=MuddyHope # Pull Request ## Related issue Fixes #1009 ## What does this PR do? - Adds the federated search to the `multi-search` method ## 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? Thank you so much for contributing to Meilisearch! Co-authored-by: MuddyHope <[email protected]> Co-authored-by: MuddyHope <[email protected]>
2 parents 7dbade5 + 2361c9d commit 316e137

File tree

2 files changed

+50
-2
lines changed

2 files changed

+50
-2
lines changed

meilisearch/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,19 @@ def index(self, uid: str) -> Index:
219219
return Index(self.config, uid=uid)
220220
raise ValueError("The index UID should not be None")
221221

222-
def multi_search(self, queries: Sequence[Mapping[str, Any]]) -> Dict[str, List[Dict[str, Any]]]:
222+
def multi_search(
223+
self, queries: Sequence[Mapping[str, Any]], federation: Optional[Dict[str, Any]] = None
224+
) -> Dict[str, List[Dict[str, Any]]]:
223225
"""Multi-index search.
224226
225227
Parameters
226228
----------
227229
queries:
228230
List of dictionaries containing the specified indexes and their search queries
229231
https://www.meilisearch.com/docs/reference/api/search#search-in-an-index
232+
federation: (optional):
233+
Dictionary containing offset and limit
234+
https://www.meilisearch.com/docs/reference/api/multi_search
230235
231236
Returns
232237
-------
@@ -240,7 +245,7 @@ def multi_search(self, queries: Sequence[Mapping[str, Any]]) -> Dict[str, List[D
240245
"""
241246
return self.http.post(
242247
f"{self.config.paths.multi_search}",
243-
body={"queries": queries},
248+
body={"queries": queries, "federation": federation},
244249
)
245250

246251
def get_all_stats(self) -> Dict[str, Any]:

tests/client/test_client_multi_search_meilisearch.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from meilisearch.errors import MeilisearchApiError
4+
from tests.common import INDEX_UID
45

56

67
def test_basic_multi_search(client, empty_index):
@@ -32,3 +33,45 @@ def test_multi_search_on_no_index(client):
3233
"""Tests multi-search on a non existing index."""
3334
with pytest.raises(MeilisearchApiError):
3435
client.multi_search([{"indexUid": "indexDoesNotExist", "q": ""}])
36+
37+
38+
def test_multi_search_with_no_value_in_federation(client, empty_index, index_with_documents):
39+
"""Tests multi-search with federation, but no value"""
40+
index_with_documents()
41+
empty_index("indexB")
42+
response = client.multi_search(
43+
[{"indexUid": INDEX_UID, "q": ""}, {"indexUid": "indexB", "q": ""}], {}
44+
)
45+
assert "results" not in response
46+
assert len(response["hits"]) > 0
47+
assert "_federation" in response["hits"][0]
48+
assert response["limit"] == 20
49+
assert response["offset"] == 0
50+
51+
52+
def test_multi_search_with_offset_and_limit_in_federation(client, index_with_documents):
53+
"""Tests multi-search with federation, with offset and limit value"""
54+
index_with_documents()
55+
response = client.multi_search([{"indexUid": INDEX_UID, "q": ""}], {"offset": 2, "limit": 2})
56+
57+
assert "results" not in response
58+
assert len(response["hits"]) == 2
59+
assert "_federation" in response["hits"][0]
60+
assert response["limit"] == 2
61+
assert response["offset"] == 2
62+
63+
64+
def test_multi_search_with_federation_options(client, index_with_documents):
65+
"""Tests multi-search with federation, with federation options"""
66+
index_with_documents()
67+
response = client.multi_search(
68+
[{"indexUid": INDEX_UID, "q": "", "federationOptions": {"weight": 0.99}}], {"limit": 2}
69+
)
70+
71+
assert "results" not in response
72+
assert isinstance(response["hits"], list)
73+
assert len(response["hits"]) == 2
74+
assert response["hits"][0]["_federation"]["indexUid"] == INDEX_UID
75+
assert response["hits"][0]["_federation"]["weightedRankingScore"] >= 0.99
76+
assert response["limit"] == 2
77+
assert response["offset"] == 0

0 commit comments

Comments
 (0)