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

Autogenerate Query classes #1890

Merged
merged 27 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
524bd9c
Autogenerate Query classes
miguelgrinberg Aug 28, 2024
47f0e4d
handle Python reserved keywords such as from
miguelgrinberg Aug 28, 2024
2d581cf
replace long deprecated 'filtered' query from tests
miguelgrinberg Aug 28, 2024
3992d9f
minor code generation updates
miguelgrinberg Aug 28, 2024
3aa2430
more code generation updates
miguelgrinberg Aug 30, 2024
33f2a7f
address typing issues
miguelgrinberg Sep 2, 2024
a4760b3
clean up code generation templates
miguelgrinberg Sep 2, 2024
44dcd87
more code generator cleanup
miguelgrinberg Sep 3, 2024
ffbbe1f
add a unit test using some of the generated classes
miguelgrinberg Sep 3, 2024
2118bd8
no need to "reset" the interface list
miguelgrinberg Sep 5, 2024
8a227a8
use the transport's DEFAULT type
miguelgrinberg Sep 5, 2024
81109e3
include inherited properties in docstrings and constructors
miguelgrinberg Sep 5, 2024
9766538
support legacy FieldValueFactor name for FieldValueFactorScore
miguelgrinberg Sep 5, 2024
984993c
Update utils/generator.py
miguelgrinberg Sep 9, 2024
55e3fc8
use the identity operator to check for defaults
miguelgrinberg Sep 9, 2024
7c81263
rename interfaces.py to types.py
miguelgrinberg Sep 9, 2024
819b79b
leave undocumented classes and attributes with an empty docstring
miguelgrinberg Sep 9, 2024
d6fc3f0
add unit test for AttrDict with from reserved keyword
miguelgrinberg Sep 9, 2024
67abf79
add a dependency on the transport library
miguelgrinberg Sep 9, 2024
ef3cc7d
Update utils/generator.py
miguelgrinberg Sep 9, 2024
1c43dd2
list required arguments first in types.py
miguelgrinberg Sep 10, 2024
4977f9f
add server defaults to argument docstrings
miguelgrinberg Sep 10, 2024
97643e1
remove unnecessary quotes from type hints in types.py
miguelgrinberg Sep 10, 2024
2dc5a23
Update utils/templates/types.py.tpl
miguelgrinberg Sep 11, 2024
937ddf1
Update utils/generator.py
miguelgrinberg Sep 11, 2024
bb23d01
Update utils/generator.py
miguelgrinberg Sep 11, 2024
0a17b26
final round of review improvements
miguelgrinberg Sep 11, 2024
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})
pquentin marked this conversation as resolved.
Show resolved Hide resolved
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
miguelgrinberg marked this conversation as resolved.
Show resolved Hide resolved
from elastic_transport.client_utils import DEFAULT, DefaultType
miguelgrinberg marked this conversation as resolved.
Show resolved Hide resolved

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]):
pquentin marked this conversation as resolved.
Show resolved Hide resolved
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:
miguelgrinberg marked this conversation as resolved.
Show resolved Hide resolved
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