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 Query classes #1917

Merged
merged 1 commit into from
Oct 7, 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
32 changes: 13 additions & 19 deletions elasticsearch_dsl/faceted_search_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,7 @@ class TermsFacet(Facet[_R]):
def add_filter(self, filter_values: List[FilterValueType]) -> Optional[Query]:
"""Create a terms filter instead of bool containing term filters."""
if filter_values:
return Terms(
_expand__to_dot=False, **{self._params["field"]: filter_values}
)
return Terms(self._params["field"], filter_values, _expand__to_dot=False)
return None


Expand Down Expand Up @@ -173,27 +171,26 @@ def __init__(

def get_value_filter(self, filter_value: FilterValueType) -> Query:
f, t = self._ranges[filter_value]
limits = {}
limits: Dict[str, Any] = {}
if f is not None:
limits["gte"] = f
if t is not None:
limits["lt"] = t

return Range(_expand__to_dot=False, **{self._params["field"]: limits})
return Range(self._params["field"], limits, _expand__to_dot=False)


class HistogramFacet(Facet[_R]):
agg_type = "histogram"

def get_value_filter(self, filter_value: FilterValueType) -> Range:
return Range(
_expand__to_dot=False,
**{
self._params["field"]: {
"gte": filter_value,
"lt": filter_value + self._params["interval"],
}
self._params["field"],
{
"gte": filter_value,
"lt": filter_value + self._params["interval"],
},
_expand__to_dot=False,
)


Expand Down Expand Up @@ -258,15 +255,12 @@ def get_value_filter(self, filter_value: Any) -> Range:
interval_type = "interval"

return Range(
_expand__to_dot=False,
**{
self._params["field"]: {
"gte": filter_value,
"lt": self.DATE_INTERVALS[self._params[interval_type]](
filter_value
),
}
self._params["field"],
{
"gte": filter_value,
"lt": self.DATE_INTERVALS[self._params[interval_type]](filter_value),
},
_expand__to_dot=False,
)


Expand Down
47 changes: 44 additions & 3 deletions elasticsearch_dsl/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,20 @@

import collections.abc
from copy import deepcopy
from typing import Any, ClassVar, Dict, MutableMapping, Optional, Union, overload
from typing import (
Any,
ClassVar,
Dict,
Literal,
MutableMapping,
Optional,
Union,
overload,
)

from .utils import DslBase
from elastic_transport.client_utils import DEFAULT, DefaultType

from .utils import AttrDict, DslBase


@overload
Expand Down Expand Up @@ -123,10 +134,14 @@ class RandomScore(ScoreFunction):
name = "random_score"


class FieldValueFactor(ScoreFunction):
class FieldValueFactorScore(ScoreFunction):
name = "field_value_factor"


class FieldValueFactor(FieldValueFactorScore): # alias of the above
pass


class Linear(ScoreFunction):
name = "linear"

Expand All @@ -137,3 +152,29 @@ class Gauss(ScoreFunction):

class Exp(ScoreFunction):
name = "exp"


class DecayFunction(AttrDict[Any]):
def __init__(
self,
*,
decay: Union[float, "DefaultType"] = DEFAULT,
offset: Any = DEFAULT,
scale: Any = DEFAULT,
origin: Any = DEFAULT,
multi_value_mode: Union[
Literal["min", "max", "avg", "sum"], "DefaultType"
] = DEFAULT,
**kwargs: Any,
):
if decay != DEFAULT:
kwargs["decay"] = decay
if offset != DEFAULT:
kwargs["offset"] = offset
if scale != DEFAULT:
kwargs["scale"] = scale
if origin != DEFAULT:
kwargs["origin"] = origin
if multi_value_mode != DEFAULT:
kwargs["multi_value_mode"] = multi_value_mode
super().__init__(kwargs)
Loading
Loading