Skip to content

[Backport 9.0] Handle new aggregation range types Pythonically #2865

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

Merged
merged 2 commits into from
Apr 1, 2025
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
13 changes: 9 additions & 4 deletions elasticsearch/dsl/aggs.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

from elastic_transport.client_utils import DEFAULT

from . import wrappers
from .query import Query
from .response.aggs import AggResponse, BucketData, FieldBucketData, TopHitsData
from .utils import _R, AttrDict, DslBase
Expand Down Expand Up @@ -761,7 +762,7 @@ def __init__(
*,
after: Union[
Mapping[
Union[str, "InstrumentedField"], Union[int, float, str, bool, None, Any]
Union[str, "InstrumentedField"], Union[int, float, str, bool, None]
],
"DefaultType",
] = DEFAULT,
Expand Down Expand Up @@ -958,7 +959,7 @@ def __init__(
format: Union[str, "DefaultType"] = DEFAULT,
missing: Union[str, int, float, bool, "DefaultType"] = DEFAULT,
ranges: Union[
Sequence["types.DateRangeExpression"],
Sequence["wrappers.AggregationRange"],
Sequence[Dict[str, Any]],
"DefaultType",
] = DEFAULT,
Expand Down Expand Up @@ -1347,7 +1348,9 @@ def __init__(
"DefaultType",
] = DEFAULT,
ranges: Union[
Sequence["types.AggregationRange"], Sequence[Dict[str, Any]], "DefaultType"
Sequence["wrappers.AggregationRange"],
Sequence[Dict[str, Any]],
"DefaultType",
] = DEFAULT,
unit: Union[
Literal["in", "ft", "yd", "mi", "nmi", "km", "m", "cm", "mm"], "DefaultType"
Expand Down Expand Up @@ -2657,7 +2660,9 @@ def __init__(
field: Union[str, "InstrumentedField", "DefaultType"] = DEFAULT,
missing: Union[int, "DefaultType"] = DEFAULT,
ranges: Union[
Sequence["types.AggregationRange"], Sequence[Dict[str, Any]], "DefaultType"
Sequence["wrappers.AggregationRange"],
Sequence[Dict[str, Any]],
"DefaultType",
] = DEFAULT,
script: Union["types.Script", Dict[str, Any], "DefaultType"] = DEFAULT,
keyed: Union[bool, "DefaultType"] = DEFAULT,
Expand Down
6 changes: 3 additions & 3 deletions elasticsearch/dsl/faceted_search_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from .response.aggs import BucketData
from .search_base import SearchBase

FilterValueType = Union[str, datetime, Sequence[str]]
FilterValueType = Union[str, int, float, bool]

__all__ = [
"FacetedSearchBase",
Expand Down Expand Up @@ -396,10 +396,10 @@ def add_filter(
]

# remember the filter values for use in FacetedResponse
self.filter_values[name] = filter_values # type: ignore[assignment]
self.filter_values[name] = filter_values

# get the filter from the facet
f = self.facets[name].add_filter(filter_values) # type: ignore[arg-type]
f = self.facets[name].add_filter(filter_values)
if f is None:
return

Expand Down
157 changes: 156 additions & 1 deletion elasticsearch/dsl/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,9 @@ def __init__(
doc_class: Union[Type["InnerDoc"], "DefaultType"] = DEFAULT,
*args: Any,
enabled: Union[bool, "DefaultType"] = DEFAULT,
subobjects: Union[bool, "DefaultType"] = DEFAULT,
subobjects: Union[
Literal["true", "false", "auto"], bool, "DefaultType"
] = DEFAULT,
copy_to: Union[
Union[str, "InstrumentedField"],
Sequence[Union[str, "InstrumentedField"]],
Expand Down Expand Up @@ -762,6 +764,11 @@ class Boolean(Field):
:arg fielddata:
:arg index:
:arg null_value:
:arg ignore_malformed:
:arg script:
:arg on_script_error:
:arg time_series_dimension: For internal use by Elastic only. Marks
the field as a time series dimension. Defaults to false.
:arg doc_values:
:arg copy_to:
:arg store:
Expand Down Expand Up @@ -789,6 +796,10 @@ def __init__(
] = DEFAULT,
index: Union[bool, "DefaultType"] = DEFAULT,
null_value: Union[bool, "DefaultType"] = DEFAULT,
ignore_malformed: Union[bool, "DefaultType"] = DEFAULT,
script: Union["types.Script", Dict[str, Any], "DefaultType"] = DEFAULT,
on_script_error: Union[Literal["fail", "continue"], "DefaultType"] = DEFAULT,
time_series_dimension: Union[bool, "DefaultType"] = DEFAULT,
doc_values: Union[bool, "DefaultType"] = DEFAULT,
copy_to: Union[
Union[str, "InstrumentedField"],
Expand Down Expand Up @@ -816,6 +827,14 @@ def __init__(
kwargs["index"] = index
if null_value is not DEFAULT:
kwargs["null_value"] = null_value
if ignore_malformed is not DEFAULT:
kwargs["ignore_malformed"] = ignore_malformed
if script is not DEFAULT:
kwargs["script"] = script
if on_script_error is not DEFAULT:
kwargs["on_script_error"] = on_script_error
if time_series_dimension is not DEFAULT:
kwargs["time_series_dimension"] = time_series_dimension
if doc_values is not DEFAULT:
kwargs["doc_values"] = doc_values
if copy_to is not DEFAULT:
Expand Down Expand Up @@ -1092,6 +1111,56 @@ def __init__(
super().__init__(*args, **kwargs)


class CountedKeyword(Field):
"""
:arg index:
:arg meta: Metadata about the field.
:arg properties:
:arg ignore_above:
:arg dynamic:
:arg fields:
:arg synthetic_source_keep:
"""

name = "counted_keyword"
_param_defs = {
"properties": {"type": "field", "hash": True},
"fields": {"type": "field", "hash": True},
}

def __init__(
self,
*args: Any,
index: Union[bool, "DefaultType"] = DEFAULT,
meta: Union[Mapping[str, str], "DefaultType"] = DEFAULT,
properties: Union[Mapping[str, Field], "DefaultType"] = DEFAULT,
ignore_above: Union[int, "DefaultType"] = DEFAULT,
dynamic: Union[
Literal["strict", "runtime", "true", "false"], bool, "DefaultType"
] = DEFAULT,
fields: Union[Mapping[str, Field], "DefaultType"] = DEFAULT,
synthetic_source_keep: Union[
Literal["none", "arrays", "all"], "DefaultType"
] = DEFAULT,
**kwargs: Any,
):
if index is not DEFAULT:
kwargs["index"] = index
if meta is not DEFAULT:
kwargs["meta"] = meta
if properties is not DEFAULT:
kwargs["properties"] = properties
if ignore_above is not DEFAULT:
kwargs["ignore_above"] = ignore_above
if dynamic is not DEFAULT:
kwargs["dynamic"] = dynamic
if fields is not DEFAULT:
kwargs["fields"] = fields
if synthetic_source_keep is not DEFAULT:
kwargs["synthetic_source_keep"] = synthetic_source_keep
super().__init__(*args, **kwargs)


class Date(Field):
"""
:arg default_timezone: timezone that will be automatically used for tz-naive values
Expand All @@ -1101,6 +1170,8 @@ class Date(Field):
:arg format:
:arg ignore_malformed:
:arg index:
:arg script:
:arg on_script_error:
:arg null_value:
:arg precision_step:
:arg locale:
Expand Down Expand Up @@ -1133,6 +1204,8 @@ def __init__(
format: Union[str, "DefaultType"] = DEFAULT,
ignore_malformed: Union[bool, "DefaultType"] = DEFAULT,
index: Union[bool, "DefaultType"] = DEFAULT,
script: Union["types.Script", Dict[str, Any], "DefaultType"] = DEFAULT,
on_script_error: Union[Literal["fail", "continue"], "DefaultType"] = DEFAULT,
null_value: Any = DEFAULT,
precision_step: Union[int, "DefaultType"] = DEFAULT,
locale: Union[str, "DefaultType"] = DEFAULT,
Expand Down Expand Up @@ -1165,6 +1238,10 @@ def __init__(
kwargs["ignore_malformed"] = ignore_malformed
if index is not DEFAULT:
kwargs["index"] = index
if script is not DEFAULT:
kwargs["script"] = script
if on_script_error is not DEFAULT:
kwargs["on_script_error"] = on_script_error
if null_value is not DEFAULT:
kwargs["null_value"] = null_value
if precision_step is not DEFAULT:
Expand Down Expand Up @@ -1229,6 +1306,8 @@ class DateNanos(Field):
:arg format:
:arg ignore_malformed:
:arg index:
:arg script:
:arg on_script_error:
:arg null_value:
:arg precision_step:
:arg doc_values:
Expand All @@ -1255,6 +1334,8 @@ def __init__(
format: Union[str, "DefaultType"] = DEFAULT,
ignore_malformed: Union[bool, "DefaultType"] = DEFAULT,
index: Union[bool, "DefaultType"] = DEFAULT,
script: Union["types.Script", Dict[str, Any], "DefaultType"] = DEFAULT,
on_script_error: Union[Literal["fail", "continue"], "DefaultType"] = DEFAULT,
null_value: Any = DEFAULT,
precision_step: Union[int, "DefaultType"] = DEFAULT,
doc_values: Union[bool, "DefaultType"] = DEFAULT,
Expand Down Expand Up @@ -1284,6 +1365,10 @@ def __init__(
kwargs["ignore_malformed"] = ignore_malformed
if index is not DEFAULT:
kwargs["index"] = index
if script is not DEFAULT:
kwargs["script"] = script
if on_script_error is not DEFAULT:
kwargs["on_script_error"] = on_script_error
if null_value is not DEFAULT:
kwargs["null_value"] = null_value
if precision_step is not DEFAULT:
Expand Down Expand Up @@ -3068,6 +3153,76 @@ def __init__(
super().__init__(*args, **kwargs)


class Passthrough(Field):
"""
:arg enabled:
:arg priority:
:arg time_series_dimension:
:arg copy_to:
:arg store:
:arg meta: Metadata about the field.
:arg properties:
:arg ignore_above:
:arg dynamic:
:arg fields:
:arg synthetic_source_keep:
"""

name = "passthrough"
_param_defs = {
"properties": {"type": "field", "hash": True},
"fields": {"type": "field", "hash": True},
}

def __init__(
self,
*args: Any,
enabled: Union[bool, "DefaultType"] = DEFAULT,
priority: Union[int, "DefaultType"] = DEFAULT,
time_series_dimension: Union[bool, "DefaultType"] = DEFAULT,
copy_to: Union[
Union[str, "InstrumentedField"],
Sequence[Union[str, "InstrumentedField"]],
"DefaultType",
] = DEFAULT,
store: Union[bool, "DefaultType"] = DEFAULT,
meta: Union[Mapping[str, str], "DefaultType"] = DEFAULT,
properties: Union[Mapping[str, Field], "DefaultType"] = DEFAULT,
ignore_above: Union[int, "DefaultType"] = DEFAULT,
dynamic: Union[
Literal["strict", "runtime", "true", "false"], bool, "DefaultType"
] = DEFAULT,
fields: Union[Mapping[str, Field], "DefaultType"] = DEFAULT,
synthetic_source_keep: Union[
Literal["none", "arrays", "all"], "DefaultType"
] = DEFAULT,
**kwargs: Any,
):
if enabled is not DEFAULT:
kwargs["enabled"] = enabled
if priority is not DEFAULT:
kwargs["priority"] = priority
if time_series_dimension is not DEFAULT:
kwargs["time_series_dimension"] = time_series_dimension
if copy_to is not DEFAULT:
kwargs["copy_to"] = str(copy_to)
if store is not DEFAULT:
kwargs["store"] = store
if meta is not DEFAULT:
kwargs["meta"] = meta
if properties is not DEFAULT:
kwargs["properties"] = properties
if ignore_above is not DEFAULT:
kwargs["ignore_above"] = ignore_above
if dynamic is not DEFAULT:
kwargs["dynamic"] = dynamic
if fields is not DEFAULT:
kwargs["fields"] = fields
if synthetic_source_keep is not DEFAULT:
kwargs["synthetic_source_keep"] = synthetic_source_keep
super().__init__(*args, **kwargs)


class Percolator(Field):
"""
:arg meta: Metadata about the field.
Expand Down
8 changes: 7 additions & 1 deletion elasticsearch/dsl/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,8 @@ class Knn(Query):
:arg filter: Filters for the kNN search query
:arg similarity: The minimum similarity for a vector to be considered
a match
:arg rescore_vector: Apply oversampling and rescoring to quantized
vectors *
:arg boost: Floating point number used to decrease or increase the
relevance scores of the query. Boost values are relative to the
default value of 1.0. A boost value between 0 and 1.0 decreases
Expand All @@ -1108,6 +1110,9 @@ def __init__(
k: Union[int, "DefaultType"] = DEFAULT,
filter: Union[Query, Sequence[Query], "DefaultType"] = DEFAULT,
similarity: Union[float, "DefaultType"] = DEFAULT,
rescore_vector: Union[
"types.RescoreVector", Dict[str, Any], "DefaultType"
] = DEFAULT,
boost: Union[float, "DefaultType"] = DEFAULT,
_name: Union[str, "DefaultType"] = DEFAULT,
**kwargs: Any,
Expand All @@ -1120,6 +1125,7 @@ def __init__(
k=k,
filter=filter,
similarity=similarity,
rescore_vector=rescore_vector,
boost=boost,
_name=_name,
**kwargs,
Expand Down Expand Up @@ -2650,7 +2656,7 @@ def __init__(
self,
_field: Union[str, "InstrumentedField", "DefaultType"] = DEFAULT,
_value: Union[
Sequence[Union[int, float, str, bool, None, Any]],
Sequence[Union[int, float, str, bool, None]],
"types.TermsLookup",
Dict[str, Any],
"DefaultType",
Expand Down
Loading