Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Pagination Settings #313

Merged
merged 3 commits into from Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 79 additions & 1 deletion meilisearch_python_async/index.py
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions meilisearch_python_async/models/settings.py
Expand Up @@ -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
Expand All @@ -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
40 changes: 39 additions & 1 deletion tests/test_index.py
Expand Up @@ -10,6 +10,7 @@
Faceting,
MeiliSearchSettings,
MinWordSizeForTypos,
Pagination,
TypoTolerance,
)
from meilisearch_python_async.task import wait_for_task
Expand All @@ -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),
)


Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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"
Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down