diff --git a/meilisearch/config.py b/meilisearch/config.py index b498426f..078a3396 100644 --- a/meilisearch/config.py +++ b/meilisearch/config.py @@ -28,6 +28,7 @@ class Paths(): typo_tolerance = 'typo-tolerance' dumps = 'dumps' pagination = 'pagination' + faceting = 'faceting' def __init__( self, diff --git a/meilisearch/index.py b/meilisearch/index.py index 057e09d0..309ec40a 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -1288,6 +1288,65 @@ def reset_pagination_settings(self) -> Dict[str, Any]: """ return self.http.delete(self.__settings_url_for(self.config.paths.pagination)) + def get_faceting_settings(self) -> Dict[str, Any]: + """Get the faceting settings of an index. + + Returns + ------- + settings: dict + Dictionary containing the faceting settings of the index. + + Raises + ------ + MeiliSearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors + """ + + return self.http.get(self.__settings_url_for(self.config.paths.faceting)) + + def update_faceting_settings(self, body: Dict[str, Any]) -> Dict[str, Any]: + """Update the faceting settings of the index. + + Parameters + ---------- + body: dict + Dictionary containing the faceting settings. + https://docs.meilisearch.com/reference/api/faceting.html#update-faceting-settings + + Returns + ------- + task: + Dictionary containing a task to track the informations about the progress of an asynchronous process. + https://docs.meilisearch.com/reference/api/tasks.html#get-one-task + + Raises + ------ + MeiliSearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors + """ + return self.http.patch( + path=self.__settings_url_for(self.config.paths.faceting), + body=body + ) + + + def reset_faceting_settings(self) -> Dict[str, Any]: + """Reset faceting settings of the index to default values. + + Returns + ------- + task: + Dictionary containing a task to track the informations about the progress of an asynchronous process. + https://docs.meilisearch.com/reference/api/tasks.html#get-one-task + + Raises + ------ + MeiliSearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors + """ + return self.http.delete(self.__settings_url_for(self.config.paths.faceting)) + + @staticmethod def _batch( documents: List[Dict[str, Any]], batch_size: int diff --git a/tests/settings/test_setting_faceting.py b/tests/settings/test_setting_faceting.py new file mode 100644 index 00000000..1c8b477a --- /dev/null +++ b/tests/settings/test_setting_faceting.py @@ -0,0 +1,31 @@ +DEFAULT_MAX_VALUE_PER_FACET = 100 +NEW_MAX_VALUE_PER_FACET = {'maxValuesPerFacet': 200 } + + +def test_get_faceting_settings(empty_index): + response = empty_index().get_faceting_settings() + + assert isinstance(response, dict) + assert { 'maxValuesPerFacet': DEFAULT_MAX_VALUE_PER_FACET } == response + + +def test_update_faceting_settings(empty_index): + index = empty_index() + response = index.update_faceting_settings(NEW_MAX_VALUE_PER_FACET) + assert isinstance(response, dict) + assert 'taskUid' in response + + index.wait_for_task(response['taskUid']) + response = index.get_faceting_settings() + assert isinstance(response, dict) + assert NEW_MAX_VALUE_PER_FACET == response + + +def test_delete_faceting_settings(empty_index): + index = empty_index() + response = index.reset_faceting_settings() + + index.wait_for_task(response['taskUid']) + response = index.get_faceting_settings() + assert isinstance(response, dict) + assert { 'maxValuesPerFacet': DEFAULT_MAX_VALUE_PER_FACET } == response