From fa0a8efeb4f72210b5b454adf6e62a6725893953 Mon Sep 17 00:00:00 2001 From: Shalabh Agarwal Date: Tue, 28 May 2024 23:07:31 +0530 Subject: [PATCH] Added search cutoff ms settings --- .code-samples.meilisearch.yaml | 9 ++- meilisearch/_httprequests.py | 4 +- meilisearch/config.py | 1 + meilisearch/index.py | 60 +++++++++++++++++++ ...test_settings_search_cutoff_meilisearch.py | 35 +++++++++++ 5 files changed, 106 insertions(+), 3 deletions(-) create mode 100644 tests/settings/test_settings_search_cutoff_meilisearch.py diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index f3656ff5..c0277345 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -139,7 +139,8 @@ update_settings_1: |- }, 'faceting': { 'maxValuesPerFacet': 200 - } + }, + 'searchCutoffMs': 150 }) reset_settings_1: |- client.index('movies').reset_settings() @@ -703,3 +704,9 @@ reset_dictionary_1: |- client.index('books').reset_dictionary() create_snapshot_1: |- client.create_snapshot() +get_search_cutoff_1: |- + client.index('movies').get_search_cutoff_ms() +update_search_cutoff_1: |- + client.index('movies').update_search_cutoff_ms(150) +reset_search_cutoff_1: |- + client.index('movies').reset_search_cutoff_ms() diff --git a/meilisearch/_httprequests.py b/meilisearch/_httprequests.py index 5876540e..1af97ad3 100644 --- a/meilisearch/_httprequests.py +++ b/meilisearch/_httprequests.py @@ -28,7 +28,7 @@ def send_request( http_method: Callable, path: str, body: Optional[ - Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str] + Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str, int] ] = None, content_type: Optional[str] = None, ) -> Any: @@ -90,7 +90,7 @@ def put( self, path: str, body: Optional[ - Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str] + Union[Mapping[str, Any], Sequence[Mapping[str, Any]], List[str], str, int] ] = None, content_type: Optional[str] = "application/json", ) -> Any: diff --git a/meilisearch/config.py b/meilisearch/config.py index 09cc5fad..3677f948 100644 --- a/meilisearch/config.py +++ b/meilisearch/config.py @@ -39,6 +39,7 @@ class Paths: non_separator_tokens = "non-separator-tokens" swap = "swap-indexes" embedders = "embedders" + search_cutoff_ms = "search-cutoff-ms" def __init__( self, diff --git a/meilisearch/index.py b/meilisearch/index.py index ac75e14c..f7bbce4b 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -1856,6 +1856,66 @@ def reset_embedders(self) -> TaskInfo: ) return TaskInfo(**task) + + # SEARCH CUTOFF MS SETTINGS + + def get_search_cutoff_ms(self) -> int | None: + """Get the search cutoff in ms of the index. + + Returns + ------- + settings: + Integer value of search cutoff in ms 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 + """ + return self.http.get(self.__settings_url_for(self.config.paths.search_cutoff_ms)) + + def update_search_cutoff_ms(self, body: Union[int, None]) -> TaskInfo: + """Update the search cutoff in ms of the index. + + Parameters + ---------- + body: + Integer value of the search cutoff time in ms. + + 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.search_cutoff_ms), body) + + return TaskInfo(**task) + + def reset_search_cutoff_ms(self) -> TaskInfo: + """Reset the search cutoff 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.search_cutoff_ms), + ) + + return TaskInfo(**task) @staticmethod def _batch( diff --git a/tests/settings/test_settings_search_cutoff_meilisearch.py b/tests/settings/test_settings_search_cutoff_meilisearch.py new file mode 100644 index 00000000..876d384d --- /dev/null +++ b/tests/settings/test_settings_search_cutoff_meilisearch.py @@ -0,0 +1,35 @@ +NEW_SEARCH_CUTOFF_MS = 150 + + +def test_get_search_cutoff_ms(empty_index): + """Tests getting default search cutoff in ms.""" + response = empty_index().get_search_cutoff_ms() + assert response is None + + +def test_update_search_cutoff_ms(empty_index): + """Tests updating search cutoff in ms.""" + index = empty_index() + response = index.update_search_cutoff_ms(NEW_SEARCH_CUTOFF_MS) + update = index.wait_for_task(response.task_uid) + assert update.status == "succeeded" + response = index.get_search_cutoff_ms() + assert NEW_SEARCH_CUTOFF_MS == response + + +def test_reset_search_cutoff_ms(empty_index): + """Tests resetting the search cutoff to its default value.""" + index = empty_index() + # Update the settings first + response = index.update_search_cutoff_ms(NEW_SEARCH_CUTOFF_MS) + update = index.wait_for_task(response.task_uid) + assert update.status == "succeeded" + # Check the settings have been correctly updated + response = index.get_search_cutoff_ms() + assert NEW_SEARCH_CUTOFF_MS == response + # Check the reset of the settings + response = index.reset_search_cutoff_ms() + update = index.wait_for_task(response.task_uid) + assert update.status == "succeeded" + response = index.get_search_cutoff_ms() + assert response is None