Skip to content

Commit 5d7305d

Browse files
committed
Add multiple searches
1 parent 0075485 commit 5d7305d

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

meilisearch/client.py

Lines changed: 27 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,31 @@ 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+
211+
def multi_search(self, queries: Dict[str, Any]) -> Dict[str, Any]:
212+
"""Multi Search in the index.
213+
214+
Parameters
215+
----------
216+
queries:
217+
Dictionary containing the query searched word(s) and the query parameters
218+
https://docs.meilisearch.com/reference/api/search.html#search-in-an-index
219+
220+
Returns
221+
-------
222+
results:
223+
Dictionary of results for each index with hits, offset, limit, processingTime and initial query
224+
225+
Raises
226+
------
227+
MeiliSearchApiError
228+
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
229+
"""
230+
return self.http.post(
231+
f"{self.config.paths.multi_search}",
232+
body={'queries': queries},
233+
)
234+
208235
def get_all_stats(self) -> Dict[str, Any]:
209236
"""Get all stats of Meilisearch
210237

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: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pytest
2+
3+
def test_basic_multi_search(client, empty_index):
4+
"""Tests multi-search on two indexes."""
5+
empty_index("indexA")
6+
empty_index("indexB")
7+
response = client.multi_search([
8+
{ 'indexUid': 'indexA', 'q': ''},
9+
{ '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+
def test_multi_search_one_index(client, empty_index):
19+
"""Tests multi-search on a simple query."""
20+
empty_index("indexA")
21+
response = client.multi_search([
22+
{ 'indexUid': 'indexA', 'q': ''}
23+
])
24+
25+
assert isinstance(response, dict)
26+
assert response["results"][0]["indexUid"] == "indexA"
27+
assert response["results"][0]["limit"] == 20
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([
33+
{ 'indexUid': 'indexDoesNotExist', 'q': ''}
34+
])

0 commit comments

Comments
 (0)