Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support user dictionary loading #870

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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()
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Paths:
dumps = "dumps"
pagination = "pagination"
faceting = "faceting"
dictionary = "dictionary"
swap = "swap-indexes"

def __init__(
Expand Down
60 changes: 60 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions tests/settings/test_settings_dictionary_meilisearch.py
Original file line number Diff line number Diff line change
@@ -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 == []