Skip to content

Commit

Permalink
Merge #848
Browse files Browse the repository at this point in the history
848: Add sort_facet_values_by to Faceting model r=sanders41 a=sanders41

# Pull Request

`@curquiza` this is what is needed in addition to the code samples for #829

## Related issue
Fixes #829

## What does this PR do?
- Adds `sort_facet_values_by` to the `Faceting` model

## PR checklist
Please check if your PR fulfills the following requirements:
- [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
- [x] Have you read the contributing guidelines?
- [x] Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!


Co-authored-by: Paul Sanders <[email protected]>
  • Loading branch information
meili-bors[bot] and sanders41 authored Sep 21, 2023
2 parents 9f6944e + 715a9e9 commit d31673b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions meilisearch/models/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def __iter__(self) -> Iterator:

class Faceting(CamelBase):
max_values_per_facet: int
sort_facet_values_by: Optional[Dict[str, str]] = None


class Pagination(CamelBase):
Expand Down
54 changes: 54 additions & 0 deletions tests/settings/test_setting_faceting.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import pytest

from meilisearch.models.index import Faceting

DEFAULT_MAX_VALUE_PER_FACET = 100
DEFAULT_SORT_FACET_VALUES_BY = {"*": "alpha"}
NEW_MAX_VALUE_PER_FACET = {"maxValuesPerFacet": 200}


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

assert DEFAULT_MAX_VALUE_PER_FACET == response.max_values_per_facet
assert DEFAULT_SORT_FACET_VALUES_BY == response.sort_facet_values_by


def test_update_faceting_settings(empty_index):
Expand All @@ -25,3 +31,51 @@ def test_delete_faceting_settings(empty_index):
index.wait_for_task(response.task_uid)
response = index.get_faceting_settings()
assert DEFAULT_MAX_VALUE_PER_FACET == response.max_values_per_facet


@pytest.mark.parametrize(
"index_name, facet_order, max_values_per_facet, expected",
[
("*", "alpha", 17, {"max_values_per_facet": 17, "sort_facet_values_by": {"*": "alpha"}}),
("*", "count", 41, {"max_values_per_facet": 41, "sort_facet_values_by": {"*": "count"}}),
(
"movies",
"alpha",
42,
{"max_values_per_facet": 42, "sort_facet_values_by": {"*": "alpha", "movies": "alpha"}},
),
(
"movies",
"alpha",
73,
{"max_values_per_facet": 73, "sort_facet_values_by": {"*": "alpha", "movies": "alpha"}},
),
],
)
def test_update_faceting_sort_facet_values(
index_name, facet_order, max_values_per_facet, expected, empty_index
):
faceting = Faceting(
max_values_per_facet=max_values_per_facet,
sort_facet_values_by={index_name: facet_order},
)
index = empty_index()
response = index.update_faceting_settings(faceting.model_dump(by_alias=True))
index.wait_for_task(response.task_uid)
response = index.get_faceting_settings()
assert response.model_dump() == expected


def test_reset_faceting(empty_index):
index = empty_index()
response = index.update_faceting_settings(
{"maxValuesPerFacet": 17, "sortFacetValuesBy": {"*": "count"}}
)
index.wait_for_task(response.task_uid)
response = index.reset_faceting_settings()
index.wait_for_task(response.task_uid)
response = index.get_faceting_settings()
assert response == Faceting(
max_values_per_facet=DEFAULT_MAX_VALUE_PER_FACET,
sort_facet_values_by=DEFAULT_SORT_FACET_VALUES_BY,
)

0 comments on commit d31673b

Please sign in to comment.