Skip to content

Commit cfe2af2

Browse files
committed
Add proximityPrecision setting
1 parent 1b76f36 commit cfe2af2

File tree

6 files changed

+130
-2
lines changed

6 files changed

+130
-2
lines changed

.code-samples.meilisearch.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,9 @@ update_search_cutoff_1: |-
714714
client.index('movies').update_search_cutoff_ms(150)
715715
reset_search_cutoff_1: |-
716716
client.index('movies').reset_search_cutoff_ms()
717+
get_proximity_precision_settings_1: |-
718+
client.index('books').get_proximity_precision()
719+
update_proximity_precision_settings_1: |-
720+
client.index('books').update_proximity_precision(ProximityPrecision.BY_ATTRIBUTE)
721+
reset_proximity_precision_settings_1: |-
722+
client.index('books').reset_proximity_precision()

meilisearch/_httprequests.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
MeilisearchCommunicationError,
1313
MeilisearchTimeoutError,
1414
)
15+
from meilisearch.models.index import ProximityPrecision
1516
from meilisearch.version import qualified_version
1617

1718

@@ -28,7 +29,14 @@ def send_request(
2829
http_method: Callable,
2930
path: str,
3031
body: Optional[
31-
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str, int]
32+
Union[
33+
Mapping[str, Any],
34+
Sequence[Mapping[str, Any]],
35+
List[str],
36+
str,
37+
int,
38+
ProximityPrecision,
39+
]
3240
] = None,
3341
content_type: Optional[str] = None,
3442
) -> Any:
@@ -90,7 +98,14 @@ def put(
9098
self,
9199
path: str,
92100
body: Optional[
93-
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str, int]
101+
Union[
102+
Mapping[str, Any],
103+
Sequence[Mapping[str, Any]],
104+
List[str],
105+
str,
106+
int,
107+
ProximityPrecision,
108+
]
94109
] = None,
95110
content_type: Optional[str] = "application/json",
96111
) -> Any:

meilisearch/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class Paths:
4040
swap = "swap-indexes"
4141
embedders = "embedders"
4242
search_cutoff_ms = "search-cutoff-ms"
43+
proximity_precision = "proximity-precision"
4344

4445
def __init__(
4546
self,

meilisearch/index.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
IndexStats,
1818
OpenAiEmbedder,
1919
Pagination,
20+
ProximityPrecision,
2021
TypoTolerance,
2122
UserProvidedEmbedder,
2223
)
@@ -1917,6 +1918,68 @@ def reset_search_cutoff_ms(self) -> TaskInfo:
19171918

19181919
return TaskInfo(**task)
19191920

1921+
# PROXIMITY PRECISION SETTINGS
1922+
1923+
def get_proximity_precision(self) -> ProximityPrecision | None:
1924+
"""Get the proximity_precision of the index.
1925+
1926+
Returns
1927+
-------
1928+
settings:
1929+
proximity_precision of the index.
1930+
1931+
Raises
1932+
------
1933+
MeilisearchApiError
1934+
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
1935+
"""
1936+
return self.http.get(self.__settings_url_for(self.config.paths.proximity_precision))
1937+
1938+
def update_proximity_precision(self, body: ProximityPrecision) -> TaskInfo:
1939+
"""Update the proximity_precision of the index.
1940+
1941+
Parameters
1942+
----------
1943+
body:
1944+
proximity_precision
1945+
1946+
Returns
1947+
-------
1948+
task_info:
1949+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
1950+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
1951+
1952+
Raises
1953+
------
1954+
MeilisearchApiError
1955+
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
1956+
"""
1957+
task = self.http.put(
1958+
self.__settings_url_for(self.config.paths.proximity_precision), body.value
1959+
)
1960+
1961+
return TaskInfo(**task)
1962+
1963+
def reset_proximity_precision(self) -> TaskInfo:
1964+
"""Reset the proximity_precision of the index
1965+
1966+
Returns
1967+
-------
1968+
task_info:
1969+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
1970+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
1971+
1972+
Raises
1973+
------
1974+
MeilisearchApiError
1975+
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
1976+
"""
1977+
task = self.http.delete(
1978+
self.__settings_url_for(self.config.paths.proximity_precision),
1979+
)
1980+
1981+
return TaskInfo(**task)
1982+
19201983
@staticmethod
19211984
def _batch(
19221985
documents: Sequence[Mapping[str, Any]], batch_size: int

meilisearch/models/index.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
from enum import Enum
34
from typing import Any, Dict, Iterator, List, Optional, Union
45

56
from camel_converter import to_snake
@@ -48,6 +49,11 @@ class TypoTolerance(CamelBase):
4849
min_word_size_for_typos: Optional[MinWordSizeForTypos] = None
4950

5051

52+
class ProximityPrecision(Enum):
53+
BY_WORD = "byWord"
54+
BY_ATTRIBUTE = "byAttribute"
55+
56+
5157
class OpenAiEmbedder(CamelBase):
5258
source: str = "openAi"
5359
model: Optional[str] = None # Defaults to text-embedding-ada-002
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from meilisearch.models.index import ProximityPrecision
2+
3+
NEW_PROXIMITY_PRECISION = ProximityPrecision.BY_ATTRIBUTE
4+
5+
6+
def test_get_proximity_precision(empty_index):
7+
"""Tests getting default proximity precision."""
8+
response = empty_index().get_proximity_precision()
9+
assert response == ProximityPrecision.BY_WORD.value
10+
11+
12+
def test_update_proximity_precision(empty_index):
13+
"""Tests updating proximity precision."""
14+
index = empty_index()
15+
response = index.update_proximity_precision(NEW_PROXIMITY_PRECISION)
16+
update = index.wait_for_task(response.task_uid)
17+
assert update.status == "succeeded"
18+
response = index.get_proximity_precision()
19+
assert NEW_PROXIMITY_PRECISION.value == response
20+
21+
22+
def test_reset_proximity_precision(empty_index):
23+
"""Tests resetting the proximity precision to its default value."""
24+
index = empty_index()
25+
# Update the settings first
26+
response = index.update_proximity_precision(NEW_PROXIMITY_PRECISION)
27+
update = index.wait_for_task(response.task_uid)
28+
assert update.status == "succeeded"
29+
# Check the settings have been correctly updated
30+
response = index.get_proximity_precision()
31+
assert NEW_PROXIMITY_PRECISION.value == response
32+
# Check the reset of the settings
33+
response = index.reset_proximity_precision()
34+
update = index.wait_for_task(response.task_uid)
35+
assert update.status == "succeeded"
36+
response = index.get_proximity_precision()
37+
assert response == ProximityPrecision.BY_WORD.value

0 commit comments

Comments
 (0)