Skip to content

Commit

Permalink
Autogenerate Query classes (#1890) (#1917)
Browse files Browse the repository at this point in the history
* Autogenerate Query classes

* handle Python reserved keywords such as from

* replace long deprecated 'filtered' query from tests

* minor code generation updates

* more code generation updates

* address typing issues

* clean up code generation templates

* more code generator cleanup

* add a unit test using some of the generated classes

* no need to "reset" the interface list

* use the transport's DEFAULT type

* include inherited properties in docstrings and constructors

* support legacy FieldValueFactor name for FieldValueFactorScore

* Update utils/generator.py

Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>

* use the identity operator to check for defaults

* rename interfaces.py to types.py

* leave undocumented classes and attributes with an empty docstring

* add unit test for AttrDict with from reserved keyword

* add a dependency on the transport library

* Update utils/generator.py

Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>

* list required arguments first in types.py

* add server defaults to argument docstrings

* remove unnecessary quotes from type hints in types.py

* Update utils/templates/types.py.tpl

Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>

* Update utils/generator.py

Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>

* Update utils/generator.py

Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>

* final round of review improvements

---------

Co-authored-by: Quentin Pradet <quentin.pradet@gmail.com>
(cherry picked from commit bacfc74)

Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
  • Loading branch information
github-actions[bot] and miguelgrinberg authored Oct 7, 2024
1 parent 1705390 commit ee1dc8f
Show file tree
Hide file tree
Showing 16 changed files with 6,964 additions and 253 deletions.
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

0 comments on commit ee1dc8f

Please sign in to comment.