Skip to content

Commit d0501ae

Browse files
Merge #1060
1060: Support localized-attributes settings r=sanders41 a=ellnix # Pull Request ## Related issue Fixes #1011 ## PR checklist Please check if your PR fulfills the following requirements: - [X] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [X] Have you read the contributing guidelines? - [X] Have you made sure that the title is accurate and descriptive of the changes? Not very experienced with python, let me know if I should have created a model for the localized attributes, similar to embedders or proximity precision. Regarding #1011, I don't think anything needs to change in the search parameters, since those seem to be dynamic anyway. Co-authored-by: ellnix <ellnix@disroot.org>
2 parents 5a33fab + 305b32b commit d0501ae

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed

.code-samples.meilisearch.yaml

+10
Original file line numberDiff line numberDiff line change
@@ -732,3 +732,13 @@ reset_proximity_precision_settings_1: |-
732732
client.index('books').reset_proximity_precision()
733733
search_parameter_reference_ranking_score_threshold_1: |-
734734
client.index('INDEX_NAME').search('badman', { 'rankingScoreThreshold': 0.2 })
735+
search_parameter_reference_locales_1: |-
736+
client.index('INDEX_NAME').search('進撃の巨人', { 'locales': ['jpn'] })
737+
get_localized_attribute_settings_1: |-
738+
client.index('INDEX_NAME').get_localized_attributes()
739+
update_localized_attribute_settings_1: |-
740+
client.index('INDEX_NAME').update_localized_attributes([
741+
{'attribute_patterns': ['*_ja'], 'locales': ['jpn']}
742+
])
743+
reset_localized_attribute_settings_1: |-
744+
client.index('INDEX_NAME').reset_localized_attributes()

meilisearch/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class Paths:
4141
embedders = "embedders"
4242
search_cutoff_ms = "search-cutoff-ms"
4343
proximity_precision = "proximity-precision"
44+
localized_attributes = "localized-attributes"
4445

4546
def __init__(
4647
self,

meilisearch/index.py

+68
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
Faceting,
3030
HuggingFaceEmbedder,
3131
IndexStats,
32+
LocalizedAttributes,
3233
OpenAiEmbedder,
3334
Pagination,
3435
ProximityPrecision,
@@ -2042,6 +2043,73 @@ def reset_proximity_precision(self) -> TaskInfo:
20422043

20432044
return TaskInfo(**task)
20442045

2046+
# LOCALIZED ATTRIBUTES SETTINGS
2047+
2048+
def get_localized_attributes(self) -> Union[List[LocalizedAttributes], None]:
2049+
"""Get the localized_attributes of the index.
2050+
2051+
Returns
2052+
-------
2053+
settings:
2054+
localized_attributes of the index.
2055+
2056+
Raises
2057+
------
2058+
MeilisearchApiError
2059+
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
2060+
"""
2061+
response = self.http.get(self.__settings_url_for(self.config.paths.localized_attributes))
2062+
2063+
if not response:
2064+
return None
2065+
2066+
return [LocalizedAttributes(**attrs) for attrs in response]
2067+
2068+
def update_localized_attributes(
2069+
self, body: Union[List[Mapping[str, List[str]]], None]
2070+
) -> TaskInfo:
2071+
"""Update the localized_attributes of the index.
2072+
2073+
Parameters
2074+
----------
2075+
body:
2076+
localized_attributes
2077+
2078+
Returns
2079+
-------
2080+
task_info:
2081+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
2082+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
2083+
2084+
Raises
2085+
------
2086+
MeilisearchApiError
2087+
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
2088+
"""
2089+
task = self.http.put(self.__settings_url_for(self.config.paths.localized_attributes), body)
2090+
2091+
return TaskInfo(**task)
2092+
2093+
def reset_localized_attributes(self) -> TaskInfo:
2094+
"""Reset the localized_attributes of the index
2095+
2096+
Returns
2097+
-------
2098+
task_info:
2099+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
2100+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
2101+
2102+
Raises
2103+
------
2104+
MeilisearchApiError
2105+
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
2106+
"""
2107+
task = self.http.delete(
2108+
self.__settings_url_for(self.config.paths.localized_attributes),
2109+
)
2110+
2111+
return TaskInfo(**task)
2112+
20452113
@staticmethod
20462114
def _batch(
20472115
documents: Sequence[Mapping[str, Any]], batch_size: int

meilisearch/models/index.py

+5
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ class ProximityPrecision(str, Enum):
5454
BY_ATTRIBUTE = "byAttribute"
5555

5656

57+
class LocalizedAttributes(CamelBase):
58+
attribute_patterns: List[str]
59+
locales: List[str]
60+
61+
5762
class OpenAiEmbedder(CamelBase):
5863
source: str = "openAi"
5964
model: Optional[str] = None # Defaults to text-embedding-3-small
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from typing import List
2+
3+
from meilisearch.models.index import LocalizedAttributes
4+
5+
NEW_LOCALIZED_ATTRIBUTES = [{"attributePatterns": ["title"], "locales": ["eng"]}]
6+
7+
8+
def unpack_loc_attrs_response(response: List[LocalizedAttributes]):
9+
return [loc_attrs.model_dump(by_alias=True) for loc_attrs in response]
10+
11+
12+
def test_get_localized_attributes(empty_index):
13+
"""Tests getting default localized_attributes."""
14+
response = empty_index().get_localized_attributes()
15+
assert response is None
16+
17+
18+
def test_update_localized_attributes(empty_index):
19+
"""Tests updating proximity precision."""
20+
index = empty_index()
21+
response = index.update_localized_attributes(NEW_LOCALIZED_ATTRIBUTES)
22+
update = index.wait_for_task(response.task_uid)
23+
assert update.status == "succeeded"
24+
response = index.get_localized_attributes()
25+
assert NEW_LOCALIZED_ATTRIBUTES == unpack_loc_attrs_response(response)
26+
27+
28+
def test_reset_localized_attributes(empty_index):
29+
"""Tests resetting the proximity precision to its default value."""
30+
index = empty_index()
31+
# Update the settings first
32+
response = index.update_localized_attributes(NEW_LOCALIZED_ATTRIBUTES)
33+
update = index.wait_for_task(response.task_uid)
34+
assert update.status == "succeeded"
35+
# Check the settings have been correctly updated
36+
response = index.get_localized_attributes()
37+
assert NEW_LOCALIZED_ATTRIBUTES == unpack_loc_attrs_response(response)
38+
# Check the reset of the settings
39+
response = index.reset_localized_attributes()
40+
update = index.wait_for_task(response.task_uid)
41+
assert update.status == "succeeded"
42+
response = index.get_localized_attributes()
43+
assert response is None

0 commit comments

Comments
 (0)