Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport 8.x] Autogenerate aggregation responses and hits #1940

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 92 additions & 6 deletions elasticsearch_dsl/response/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@
from ..search_base import Request, SearchBase
from ..update_by_query_base import UpdateByQueryBase

__all__ = ["Response", "AggResponse", "UpdateByQueryResponse", "Hit", "HitMeta"]
__all__ = [
"Response",
"AggResponse",
"UpdateByQueryResponse",
"Hit",
"HitMeta",
"AggregateResponseType",
]


class Response(AttrDict[Any], Generic[_R]):
"""An Elasticsearch response.
"""An Elasticsearch search response.

:arg took: (required)
:arg timed_out: (required)
Expand Down Expand Up @@ -195,21 +202,100 @@ def search_after(self) -> "SearchBase[_R]":
return self._search.extra(search_after=self.hits[-1].meta.sort) # type: ignore


AggregateResponseType = Union[
"types.CardinalityAggregate",
"types.HdrPercentilesAggregate",
"types.HdrPercentileRanksAggregate",
"types.TDigestPercentilesAggregate",
"types.TDigestPercentileRanksAggregate",
"types.PercentilesBucketAggregate",
"types.MedianAbsoluteDeviationAggregate",
"types.MinAggregate",
"types.MaxAggregate",
"types.SumAggregate",
"types.AvgAggregate",
"types.WeightedAvgAggregate",
"types.ValueCountAggregate",
"types.SimpleValueAggregate",
"types.DerivativeAggregate",
"types.BucketMetricValueAggregate",
"types.StatsAggregate",
"types.StatsBucketAggregate",
"types.ExtendedStatsAggregate",
"types.ExtendedStatsBucketAggregate",
"types.GeoBoundsAggregate",
"types.GeoCentroidAggregate",
"types.HistogramAggregate",
"types.DateHistogramAggregate",
"types.AutoDateHistogramAggregate",
"types.VariableWidthHistogramAggregate",
"types.StringTermsAggregate",
"types.LongTermsAggregate",
"types.DoubleTermsAggregate",
"types.UnmappedTermsAggregate",
"types.LongRareTermsAggregate",
"types.StringRareTermsAggregate",
"types.UnmappedRareTermsAggregate",
"types.MultiTermsAggregate",
"types.MissingAggregate",
"types.NestedAggregate",
"types.ReverseNestedAggregate",
"types.GlobalAggregate",
"types.FilterAggregate",
"types.ChildrenAggregate",
"types.ParentAggregate",
"types.SamplerAggregate",
"types.UnmappedSamplerAggregate",
"types.GeoHashGridAggregate",
"types.GeoTileGridAggregate",
"types.GeoHexGridAggregate",
"types.RangeAggregate",
"types.DateRangeAggregate",
"types.GeoDistanceAggregate",
"types.IpRangeAggregate",
"types.IpPrefixAggregate",
"types.FiltersAggregate",
"types.AdjacencyMatrixAggregate",
"types.SignificantLongTermsAggregate",
"types.SignificantStringTermsAggregate",
"types.UnmappedSignificantTermsAggregate",
"types.CompositeAggregate",
"types.FrequentItemSetsAggregate",
"types.TimeSeriesAggregate",
"types.ScriptedMetricAggregate",
"types.TopHitsAggregate",
"types.InferenceAggregate",
"types.StringStatsAggregate",
"types.BoxPlotAggregate",
"types.TopMetricsAggregate",
"types.TTestAggregate",
"types.RateAggregate",
"types.CumulativeCardinalityAggregate",
"types.MatrixStatsAggregate",
"types.GeoLineAggregate",
]


class AggResponse(AttrDict[Any], Generic[_R]):
"""An Elasticsearch aggregation response."""

_meta: Dict[str, Any]

def __init__(self, aggs: "Agg[_R]", search: "Request[_R]", data: Dict[str, Any]):
super(AttrDict, self).__setattr__("_meta", {"search": search, "aggs": aggs})
super().__init__(data)

def __getitem__(self, attr_name: str) -> Any:
def __getitem__(self, attr_name: str) -> AggregateResponseType:
if attr_name in self._meta["aggs"]:
# don't do self._meta['aggs'][attr_name] to avoid copying
agg = self._meta["aggs"].aggs[attr_name]
return agg.result(self._meta["search"], self._d_[attr_name])
return super().__getitem__(attr_name)
return cast(
AggregateResponseType,
agg.result(self._meta["search"], self._d_[attr_name]),
)
return super().__getitem__(attr_name) # type: ignore

def __iter__(self) -> Iterator["Agg"]: # type: ignore[override]
def __iter__(self) -> Iterator[AggregateResponseType]: # type: ignore[override]
for name in self._meta["aggs"]:
yield self[name]

Expand Down
Loading
Loading