Skip to content

Commit

Permalink
Add sort_facet_values_by to Faceting model
Browse files Browse the repository at this point in the history
  • Loading branch information
sanders41 committed Sep 20, 2023
1 parent a4b6513 commit 69f6be1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
32 changes: 32 additions & 0 deletions meilisearch/models/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

from typing import Any, Dict, Iterator, List, Optional

import pydantic
from camel_converter import to_snake
from camel_converter.pydantic_base import CamelBase

from meilisearch._utils import is_pydantic_2


class IndexStats:
__dict: Dict
Expand All @@ -29,6 +32,35 @@ def __iter__(self) -> Iterator:

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

if is_pydantic_2():

@pydantic.field_validator("sort_facet_values_by") # type: ignore[attr-defined]
@classmethod
def validate_facet_order(cls, v: Optional[Dict[str, str]]) -> Optional[Dict[str, str]]:
if not v: # pragma: no cover
return None

for _, value in v.items():
if value not in ("alpha", "count"):
raise ValueError('facet_order must be either "alpha" or "count"')

return v

else: # pragma: no cover

@pydantic.validator("sort_facet_values_by") # type: ignore[attr-defined]
@classmethod
def validate_facet_order(cls, v: Optional[Dict[str, str]]) -> Optional[Dict[str, str]]:
if not v:
return None

for _, value in v.items():
if value not in ("alpha", "count"):
raise ValueError('facet_order must be either "alpha" or "count"')

return v


class Pagination(CamelBase):
Expand Down
62 changes: 62 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,59 @@ 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_update_faceting_sort_facet_values_invalid_sort_type():
with pytest.raises(ValueError):
Faceting(
max_values_per_facet=2,
sort_facet_values_by={"*": "bad"},
)


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 69f6be1

Please sign in to comment.