Skip to content

Commit d273389

Browse files
Merge #971
971: Add proximityPrecision setting r=curquiza a=the-sinner # Pull Request ## Related issue Fixes #916 ## What does this PR do? - Add proximityPrecision setting ## 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? Thank you so much for contributing to Meilisearch! Co-authored-by: Shalabh Agarwal <[email protected]>
2 parents 1b76f36 + 9c1d189 commit d273389

File tree

6 files changed

+132
-3
lines changed

6 files changed

+132
-3
lines changed

.code-samples.meilisearch.yaml

+6
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

+17-2
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

+1
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

+65-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from urllib import parse
66
from warnings import warn
77

8+
from camel_converter import to_snake
9+
810
from meilisearch._httprequests import HttpRequests
911
from meilisearch._utils import iso_to_date_time
1012
from meilisearch.config import Config
@@ -17,14 +19,15 @@
1719
IndexStats,
1820
OpenAiEmbedder,
1921
Pagination,
22+
ProximityPrecision,
2023
TypoTolerance,
2124
UserProvidedEmbedder,
2225
)
2326
from meilisearch.models.task import Task, TaskInfo, TaskResults
2427
from meilisearch.task import TaskHandler
2528

2629

27-
# pylint: disable=too-many-public-methods
30+
# pylint: disable=too-many-public-methods, too-many-lines
2831
class Index:
2932
"""
3033
Indexes routes wrapper.
@@ -1917,6 +1920,67 @@ def reset_search_cutoff_ms(self) -> TaskInfo:
19171920

19181921
return TaskInfo(**task)
19191922

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

meilisearch/models/index.py

+6
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(str, 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
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
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 == 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 == 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

0 commit comments

Comments
 (0)