Skip to content

Commit 8d8b4ad

Browse files
Merge #870
870: Support user dictionary loading r=sanders41 a=ellnix # Pull Request ## Related issue Fixes #853 ## 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? Co-authored-by: ellnix <103502144+ellnix@users.noreply.github.com>
2 parents 844f540 + 52d352d commit 8d8b4ad

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,3 +683,9 @@ facet_search_2: |-
683683
client.index('books').update_faceting_settings({ 'sortFacetValuesBy': { 'genres': 'count' } })
684684
facet_search_3: |-
685685
client.index('books').facet_search('genres', 'c')
686+
get_dictionary_1: |-
687+
client.index('books').get_dictionary()
688+
update_dictionary_1: |-
689+
client.index('books').update_dictionary(["J. R. R.", "W. E. B."])
690+
reset_dictionary_1: |-
691+
client.index('books').reset_dictionary()

meilisearch/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Paths:
3333
dumps = "dumps"
3434
pagination = "pagination"
3535
faceting = "faceting"
36+
dictionary = "dictionary"
3637
swap = "swap-indexes"
3738

3839
def __init__(

meilisearch/index.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,66 @@ def reset_faceting_settings(self) -> TaskInfo:
15771577

15781578
return TaskInfo(**task)
15791579

1580+
# USER DICTIONARY SUB-ROUTES
1581+
1582+
def get_dictionary(self) -> List[str]:
1583+
"""Get the dictionary entries of the index.
1584+
1585+
Returns
1586+
-------
1587+
settings:
1588+
List containing the dictionary entries of the index.
1589+
1590+
Raises
1591+
------
1592+
MeilisearchApiError
1593+
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
1594+
"""
1595+
return self.http.get(self.__settings_url_for(self.config.paths.dictionary))
1596+
1597+
def update_dictionary(self, body: List[str]) -> TaskInfo:
1598+
"""Update the dictionary of the index.
1599+
1600+
Parameters
1601+
----------
1602+
body:
1603+
List of the new dictionary entries.
1604+
1605+
Returns
1606+
-------
1607+
task_info:
1608+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
1609+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
1610+
1611+
Raises
1612+
------
1613+
MeilisearchApiError
1614+
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
1615+
"""
1616+
task = self.http.put(self.__settings_url_for(self.config.paths.dictionary), body)
1617+
1618+
return TaskInfo(**task)
1619+
1620+
def reset_dictionary(self) -> TaskInfo:
1621+
"""Clear all entries in dictionary
1622+
1623+
Returns
1624+
-------
1625+
task_info:
1626+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
1627+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
1628+
1629+
Raises
1630+
------
1631+
MeilisearchApiError
1632+
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
1633+
"""
1634+
task = self.http.delete(
1635+
self.__settings_url_for(self.config.paths.dictionary),
1636+
)
1637+
1638+
return TaskInfo(**task)
1639+
15801640
@staticmethod
15811641
def _batch(
15821642
documents: List[Dict[str, Any]], batch_size: int
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
NEW_DICTIONARY = ["J. R. R. Tolkien", "W. E. B. Du Bois"]
2+
3+
4+
def test_get_dictionary_default(empty_index):
5+
"""Tests getting the default value of user dictionary."""
6+
dictionary = empty_index().get_dictionary()
7+
assert dictionary == []
8+
9+
10+
def test_update_dictionary(empty_index):
11+
"""Tests updating the user dictionary."""
12+
index = empty_index()
13+
task = index.update_dictionary(NEW_DICTIONARY)
14+
task = index.wait_for_task(task.task_uid)
15+
assert task.status == "succeeded"
16+
17+
dictionary = index.get_dictionary()
18+
for word in NEW_DICTIONARY:
19+
assert word in dictionary
20+
21+
22+
def test_reset_dictionary(empty_index):
23+
"""Tests resetting the user dictionary to its default empty list."""
24+
index = empty_index()
25+
task = index.update_dictionary(NEW_DICTIONARY)
26+
task = index.wait_for_task(task.task_uid)
27+
assert task.status == "succeeded"
28+
29+
dictionary = index.get_dictionary()
30+
for word in NEW_DICTIONARY:
31+
assert word in dictionary
32+
33+
reset_task = index.reset_dictionary()
34+
reset_task = index.wait_for_task(reset_task.task_uid)
35+
assert reset_task.status == "succeeded"
36+
37+
dictionary = index.get_dictionary()
38+
assert dictionary == []

0 commit comments

Comments
 (0)