Skip to content

Commit 099a52f

Browse files
typing of the meta attribute of hits
1 parent 18e596e commit 099a52f

File tree

9 files changed

+55
-40
lines changed

9 files changed

+55
-40
lines changed

elasticsearch_dsl/types.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5078,49 +5078,49 @@ class HistogramBucket(AttrDict[Any]):
50785078

50795079
class Hit(AttrDict[Any]):
50805080
"""
5081-
:arg _index: (required)
5082-
:arg _id:
5083-
:arg _score:
5084-
:arg _explanation:
5081+
:arg index: (required)
5082+
:arg id:
5083+
:arg score:
5084+
:arg explanation:
50855085
:arg fields:
50865086
:arg highlight:
50875087
:arg inner_hits:
50885088
:arg matched_queries:
5089-
:arg _nested:
5090-
:arg _ignored:
5089+
:arg nested:
5090+
:arg ignored:
50915091
:arg ignored_field_values:
5092-
:arg _shard:
5093-
:arg _node:
5094-
:arg _routing:
5095-
:arg _source:
5096-
:arg _rank:
5097-
:arg _seq_no:
5098-
:arg _primary_term:
5099-
:arg _version:
5092+
:arg shard:
5093+
:arg node:
5094+
:arg routing:
5095+
:arg source:
5096+
:arg rank:
5097+
:arg seq_no:
5098+
:arg primary_term:
5099+
:arg version:
51005100
:arg sort:
51015101
"""
51025102

5103-
_index: str
5104-
_id: str
5105-
_score: Union[float, None]
5106-
_explanation: "Explanation"
5103+
index: str
5104+
id: str
5105+
score: Union[float, None]
5106+
explanation: "Explanation"
51075107
fields: Mapping[str, Any]
51085108
highlight: Mapping[str, Sequence[str]]
51095109
inner_hits: Mapping[str, "InnerHitsResult"]
51105110
matched_queries: Union[Sequence[str], Mapping[str, float]]
5111-
_nested: "NestedIdentity"
5112-
_ignored: Sequence[str]
5111+
nested: "NestedIdentity"
5112+
ignored: Sequence[str]
51135113
ignored_field_values: Mapping[
51145114
str, Sequence[Union[int, float, str, bool, None, Any]]
51155115
]
5116-
_shard: str
5117-
_node: str
5118-
_routing: str
5119-
_source: Any
5120-
_rank: int
5121-
_seq_no: int
5122-
_primary_term: int
5123-
_version: int
5116+
shard: str
5117+
node: str
5118+
routing: str
5119+
source: Any
5120+
rank: int
5121+
seq_no: int
5122+
primary_term: int
5123+
version: int
51245124
sort: Sequence[Union[int, float, str, bool, None, Any]]
51255125

51265126

elasticsearch_dsl/utils.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
Iterable,
2929
Iterator,
3030
List,
31+
Mapping,
3132
Optional,
3233
Tuple,
3334
Type,
@@ -48,6 +49,7 @@
4849
from .field import Field
4950
from .index_base import IndexBase
5051
from .response import Hit # noqa: F401
52+
from .types import Hit as HitBaseType
5153

5254
UsingType: TypeAlias = Union[str, "Elasticsearch"]
5355
AsyncUsingType: TypeAlias = Union[str, "AsyncElasticsearch"]
@@ -468,7 +470,15 @@ def _clone(self) -> Self:
468470
return c
469471

470472

471-
class HitMeta(AttrDict[Any]):
473+
if TYPE_CHECKING:
474+
HitMetaBase = HitBaseType
475+
else:
476+
HitMetaBase = AttrDict[Any]
477+
478+
479+
class HitMeta(HitMetaBase):
480+
inner_hits: Mapping[str, Any]
481+
472482
def __init__(
473483
self,
474484
document: Dict[str, Any],

examples/async/parent_child.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ async def get_answers(self) -> List[Any]:
190190
elasticsearch.
191191
"""
192192
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
193-
return cast(List[Any], self.meta.inner_hits.answer.hits)
193+
return cast(List[Any], self.meta.inner_hits["answer"].hits)
194194
return [a async for a in self.search_answers()]
195195

196196
async def save(self, **kwargs: Any) -> None: # type: ignore[override]

examples/async/sparse_vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ async def main() -> None:
186186
)
187187
print(f"Summary: {hit.summary}")
188188
if args.show_inner_hits:
189-
for passage in hit.meta.inner_hits.passages:
189+
for passage in hit.meta.inner_hits["passages"]:
190190
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
191191
print("")
192192

examples/async/vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ async def main() -> None:
175175
)
176176
print(f"Summary: {hit.summary}")
177177
if args.show_inner_hits:
178-
for passage in hit.meta.inner_hits.passages:
178+
for passage in hit.meta.inner_hits["passages"]:
179179
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
180180
print("")
181181

examples/parent_child.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ def get_answers(self) -> List[Any]:
189189
elasticsearch.
190190
"""
191191
if "inner_hits" in self.meta and "answer" in self.meta.inner_hits:
192-
return cast(List[Any], self.meta.inner_hits.answer.hits)
192+
return cast(List[Any], self.meta.inner_hits["answer"].hits)
193193
return [a for a in self.search_answers()]
194194

195195
def save(self, **kwargs: Any) -> None: # type: ignore[override]

examples/sparse_vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def main() -> None:
185185
)
186186
print(f"Summary: {hit.summary}")
187187
if args.show_inner_hits:
188-
for passage in hit.meta.inner_hits.passages:
188+
for passage in hit.meta.inner_hits["passages"]:
189189
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
190190
print("")
191191

examples/vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def main() -> None:
174174
)
175175
print(f"Summary: {hit.summary}")
176176
if args.show_inner_hits:
177-
for passage in hit.meta.inner_hits.passages:
177+
for passage in hit.meta.inner_hits["passages"]:
178178
print(f" - [Score: {passage.meta.score}] {passage.content!r}")
179179
print("")
180180

utils/generator.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -702,12 +702,12 @@ def interface_to_python_class(
702702
# types via generics, each in array or object configurations.
703703
# Typing this attribute proved very difficult. A solution
704704
# that worked with mypy and pyright is to type "buckets"
705-
# with the array (list) form, and create a `buckets_as_dict`
706-
# property that is typed appropriate for accessing the
707-
# buckets when in object (dictionary) form.
705+
# for the list form, and create a `buckets_as_dict`
706+
# property that is typed appropriately for accessing the
707+
# buckets in dictionary form.
708708
# The generic type is assumed to be the first in the list,
709-
# which is a simplification that should be removed when a
710-
# more complete implementation of generic is added.
709+
# which is a simplification that should be improved when a
710+
# more complete implementation of generics is added.
711711
if generics[0]["type"]["name"] == "Void":
712712
generic_type = "Any"
713713
else:
@@ -733,6 +733,11 @@ def interface_to_python_class(
733733
)
734734
k["buckets_as_dict"] = generic_type
735735
else:
736+
if interface == "Hit" and arg["name"].startswith("_"):
737+
# Python DSL removes the undersore prefix from all the
738+
# properties of the hit, so we do the same
739+
arg["name"] = arg["name"][1:]
740+
736741
self.add_attribute(
737742
k, arg, for_types_py=for_types_py, for_response=for_response
738743
)

0 commit comments

Comments
 (0)