Skip to content

Add faceting settings customization #506

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Paths():
typo_tolerance = 'typo-tolerance'
dumps = 'dumps'
pagination = 'pagination'
faceting = 'faceting'

def __init__(
self,
Expand Down
59 changes: 59 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1288,6 +1288,65 @@ def reset_pagination_settings(self) -> Dict[str, Any]:
"""
return self.http.delete(self.__settings_url_for(self.config.paths.pagination))

def get_faceting_settings(self) -> Dict[str, Any]:
"""Get the faceting settings of an index.

Returns
-------
settings: dict
Dictionary containing the faceting 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://docs.meilisearch.com/errors/#meilisearch-errors
"""

return self.http.get(self.__settings_url_for(self.config.paths.faceting))

def update_faceting_settings(self, body: Dict[str, Any]) -> Dict[str, Any]:
"""Update the faceting settings of the index.

Parameters
----------
body: dict
Dictionary containing the faceting settings.
https://docs.meilisearch.com/reference/api/faceting.html#update-faceting-settings

Returns
-------
task:
Dictionary containing a task to track the informations about the progress of an asynchronous process.
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task

Raises
------
MeiliSearchApiError
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
"""
return self.http.patch(
path=self.__settings_url_for(self.config.paths.faceting),
body=body
)


def reset_faceting_settings(self) -> Dict[str, Any]:
"""Reset faceting settings of the index to default values.

Returns
-------
task:
Dictionary containing a task to track the informations about the progress of an asynchronous process.
https://docs.meilisearch.com/reference/api/tasks.html#get-one-task

Raises
------
MeiliSearchApiError
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://docs.meilisearch.com/errors/#meilisearch-errors
"""
return self.http.delete(self.__settings_url_for(self.config.paths.faceting))


@staticmethod
def _batch(
documents: List[Dict[str, Any]], batch_size: int
Expand Down
31 changes: 31 additions & 0 deletions tests/settings/test_setting_faceting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
DEFAULT_MAX_VALUE_PER_FACET = 100
NEW_MAX_VALUE_PER_FACET = {'maxValuesPerFacet': 200 }


def test_get_faceting_settings(empty_index):
response = empty_index().get_faceting_settings()

assert isinstance(response, dict)
assert { 'maxValuesPerFacet': DEFAULT_MAX_VALUE_PER_FACET } == response


def test_update_faceting_settings(empty_index):
index = empty_index()
response = index.update_faceting_settings(NEW_MAX_VALUE_PER_FACET)
assert isinstance(response, dict)
assert 'taskUid' in response

index.wait_for_task(response['taskUid'])
response = index.get_faceting_settings()
assert isinstance(response, dict)
assert NEW_MAX_VALUE_PER_FACET == response


def test_delete_faceting_settings(empty_index):
index = empty_index()
response = index.reset_faceting_settings()

index.wait_for_task(response['taskUid'])
response = index.get_faceting_settings()
assert isinstance(response, dict)
assert { 'maxValuesPerFacet': DEFAULT_MAX_VALUE_PER_FACET } == response