Skip to content

Commit a228b12

Browse files
committed
Add multiple searches
1 parent 0075485 commit a228b12

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

meilisearch/client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# pylint: disable=too-many-public-methods
2+
13
from __future__ import annotations
24

35
import base64
@@ -205,6 +207,30 @@ def index(self, uid: str) -> Index:
205207
return Index(self.config, uid=uid)
206208
raise ValueError("The index UID should not be None")
207209

210+
def multi_search(self, queries: Dict[str, Any]) -> Dict[str, Any]:
211+
"""Multi Search in the index.
212+
213+
Parameters
214+
----------
215+
queries:
216+
Dictionary containing the query searched word(s) and the query parameters
217+
https://docs.meilisearch.com/reference/api/search.html#search-in-an-index
218+
219+
Returns
220+
-------
221+
results:
222+
Dictionary of results for each index with hits, offset, limit, processingTime and initial query
223+
224+
Raises
225+
------
226+
MeiliSearchApiError
227+
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
228+
"""
229+
return self.http.post(
230+
f"{self.config.paths.multi_search}",
231+
body={"queries": queries},
232+
)
233+
208234
def get_all_stats(self) -> Dict[str, Any]:
209235
"""Get all stats of Meilisearch
210236

meilisearch/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Paths:
1616
task = "tasks"
1717
stat = "stats"
1818
search = "search"
19+
multi_search = "multi-search"
1920
document = "documents"
2021
setting = "settings"
2122
ranking_rules = "ranking-rules"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
3+
4+
def test_basic_multi_search(client, empty_index):
5+
"""Tests multi-search on two indexes."""
6+
empty_index("indexA")
7+
empty_index("indexB")
8+
response = client.multi_search(
9+
[{"indexUid": "indexA", "q": ""}, {"indexUid": "indexB", "q": ""}]
10+
)
11+
12+
assert isinstance(response, dict)
13+
assert response["results"][0]["indexUid"] == "indexA"
14+
assert response["results"][1]["indexUid"] == "indexB"
15+
assert response["results"][0]["limit"] == 20
16+
assert response["results"][1]["limit"] == 20
17+
18+
19+
def test_multi_search_one_index(client, empty_index):
20+
"""Tests multi-search on a simple query."""
21+
empty_index("indexA")
22+
response = client.multi_search([{"indexUid": "indexA", "q": ""}])
23+
24+
assert isinstance(response, dict)
25+
assert response["results"][0]["indexUid"] == "indexA"
26+
assert response["results"][0]["limit"] == 20
27+
28+
29+
def test_multi_search_on_no_index(client):
30+
"""Tests multi-search on a non existing index."""
31+
with pytest.raises(Exception):
32+
client.multi_search([{"indexUid": "indexDoesNotExist", "q": ""}])

0 commit comments

Comments
 (0)