From 7dd2480b2f3332532d1eb9634ef6b0d8002fcef0 Mon Sep 17 00:00:00 2001 From: Makiyu-py <73825066+Makiyu-py@users.noreply.github.com> Date: Fri, 14 Oct 2022 22:55:50 +0800 Subject: [PATCH 1/3] add pagination settings --- meilisearch_python_async/index.py | 80 ++++++++++++++++++++- meilisearch_python_async/models/settings.py | 5 ++ 2 files changed, 84 insertions(+), 1 deletion(-) 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 From 7f610a4ba2a3d04738c5ee0140d694e3cfbd77d5 Mon Sep 17 00:00:00 2001 From: Paul Sanders Date: Fri, 14 Oct 2022 17:32:52 -0400 Subject: [PATCH 2/3] Add tests --- tests/test_index.py | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/tests/test_index.py b/tests/test_index.py index ce98ed60..5fd68d1d 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -8,6 +8,7 @@ from meilisearch_python_async.index import _iso_to_date_time from meilisearch_python_async.models.settings import ( Faceting, + Pagination, MeiliSearchSettings, MinWordSizeForTypos, TypoTolerance, @@ -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,7 @@ 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 +136,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 +156,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 +173,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 +187,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 +308,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() From eea24691afafc0a5d39674d7bb5b3468454f0ff8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Oct 2022 21:33:17 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_index.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_index.py b/tests/test_index.py index 5fd68d1d..5c09854e 100644 --- a/tests/test_index.py +++ b/tests/test_index.py @@ -8,9 +8,9 @@ from meilisearch_python_async.index import _iso_to_date_time from meilisearch_python_async.models.settings import ( Faceting, - Pagination, MeiliSearchSettings, MinWordSizeForTypos, + Pagination, TypoTolerance, ) from meilisearch_python_async.task import wait_for_task @@ -124,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, default_pagination): +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