Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,12 @@ def get_stats(self) -> IndexStats:
return IndexStats(**stats)

@version_error_hint_message
def search(self, query: str, opt_params: Optional[Mapping[str, Any]] = None) -> Dict[str, Any]:
def search(
self,
query: str,
opt_params: Optional[Mapping[str, Any]] = None,
personalize: Optional[Dict[str, Any]] = None,
) -> Dict[str, Any]:
"""Search in the index.

https://www.meilisearch.com/docs/reference/api/search
Expand All @@ -353,6 +358,9 @@ def search(self, query: str, opt_params: Optional[Mapping[str, Any]] = None) ->
- offset: Number of documents to skip
- showPerformanceDetails: If true, the response includes a
performanceDetails object (raw data; fields may change in future API versions)
personalize (optional):
Dict with a 'userContext' string to personalize the search results
(experimental; requires Meilisearch >= v1.47).

Returns
-------
Expand All @@ -369,6 +377,8 @@ def search(self, query: str, opt_params: Optional[Mapping[str, Any]] = None) ->
opt_params = {}

body = {"q": query, **opt_params}
if personalize is not None:
body["personalize"] = personalize

return self.http.post(
f"{self.config.paths.index}/{self.uid}/{self.config.paths.search}",
Expand Down
12 changes: 12 additions & 0 deletions tests/index/test_index_search_meilisearch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=invalid-name

from collections import Counter
from unittest.mock import patch

import pytest

Expand All @@ -14,6 +15,17 @@ def test_basic_search(index_with_documents):
assert "hitsPerPage" is not response


def test_search_serializes_personalize(client):
"""The `personalize` parameter must be forwarded in the search request body."""
index = client.index("books")
with patch.object(index.http, "post", return_value={"hits": []}) as mock_post:
index.search("prince", personalize={"userContext": "user-123"})

_, kwargs = mock_post.call_args
assert kwargs["body"]["q"] == "prince"
assert kwargs["body"]["personalize"] == {"userContext": "user-123"}


def test_basic_search_with_empty_params(index_with_documents):
"""Tests search with a simple query and empty params."""
response = index_with_documents().search("How to Train Your Dragon", {})
Expand Down