Skip to content

Commit

Permalink
typing of the meta attribute of hits
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 18, 2024
1 parent 18e596e commit 3827ae6
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 7 deletions.
9 changes: 8 additions & 1 deletion elasticsearch_dsl/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from .field import Field
from .index_base import IndexBase
from .response import Hit # noqa: F401
from .types import Hit as HitBaseType

UsingType: TypeAlias = Union[str, "Elasticsearch"]
AsyncUsingType: TypeAlias = Union[str, "AsyncElasticsearch"]
Expand Down Expand Up @@ -468,7 +469,13 @@ def _clone(self) -> Self:
return c


class HitMeta(AttrDict[Any]):
if TYPE_CHECKING:
HitMetaBase = HitBaseType
else:
HitMetaBase = AttrDict[Any]


class HitMeta(HitMetaBase):
def __init__(
self,
document: Dict[str, Any],
Expand Down
2 changes: 1 addition & 1 deletion examples/async/parent_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ async def get_answers(self) -> List[Any]:
elasticsearch.
"""
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
return cast(List[Any], self.meta.inner_hits.answer.hits)
return cast(List[Any], self.meta.inner_hits["answer"].hits)
return [a async for a in self.search_answers()]

async def save(self, **kwargs: Any) -> None: # type: ignore[override]
Expand Down
2 changes: 1 addition & 1 deletion examples/async/sparse_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ async def main() -> None:
)
print(f"Summary: {hit.summary}")
if args.show_inner_hits:
for passage in hit.meta.inner_hits.passages:
for passage in hit.meta.inner_hits["passages"]:
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
print("")

Expand Down
2 changes: 1 addition & 1 deletion examples/async/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ async def main() -> None:
)
print(f"Summary: {hit.summary}")
if args.show_inner_hits:
for passage in hit.meta.inner_hits.passages:
for passage in hit.meta.inner_hits["passages"]:
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
print("")

Expand Down
2 changes: 1 addition & 1 deletion examples/parent_child.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def get_answers(self) -> List[Any]:
elasticsearch.
"""
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
return cast(List[Any], self.meta.inner_hits.answer.hits)
return cast(List[Any], self.meta.inner_hits["answer"].hits)
return [a for a in self.search_answers()]

def save(self, **kwargs: Any) -> None: # type: ignore[override]
Expand Down
2 changes: 1 addition & 1 deletion examples/sparse_vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ def main() -> None:
)
print(f"Summary: {hit.summary}")
if args.show_inner_hits:
for passage in hit.meta.inner_hits.passages:
for passage in hit.meta.inner_hits["passages"]:
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
print("")

Expand Down
2 changes: 1 addition & 1 deletion examples/vectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def main() -> None:
)
print(f"Summary: {hit.summary}")
if args.show_inner_hits:
for passage in hit.meta.inner_hits.passages:
for passage in hit.meta.inner_hits["passages"]:
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
print("")

Expand Down

0 comments on commit 3827ae6

Please sign in to comment.