Skip to content
Open
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
7 changes: 7 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -662,3 +662,10 @@ post_render_template_1: |-
'inline': {'id': 'this document'}
}
)
get_word_position_settings_1: |-
client.index('books').get_word_position_settings()
update_word_position_settings_1: |-
client.index('books').update_word_position_settings({'enabled': False})
reset_word_position_settings_1: |-
client.index('books').reset_word_position_settings()

1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Paths:
prefix_search = "prefix-search"
proximity_precision = "proximity-precision"
localized_attributes = "localized-attributes"
word_position = "word-position"
fields = "fields"
edit = "edit"
network = "network"
Expand Down
69 changes: 69 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
ProximityPrecision,
SizeFormat,
TypoTolerance,
WordPosition,
)
from meilisearch.models.task import Task, TaskInfo, TaskResults
from meilisearch.task import TaskHandler
Expand Down Expand Up @@ -1280,6 +1281,7 @@ def update_settings(
- 'searchCutoffMs': Maximum search time in milliseconds
- 'proximityPrecision': Precision for proximity ranking
- 'localizedAttributes': Settings for localized attributes
- 'wordPosition': Settings for word position computation during indexing
- 'foreignKeys': List of foreign key relationships to other indexes

More information:
Expand Down Expand Up @@ -2641,6 +2643,73 @@ def reset_localized_attributes(self) -> TaskInfo:

return TaskInfo(**task)

# WORD POSITION SETTINGS

def get_word_position_settings(self) -> WordPosition:
"""Get the word position settings of the index.

Returns
-------
settings:
The word position 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
"""
word_position = self.http.get(self.__settings_url_for(self.config.paths.word_position))

return WordPosition(**word_position)

def update_word_position_settings(self, body: Mapping[str, Any] | None) -> TaskInfo:
"""Update the word position settings of the index.

Parameters
----------
body: dict
Dictionary containing the word position settings.
https://www.meilisearch.com/docs/reference/api/settings#update-wordposition

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.patch(self.__settings_url_for(self.config.paths.word_position), body)

return TaskInfo(**task)

def reset_word_position_settings(self) -> TaskInfo:
"""Reset the word position settings of the index to default values.

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.word_position),
)

return TaskInfo(**task)

get_word_position = get_word_position_settings
update_word_position = update_word_position_settings
reset_word_position = reset_word_position_settings

def get_fields(
self,
offset: int | None = None,
Expand Down
4 changes: 4 additions & 0 deletions meilisearch/models/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,7 @@ class EmbedderDistribution(CamelBase):
class LocalizedAttributes(CamelBase):
attribute_patterns: list[str]
locales: list[str]


class WordPosition(CamelBase):
enabled: bool | None = True
10 changes: 10 additions & 0 deletions tests/settings/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,13 @@ def test_reset_settings(new_settings, empty_index):
assert response["stopWords"] == []
assert response["synonyms"] == {}
assert response["embedders"] == {}


def test_update_word_position_via_general_settings(empty_index):
"""Tests updating wordPosition via general settings endpoint."""
index = empty_index()
response = index.update_settings({"wordPosition": {"enabled": False}})
update = index.wait_for_task(response.task_uid)
assert update.status == "succeeded"
response = index.get_settings()
assert response["wordPosition"] == {"enabled": False}
35 changes: 35 additions & 0 deletions tests/settings/test_settings_word_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from meilisearch.models.index import WordPosition

DEFAULT_WORD_POSITION = {"enabled": True}
NEW_WORD_POSITION = {"enabled": False}


def test_get_word_position_settings(empty_index):
response = empty_index().get_word_position_settings()

assert isinstance(response, WordPosition)
assert response.model_dump(by_alias=True) == DEFAULT_WORD_POSITION
assert response.enabled is True


def test_update_word_position_settings(empty_index):
index = empty_index()
response = index.update_word_position_settings(NEW_WORD_POSITION)
index.wait_for_task(response.task_uid)
response = index.get_word_position_settings()
assert response.model_dump(by_alias=True) == NEW_WORD_POSITION
assert response.enabled is False


def test_reset_word_position_settings(empty_index):
index = empty_index()
response = index.update_word_position_settings(NEW_WORD_POSITION)
index.wait_for_task(response.task_uid)
response = index.get_word_position_settings()
assert response.model_dump(by_alias=True) == NEW_WORD_POSITION

response = index.reset_word_position_settings()
index.wait_for_task(response.task_uid)
response = index.get_word_position_settings()
assert response.model_dump(by_alias=True) == DEFAULT_WORD_POSITION
assert response.enabled is True
15 changes: 15 additions & 0 deletions tests/types/index_word_position.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from typing_extensions import assert_type

from meilisearch import Client
from meilisearch.models.index import WordPosition
from meilisearch.models.task import TaskInfo


def check_index_word_position(client: Client) -> None:
index = client.index("test")
assert_type(index.get_word_position_settings(), WordPosition)
assert_type(index.get_word_position(), WordPosition)
assert_type(index.update_word_position_settings({"enabled": False}), TaskInfo)
assert_type(index.update_word_position({"enabled": False}), TaskInfo)
assert_type(index.reset_word_position_settings(), TaskInfo)
assert_type(index.reset_word_position(), TaskInfo)