Skip to content

Commit

Permalink
Add methods for Prefix Search settings requests
Browse files Browse the repository at this point in the history
  • Loading branch information
thicolares committed Jan 25, 2025
1 parent 4bc697d commit cdbbdc1
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,12 @@ update_search_cutoff_1: |-
client.index('movies').update_search_cutoff_ms(150)
reset_search_cutoff_1: |-
client.index('movies').reset_search_cutoff_ms()
get_prefix_search_settings_1: |-
client.index('books').get_prefix_search()
update_prefix_search_settings_1: |-
client.index('books').update_prefix_search(PrefixSearch.DISABLED)
reset_prefix_search_settings_1: |-
client.index('books').reset_prefix_search()
get_proximity_precision_settings_1: |-
client.index('books').get_proximity_precision()
update_proximity_precision_settings_1: |-
Expand Down
3 changes: 2 additions & 1 deletion meilisearch/_httprequests.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
MeilisearchCommunicationError,
MeilisearchTimeoutError,
)
from meilisearch.models.index import ProximityPrecision
from meilisearch.models.index import PrefixSearch, ProximityPrecision
from meilisearch.version import qualified_version


Expand Down Expand Up @@ -116,6 +116,7 @@ def put(
bytes,
str,
int,
PrefixSearch,
ProximityPrecision,
]
] = None,
Expand Down
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Paths:
swap = "swap-indexes"
embedders = "embedders"
search_cutoff_ms = "search-cutoff-ms"
prefix_search = "prefix-search"
proximity_precision = "proximity-precision"
localized_attributes = "localized-attributes"

Expand Down
53 changes: 53 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
LocalizedAttributes,
OpenAiEmbedder,
Pagination,
PrefixSearch,
ProximityPrecision,
TypoTolerance,
UserProvidedEmbedder,
Expand Down Expand Up @@ -2056,6 +2057,58 @@ def reset_search_cutoff_ms(self) -> TaskInfo:

return TaskInfo(**task)

# PREFIX SEARCH

def get_prefix_search(self) -> PrefixSearch:
"""Get the prefix search settings of an index.
Returns
-------
settings:
The prefix search 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://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
"""
prefix_search = self.http.get(self.__settings_url_for(self.config.paths.prefix_search))

return PrefixSearch[to_snake(prefix_search).upper()]

def update_prefix_search(self, body: Union[PrefixSearch, None]) -> TaskInfo:
"""Update the prefix search settings of the index.
Parameters
----------
body:
Prefix search settings
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
"""
task = self.http.put(self.__settings_url_for(self.config.paths.prefix_search), body)

return TaskInfo(**task)

def reset_prefix_search(self) -> TaskInfo:
"""Reset the prefix search settings 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
"""
task = self.http.delete(
self.__settings_url_for(self.config.paths.prefix_search),
)

return TaskInfo(**task)

# PROXIMITY PRECISION SETTINGS

def get_proximity_precision(self) -> ProximityPrecision:
Expand Down
12 changes: 12 additions & 0 deletions meilisearch/models/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ class TypoTolerance(CamelBase):
min_word_size_for_typos: Optional[MinWordSizeForTypos] = None


class PrefixSearch(str, Enum):
INDEXING_TIME = "indexingTime"
"""
Calculate prefix search during indexing. This is the default behavior.
"""

DISABLED = "disabled"
"""
Do not calculate prefix search. May speed up indexing, but will severely impact search result relevancy.
"""


class ProximityPrecision(str, Enum):
BY_WORD = "byWord"
BY_ATTRIBUTE = "byAttribute"
Expand Down
39 changes: 39 additions & 0 deletions tests/settings/test_settings_prefix_search.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from meilisearch.models.index import PrefixSearch


DEFAULT_PREFIX_SEARCH_SETTINGS = PrefixSearch.INDEXING_TIME


def test_get_prefix_search(empty_index):
response = empty_index().get_prefix_search()

assert DEFAULT_PREFIX_SEARCH_SETTINGS == response


def test_update_prefix_search(empty_index):
index = empty_index()

response = index.update_prefix_search(PrefixSearch.DISABLED)
index.wait_for_task(response.task_uid)
response = index.get_prefix_search()
assert PrefixSearch.DISABLED == response

response = index.update_prefix_search(PrefixSearch.INDEXING_TIME)
index.wait_for_task(response.task_uid)
response = index.get_prefix_search()
assert PrefixSearch.INDEXING_TIME == response


def test_reset_prefix_search(empty_index):
index = empty_index()

response = index.update_prefix_search(PrefixSearch.DISABLED)
index.wait_for_task(response.task_uid)
response = index.get_prefix_search()
assert PrefixSearch.DISABLED == response
assert DEFAULT_PREFIX_SEARCH_SETTINGS != response

response = index.reset_prefix_search()
index.wait_for_task(response.task_uid)
response = index.get_prefix_search()
assert DEFAULT_PREFIX_SEARCH_SETTINGS == response

0 comments on commit cdbbdc1

Please sign in to comment.