diff --git a/meilisearch_python_async/index.py b/meilisearch_python_async/index.py index 63667792..409adf33 100644 --- a/meilisearch_python_async/index.py +++ b/meilisearch_python_async/index.py @@ -17,7 +17,12 @@ from meilisearch_python_async.models.documents import DocumentsInfo from meilisearch_python_async.models.index import IndexStats from meilisearch_python_async.models.search import SearchResults -from meilisearch_python_async.models.settings import Faceting, MeiliSearchSettings, TypoTolerance +from meilisearch_python_async.models.settings import ( + Faceting, + MeiliSearchSettings, + Pagination, + TypoTolerance, +) from meilisearch_python_async.models.task import TaskInfo from meilisearch_python_async.task import wait_for_task @@ -2109,6 +2114,79 @@ async def reset_faceting(self) -> TaskInfo: return TaskInfo(**response.json()) + async def get_pagination(self) -> Pagination: + """Get pagination settings for the index. + + Returns: + + Pagination for the index. + + Raises: + + MeilisearchCommunicationError: If there was an error communicating with the server. + MeilisearchApiError: If the MeiliSearch API returned an error. + + Examples: + + >>> from meilisearch_async_client import Client + >>> async with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> pagination_settings = await index.get_pagination() + """ + url = f"{self._settings_url}/pagination" + response = await self._http_requests.get(url) + + return Pagination(**response.json()) + + async def update_pagination(self, settings: Pagination) -> TaskInfo: + """Partially update the pagination settings for an index. + + Returns: + + Task to track the action. + + Raises: + + MeilisearchCommunicationError: If there was an error communicating with the server. + MeilisearchApiError: If the MeiliSearch API returned an error. + + Examples: + + >>> from meilisearch_python_async import Client + >>> from meilisearch_python_async.models.settings import Pagination + >>> async with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> await index.update_pagination(settings=Pagination(max_total_hits=123)) + """ + url = f"{self._settings_url}/pagination" + response = await self._http_requests.patch(url, settings.dict(by_alias=True)) + + return TaskInfo(**response.json()) + + async def reset_pagination(self) -> TaskInfo: + """Reset an index's pagination settings to their default value. + + Returns: + + The details of the task status. + + Raises: + + MeilisearchCommunicationError: If there was an error communicating with the server. + MeilisearchApiError: If the MeiliSearch API returned an error. + + Examples: + + >>> from meilisearch_async_client import Client + >>> async with Client("http://localhost.com", "masterKey") as client: + >>> index = client.index("movies") + >>> await index.reset_pagination() + """ + url = f"{self._settings_url}/pagination" + response = await self._http_requests.delete(url) + + return TaskInfo(**response.json()) + def _batch(documents: list[dict], batch_size: int) -> Generator[list[dict], None, None]: total_len = len(documents) diff --git a/meilisearch_python_async/models/settings.py b/meilisearch_python_async/models/settings.py index 99bb5dfc..a20c26de 100644 --- a/meilisearch_python_async/models/settings.py +++ b/meilisearch_python_async/models/settings.py @@ -19,6 +19,10 @@ class Faceting(CamelBase): max_values_per_facet: int +class Pagination(CamelBase): + max_total_hits: int + + class MeiliSearchSettings(CamelBase): synonyms: Optional[Dict[str, Any]] = None stop_words: Optional[List[str]] = None @@ -30,3 +34,4 @@ class MeiliSearchSettings(CamelBase): sortable_attributes: Optional[List[str]] = None typo_tolerance: Optional[TypoTolerance] = None faceting: Optional[Faceting] = None + pagination: Optional[Pagination] = None diff --git a/tests/test_index.py b/tests/test_index.py index ce98ed60..5c09854e 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -10,6 +10,7 @@ Faceting, MeiliSearchSettings, MinWordSizeForTypos, + Pagination, TypoTolerance, ) from meilisearch_python_async.task import wait_for_task @@ -23,6 +24,7 @@ def new_settings(): sortable_attributes=["genre", "title"], typo_tolerance=TypoTolerance(enabled=False), faceting=Faceting(max_values_per_facet=123), + pagination=Pagination(max_total_hits=17), ) @@ -76,6 +78,11 @@ def filterable_attributes(): return ["release_date", "title"] +@pytest.fixture +def default_pagination(): + return Pagination(max_total_hits=1000) + + @pytest.fixture def sortable_attributes(): return ["genre", "title"] @@ -117,7 +124,9 @@ async def test_get_stats(empty_index, small_movies): assert response.number_of_documents == 30 -async def test_get_settings_default(empty_index, default_ranking_rules, default_faceting): +async def test_get_settings_default( + empty_index, default_ranking_rules, default_faceting, default_pagination +): index = await empty_index() response = await index.get_settings() assert response.ranking_rules == default_ranking_rules @@ -129,6 +138,7 @@ async def test_get_settings_default(empty_index, default_ranking_rules, default_ assert response.sortable_attributes == [] assert response.typo_tolerance.enabled is True assert response.faceting == default_faceting + assert response.pagination == default_pagination async def test_update_settings(empty_index, new_settings): @@ -148,6 +158,7 @@ async def test_update_settings(empty_index, new_settings): assert ( response.faceting.max_values_per_facet == new_settings.faceting.max_values_per_facet == 123 ) + assert response.pagination == new_settings.pagination async def test_reset_settings(empty_index, new_settings, default_ranking_rules): @@ -164,6 +175,7 @@ async def test_reset_settings(empty_index, new_settings, default_ranking_rules): assert response.synonyms == {} assert response.sortable_attributes == new_settings.sortable_attributes assert response.typo_tolerance.enabled is False + assert response.pagination == new_settings.pagination response = await index.reset_settings() update = await wait_for_task(index.http_client, response.task_uid) assert update.status == "succeeded" @@ -177,6 +189,7 @@ async def test_reset_settings(empty_index, new_settings, default_ranking_rules): assert response.sortable_attributes == [] assert response.typo_tolerance.enabled is True assert response.faceting.max_values_per_facet == 100 + assert response.pagination.max_total_hits == 1000 async def test_get_ranking_rules_default(empty_index, default_ranking_rules): @@ -297,6 +310,31 @@ async def test_reset_displayed_attributes(empty_index, displayed_attributes): assert response == ["*"] +async def test_get_pagination(empty_index): + index = await empty_index() + response = await index.get_pagination() + assert response.max_total_hits == 1000 + + +async def test_update_pagination(empty_index): + pagination = Pagination(max_total_hits=17) + index = await empty_index() + response = await index.update_pagination(pagination) + await wait_for_task(index.http_client, response.task_uid) + response = await index.get_pagination() + assert pagination.dict() == pagination.dict() + + +async def test_reset_pagination(empty_index, default_pagination): + index = await empty_index() + response = await index.update_pagination(Pagination(max_total_hits=17)) + await wait_for_task(index.http_client, response.task_uid) + response = await index.reset_pagination() + await wait_for_task(index.http_client, response.task_uid) + response = await index.get_pagination() + assert response.dict() == default_pagination.dict() + + async def test_get_stop_words_default(empty_index): index = await empty_index() response = await index.get_stop_words()