Skip to content

Commit 259d857

Browse files
committed
index stats dot attributes + tests
1 parent 1507ed5 commit 259d857

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

meilisearch/index.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def get_stats(self) -> IndexStats:
227227
stats = self.http.get(
228228
f'{self.config.paths.index}/{self.uid}/{self.config.paths.stat}'
229229
)
230-
return IndexStats(**stats)
230+
return IndexStats(stats)
231231

232232
def search(self, query: str, opt_params: Optional[Dict[str, Any]] = None) -> Dict[str, Any]:
233233
"""Search in the index.

meilisearch/models/index.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
from typing import Any, Dict
2-
from camel_converter.pydantic_base import CamelBase
1+
from typing import Any, Dict, Iterator
2+
from camel_converter import to_snake
33

4-
class IndexStats(CamelBase):
5-
number_of_documents: int
6-
is_indexing: bool
7-
field_distribution: Dict[str, Any]
4+
class IndexStats:
5+
__dict: dict
6+
7+
def __init__(self, doc: Dict[str, Any]) -> None:
8+
self.__dict = doc
9+
for key, val in doc.items():
10+
key = to_snake(key)
11+
if isinstance(val, dict):
12+
setattr(self, key, IndexStats(val))
13+
else:
14+
setattr(self, key, val)
15+
16+
def __getattr__(self, attr: str) -> Any:
17+
if attr in self.__dict.keys():
18+
return attr
19+
raise AttributeError(f"{self.__class__.__name__} object has no attribute {attr}")
20+
21+
def __iter__(self) -> Iterator:
22+
return iter(self.__dict__.items()) # type: ignore

tests/index/test_index_stats_meilisearch.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ def test_get_stats(empty_index):
55
response = empty_index().get_stats()
66
assert isinstance(response, IndexStats)
77
assert response.number_of_documents == 0
8+
9+
def test_get_stats_default(index_with_documents):
10+
"""Tests getting stats of a non-empty index."""
11+
response = index_with_documents().get_stats()
12+
assert isinstance(response, IndexStats)
13+
assert response.number_of_documents == 31
14+
assert hasattr(response.field_distribution, 'genre')
15+
assert response.field_distribution.genre == 11

0 commit comments

Comments
 (0)