Skip to content

Commit

Permalink
Merge #971
Browse files Browse the repository at this point in the history
971: Add proximityPrecision setting r=sanders41 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 <shalabhagarwal1024@gmail.com>
  • Loading branch information
meili-bors[bot] and the-sinner committed Jun 5, 2024
2 parents 1b76f36 + 9c1d189 commit 63e139b
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,9 @@ update_search_cutoff_1: |-
client.index('movies').update_search_cutoff_ms(150)
reset_search_cutoff_1: |-
client.index('movies').reset_search_cutoff_ms()
get_proximity_precision_settings_1: |-
client.index('books').get_proximity_precision()
update_proximity_precision_settings_1: |-
client.index('books').update_proximity_precision(ProximityPrecision.BY_ATTRIBUTE)
reset_proximity_precision_settings_1: |-
client.index('books').reset_proximity_precision()
19 changes: 17 additions & 2 deletions meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
MeilisearchCommunicationError,
MeilisearchTimeoutError,
)
from meilisearch.models.index import ProximityPrecision
from meilisearch.version import qualified_version


Expand All @@ -28,7 +29,14 @@ def send_request(
http_method: Callable,
path: str,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str, int]
Union[
Mapping[str, Any],
Sequence[Mapping[str, Any]],
List[str],
str,
int,
ProximityPrecision,
]
] = None,
content_type: Optional[str] = None,
) -> Any:
Expand Down Expand Up @@ -90,7 +98,14 @@ def put(
self,
path: str,
body: Optional[
Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str, int]
Union[
Mapping[str, Any],
Sequence[Mapping[str, Any]],
List[str],
str,
int,
ProximityPrecision,
]
] = None,
content_type: Optional[str] = "application/json",
) -> Any:
Expand Down
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Paths:
swap = "swap-indexes"
embedders = "embedders"
search_cutoff_ms = "search-cutoff-ms"
proximity_precision = "proximity-precision"

def __init__(
self,
Expand Down
66 changes: 65 additions & 1 deletion meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from urllib import parse
from warnings import warn

from camel_converter import to_snake

from meilisearch._httprequests import HttpRequests
from meilisearch._utils import iso_to_date_time
from meilisearch.config import Config
Expand All @@ -17,14 +19,15 @@
IndexStats,
OpenAiEmbedder,
Pagination,
ProximityPrecision,
TypoTolerance,
UserProvidedEmbedder,
)
from meilisearch.models.task import Task, TaskInfo, TaskResults
from meilisearch.task import TaskHandler


# pylint: disable=too-many-public-methods
# pylint: disable=too-many-public-methods, too-many-lines
class Index:
"""
Indexes routes wrapper.
Expand Down Expand Up @@ -1917,6 +1920,67 @@ def reset_search_cutoff_ms(self) -> TaskInfo:

return TaskInfo(**task)

# PROXIMITY PRECISION SETTINGS

def get_proximity_precision(self) -> ProximityPrecision:
"""Get the proximity_precision of the index.
Returns
-------
settings:
proximity_precision 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.proximity_precision))
return ProximityPrecision[to_snake(response).upper()]

def update_proximity_precision(self, body: Union[ProximityPrecision, None]) -> TaskInfo:
"""Update the proximity_precision of the index.
Parameters
----------
body:
proximity_precision
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.proximity_precision), body)

return TaskInfo(**task)

def reset_proximity_precision(self) -> TaskInfo:
"""Reset the proximity_precision 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.proximity_precision),
)

return TaskInfo(**task)

@staticmethod
def _batch(
documents: Sequence[Mapping[str, Any]], batch_size: int
Expand Down
6 changes: 6 additions & 0 deletions meilisearch/models/index.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

from enum import Enum
from typing import Any, Dict, Iterator, List, Optional, Union

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


class ProximityPrecision(str, Enum):
BY_WORD = "byWord"
BY_ATTRIBUTE = "byAttribute"


class OpenAiEmbedder(CamelBase):
source: str = "openAi"
model: Optional[str] = None # Defaults to text-embedding-ada-002
Expand Down
37 changes: 37 additions & 0 deletions tests/settings/test_settings_proximity_precision_meilisearch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from meilisearch.models.index import ProximityPrecision

NEW_PROXIMITY_PRECISION = ProximityPrecision.BY_ATTRIBUTE


def test_get_proximity_precision(empty_index):
"""Tests getting default proximity precision."""
response = empty_index().get_proximity_precision()
assert response == ProximityPrecision.BY_WORD


def test_update_proximity_precision(empty_index):
"""Tests updating proximity precision."""
index = empty_index()
response = index.update_proximity_precision(NEW_PROXIMITY_PRECISION)
update = index.wait_for_task(response.task_uid)
assert update.status == "succeeded"
response = index.get_proximity_precision()
assert NEW_PROXIMITY_PRECISION == response


def test_reset_proximity_precision(empty_index):
"""Tests resetting the proximity precision to its default value."""
index = empty_index()
# Update the settings first
response = index.update_proximity_precision(NEW_PROXIMITY_PRECISION)
update = index.wait_for_task(response.task_uid)
assert update.status == "succeeded"
# Check the settings have been correctly updated
response = index.get_proximity_precision()
assert NEW_PROXIMITY_PRECISION == response
# Check the reset of the settings
response = index.reset_proximity_precision()
update = index.wait_for_task(response.task_uid)
assert update.status == "succeeded"
response = index.get_proximity_precision()
assert response == ProximityPrecision.BY_WORD

0 comments on commit 63e139b

Please sign in to comment.