Skip to content

Support localized-attributes settings #1060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
10 changes: 10 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -732,3 +732,13 @@ reset_proximity_precision_settings_1: |-
client.index('books').reset_proximity_precision()
search_parameter_reference_ranking_score_threshold_1: |-
client.index('INDEX_NAME').search('badman', { 'rankingScoreThreshold': 0.2 })
search_parameter_reference_locales_1: |-
client.index('INDEX_NAME').search('進撃の巨人', { 'locales': ['jpn'] })
get_localized_attribute_settings_1: |-
client.index('INDEX_NAME').get_localized_attributes()
update_localized_attribute_settings_1: |-
client.index('INDEX_NAME').update_localized_attributes([
{'attribute_patterns': ['*_ja'], 'locales': ['jpn']}
])
reset_localized_attribute_settings_1: |-
client.index('INDEX_NAME').reset_localized_attributes()
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Paths:
embedders = "embedders"
search_cutoff_ms = "search-cutoff-ms"
proximity_precision = "proximity-precision"
localized_attributes = "localized-attributes"

def __init__(
self,
Expand Down
68 changes: 68 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
Faceting,
HuggingFaceEmbedder,
IndexStats,
LocalizedAttributes,
OpenAiEmbedder,
Pagination,
ProximityPrecision,
Expand Down Expand Up @@ -2042,6 +2043,73 @@ def reset_proximity_precision(self) -> TaskInfo:

return TaskInfo(**task)

# LOCALIZED ATTRIBUTES SETTINGS

def get_localized_attributes(self) -> Union[List[LocalizedAttributes], None]:
"""Get the localized_attributes of the index.

Returns
-------
settings:
localized_attributes of the index.

Raises
------
MeilisearchApiError
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
"""
response = self.http.get(self.__settings_url_for(self.config.paths.localized_attributes))

if not response:
return None

return [LocalizedAttributes(**attrs) for attrs in response]

def update_localized_attributes(
self, body: Union[List[Mapping[str, List[str]]], None]
) -> TaskInfo:
"""Update the localized_attributes of the index.

Parameters
----------
body:
localized_attributes

Returns
-------
task_info:
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task

Raises
------
MeilisearchApiError
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
"""
task = self.http.put(self.__settings_url_for(self.config.paths.localized_attributes), body)

return TaskInfo(**task)

def reset_localized_attributes(self) -> TaskInfo:
"""Reset the localized_attributes of the index

Returns
-------
task_info:
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task

Raises
------
MeilisearchApiError
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
"""
task = self.http.delete(
self.__settings_url_for(self.config.paths.localized_attributes),
)

return TaskInfo(**task)

@staticmethod
def _batch(
documents: Sequence[Mapping[str, Any]], batch_size: int
Expand Down
5 changes: 5 additions & 0 deletions meilisearch/models/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ class ProximityPrecision(str, Enum):
BY_ATTRIBUTE = "byAttribute"


class LocalizedAttributes(CamelBase):
attribute_patterns: List[str]
locales: List[str]


class OpenAiEmbedder(CamelBase):
source: str = "openAi"
model: Optional[str] = None # Defaults to text-embedding-3-small
Expand Down
43 changes: 43 additions & 0 deletions tests/settings/test_settings_localized_attributes_meilisearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from typing import List

from meilisearch.models.index import LocalizedAttributes

NEW_LOCALIZED_ATTRIBUTES = [{"attributePatterns": ["title"], "locales": ["eng"]}]


def unpack_loc_attrs_response(response: List[LocalizedAttributes]):
return [loc_attrs.model_dump(by_alias=True) for loc_attrs in response]


def test_get_localized_attributes(empty_index):
"""Tests getting default localized_attributes."""
response = empty_index().get_localized_attributes()
assert response is None


def test_update_localized_attributes(empty_index):
"""Tests updating proximity precision."""
index = empty_index()
response = index.update_localized_attributes(NEW_LOCALIZED_ATTRIBUTES)
update = index.wait_for_task(response.task_uid)
assert update.status == "succeeded"
response = index.get_localized_attributes()
assert NEW_LOCALIZED_ATTRIBUTES == unpack_loc_attrs_response(response)


def test_reset_localized_attributes(empty_index):
"""Tests resetting the proximity precision to its default value."""
index = empty_index()
# Update the settings first
response = index.update_localized_attributes(NEW_LOCALIZED_ATTRIBUTES)
update = index.wait_for_task(response.task_uid)
assert update.status == "succeeded"
# Check the settings have been correctly updated
response = index.get_localized_attributes()
assert NEW_LOCALIZED_ATTRIBUTES == unpack_loc_attrs_response(response)
# Check the reset of the settings
response = index.reset_localized_attributes()
update = index.wait_for_task(response.task_uid)
assert update.status == "succeeded"
response = index.get_localized_attributes()
assert response is None
Loading