diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 346c78da..57abbda6 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -683,3 +683,9 @@ facet_search_2: |- client.index('books').update_faceting_settings({ 'sortFacetValuesBy': { 'genres': 'count' } }) facet_search_3: |- client.index('books').facet_search('genres', 'c') +get_dictionary_1: |- + client.index('books').get_dictionary() +update_dictionary_1: |- + client.index('books').update_dictionary(["J. R. R.", "W. E. B."]) +reset_dictionary_1: |- + client.index('books').reset_dictionary() diff --git a/meilisearch/config.py b/meilisearch/config.py index b1125db5..fdc7265b 100644 --- a/meilisearch/config.py +++ b/meilisearch/config.py @@ -33,6 +33,7 @@ class Paths: dumps = "dumps" pagination = "pagination" faceting = "faceting" + dictionary = "dictionary" swap = "swap-indexes" def __init__( diff --git a/meilisearch/index.py b/meilisearch/index.py index 67a56f20..44fd061d 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -1577,6 +1577,66 @@ def reset_faceting_settings(self) -> TaskInfo: return TaskInfo(**task) + # USER DICTIONARY SUB-ROUTES + + def get_dictionary(self) -> List[str]: + """Get the dictionary entries of the index. + + Returns + ------- + settings: + List containing the dictionary entries 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.dictionary)) + + def update_dictionary(self, body: List[str]) -> TaskInfo: + """Update the dictionary of the index. + + Parameters + ---------- + body: + List of the new dictionary entries. + + 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.dictionary), body) + + return TaskInfo(**task) + + def reset_dictionary(self) -> TaskInfo: + """Clear all entries in dictionary + + 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.dictionary), + ) + + return TaskInfo(**task) + @staticmethod def _batch( documents: List[Dict[str, Any]], batch_size: int diff --git a/tests/settings/test_settings_dictionary_meilisearch.py b/tests/settings/test_settings_dictionary_meilisearch.py new file mode 100644 index 00000000..87a2f616 --- /dev/null +++ b/tests/settings/test_settings_dictionary_meilisearch.py @@ -0,0 +1,38 @@ +NEW_DICTIONARY = ["J. R. R. Tolkien", "W. E. B. Du Bois"] + + +def test_get_dictionary_default(empty_index): + """Tests getting the default value of user dictionary.""" + dictionary = empty_index().get_dictionary() + assert dictionary == [] + + +def test_update_dictionary(empty_index): + """Tests updating the user dictionary.""" + index = empty_index() + task = index.update_dictionary(NEW_DICTIONARY) + task = index.wait_for_task(task.task_uid) + assert task.status == "succeeded" + + dictionary = index.get_dictionary() + for word in NEW_DICTIONARY: + assert word in dictionary + + +def test_reset_dictionary(empty_index): + """Tests resetting the user dictionary to its default empty list.""" + index = empty_index() + task = index.update_dictionary(NEW_DICTIONARY) + task = index.wait_for_task(task.task_uid) + assert task.status == "succeeded" + + dictionary = index.get_dictionary() + for word in NEW_DICTIONARY: + assert word in dictionary + + reset_task = index.reset_dictionary() + reset_task = index.wait_for_task(reset_task.task_uid) + assert reset_task.status == "succeeded" + + dictionary = index.get_dictionary() + assert dictionary == []