Skip to content

Commit

Permalink
Merge pull request #1023 from sanders41/meilisearch-v1.10.0
Browse files Browse the repository at this point in the history
Update for Meilisearch v1.10.0
  • Loading branch information
sanders41 authored Aug 26, 2024
2 parents 0bc33a5 + 0f2b5ab commit 8ce70a4
Show file tree
Hide file tree
Showing 11 changed files with 743 additions and 72 deletions.
65 changes: 58 additions & 7 deletions meilisearch_python_sdk/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
)
from meilisearch_python_sdk.models.health import Health
from meilisearch_python_sdk.models.index import IndexInfo
from meilisearch_python_sdk.models.search import SearchParams, SearchResultsWithUID
from meilisearch_python_sdk.models.search import (
Federation,
SearchParams,
SearchResultsFederated,
SearchResultsWithUID,
)
from meilisearch_python_sdk.models.settings import MeilisearchSettings
from meilisearch_python_sdk.models.task import TaskInfo, TaskResult, TaskStatus
from meilisearch_python_sdk.models.version import Version
Expand Down Expand Up @@ -633,13 +638,19 @@ async def update_key(self, key: KeyUpdate) -> Key:
return Key(**response.json())

async def multi_search(
self, queries: list[SearchParams], *, hits_type: Any = JsonDict
) -> list[SearchResultsWithUID]:
self,
queries: list[SearchParams],
*,
federation: Federation | None = None,
hits_type: Any = JsonDict,
) -> list[SearchResultsWithUID] | SearchResultsFederated:
"""Multi-index search.
Args:
queries: List of SearchParameters
federation: If included a single search result with hits built from all queries. This
parameter can only be used with Meilisearch >= v1.10.0. Defaults to None.
hits_type: Allows for a custom type to be passed to use for hits. Defaults to
JsonDict
Expand All @@ -664,11 +675,28 @@ async def multi_search(
>>> search_results = await client.search(queries)
"""
url = "multi-search"
if federation:
processed_queries = []
for query in queries:
q = query.model_dump(by_alias=True)
del q["limit"]
del q["offset"]
processed_queries.append(q)
else:
processed_queries = [x.model_dump(by_alias=True) for x in queries]

response = await self._http_requests.post(
url,
body={"queries": [x.model_dump(by_alias=True) for x in queries]}, # type: ignore[attr-defined]
body={
"federation": federation.model_dump(by_alias=True) if federation else None,
"queries": processed_queries,
},
)

if federation:
results = response.json()
return SearchResultsFederated[hits_type](**results)

return [SearchResultsWithUID[hits_type](**x) for x in response.json()["results"]]

async def get_raw_index(self, uid: str) -> IndexInfo | None:
Expand Down Expand Up @@ -1510,13 +1538,19 @@ def update_key(self, key: KeyUpdate) -> Key:
return Key(**response.json())

def multi_search(
self, queries: list[SearchParams], *, hits_type: Any = JsonDict
) -> list[SearchResultsWithUID]:
self,
queries: list[SearchParams],
*,
federation: Federation | None = None,
hits_type: Any = JsonDict,
) -> list[SearchResultsWithUID] | SearchResultsFederated:
"""Multi-index search.
Args:
queries: List of SearchParameters
federation: If included a single search result with hits built from all queries. This
parameter can only be used with Meilisearch >= v1.10.0. Defaults to None.
hits_type: Allows for a custom type to be passed to use for hits. Defaults to
JsonDict
Expand All @@ -1541,11 +1575,28 @@ def multi_search(
>>> search_results = client.search(queries)
"""
url = "multi-search"
if federation:
processed_queries = []
for query in queries:
q = query.model_dump(by_alias=True)
del q["limit"]
del q["offset"]
processed_queries.append(q)
else:
processed_queries = [x.model_dump(by_alias=True) for x in queries]

response = self._http_requests.post(
url,
body={"queries": [x.model_dump(by_alias=True) for x in queries]}, # type: ignore[attr-defined]
body={
"federation": federation.model_dump(by_alias=True) if federation else None,
"queries": processed_queries,
},
)

if federation:
results = response.json()
return SearchResultsFederated[hits_type](**results)

return [SearchResultsWithUID[hits_type](**x) for x in response.json()["results"]]

def get_raw_index(self, uid: str) -> IndexInfo | None:
Expand Down
Loading

0 comments on commit 8ce70a4

Please sign in to comment.