Skip to content

Commit b45f482

Browse files
committed
Federated Search Addition
1 parent 7dbade5 commit b45f482

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

meilisearch/client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,17 @@ 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(self, queries: Sequence[Mapping[str, Any]], federation: Optional[Dict[str, Any]] = None) -> Dict[str, List[Dict[str, Any]]]:
223223
"""Multi-index search.
224224
225225
Parameters
226226
----------
227227
queries:
228228
List of dictionaries containing the specified indexes and their search queries
229229
https://www.meilisearch.com/docs/reference/api/search#search-in-an-index
230+
federation: (optional):
231+
List of dictionaries containing offset and limit
232+
https://www.meilisearch.com/docs/reference/api/multi_search
230233
231234
Returns
232235
-------
@@ -240,7 +243,7 @@ def multi_search(self, queries: Sequence[Mapping[str, Any]]) -> Dict[str, List[D
240243
"""
241244
return self.http.post(
242245
f"{self.config.paths.multi_search}",
243-
body={"queries": queries},
246+
body={"queries": queries, "federation": federation},
244247
)
245248

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

tests/client/test_client_multi_search_meilisearch.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,28 @@ def test_multi_search_on_no_index(client):
3232
"""Tests multi-search on a non existing index."""
3333
with pytest.raises(MeilisearchApiError):
3434
client.multi_search([{"indexUid": "indexDoesNotExist", "q": ""}])
35+
36+
37+
def test_multi_search_with_no_value_in_federation(client, empty_index):
38+
"""Tests multi-search with federation, but no value"""
39+
empty_index("indexA")
40+
response = client.multi_search([{"indexUid": "indexA", "q": ""}], {})
41+
42+
assert response["hits"][0]["_federation"]['indexUid'] == "indexA" if len(response["hits"]) >= 1 else isinstance(response, dict)
43+
44+
45+
def test_multi_search_with_offset_and_limit_in_federation(client, empty_index):
46+
"""Tests multi-search with federation, with offset and limit value"""
47+
empty_index("indexA")
48+
response = client.multi_search([{"indexUid": "indexA", "q": ""}], {"offset": 2, "limit": 2})
49+
50+
assert len(response["hits"]) == 2 if response['hits'] else True
51+
52+
53+
def test_multi_search_with_federation_options(client, empty_index):
54+
"""Tests multi-search with federation, with federation options"""
55+
empty_index("indexA")
56+
response = client.multi_search([{"indexUid": "indexA", "q": "", "federationOptions": {"weight": 0.99}}], {"offset": 2, "limit": 2})
57+
58+
assert isinstance(response["hits"], list)
59+
assert response["hits"][0]["_federation"]["weightedRankingScore"] < 0.99 if len(response["hits"]) >= 1 else True

0 commit comments

Comments
 (0)