Skip to content

Commit bacfc74

Browse files
Autogenerate Query classes (#1890)
* 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 <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * Update utils/generator.py Co-authored-by: Quentin Pradet <[email protected]> * Update utils/generator.py Co-authored-by: Quentin Pradet <[email protected]> * final round of review improvements --------- Co-authored-by: Quentin Pradet <[email protected]>
1 parent 4ccf20e commit bacfc74

16 files changed

+6964
-253
lines changed

Diff for: elasticsearch_dsl/faceted_search_base.py

+13-19
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ class TermsFacet(Facet[_R]):
141141
def add_filter(self, filter_values: List[FilterValueType]) -> Optional[Query]:
142142
"""Create a terms filter instead of bool containing term filters."""
143143
if filter_values:
144-
return Terms(
145-
_expand__to_dot=False, **{self._params["field"]: filter_values}
146-
)
144+
return Terms(self._params["field"], filter_values, _expand__to_dot=False)
147145
return None
148146

149147

@@ -173,27 +171,26 @@ def __init__(
173171

174172
def get_value_filter(self, filter_value: FilterValueType) -> Query:
175173
f, t = self._ranges[filter_value]
176-
limits = {}
174+
limits: Dict[str, Any] = {}
177175
if f is not None:
178176
limits["gte"] = f
179177
if t is not None:
180178
limits["lt"] = t
181179

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

184182

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

188186
def get_value_filter(self, filter_value: FilterValueType) -> Range:
189187
return Range(
190-
_expand__to_dot=False,
191-
**{
192-
self._params["field"]: {
193-
"gte": filter_value,
194-
"lt": filter_value + self._params["interval"],
195-
}
188+
self._params["field"],
189+
{
190+
"gte": filter_value,
191+
"lt": filter_value + self._params["interval"],
196192
},
193+
_expand__to_dot=False,
197194
)
198195

199196

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

260257
return Range(
261-
_expand__to_dot=False,
262-
**{
263-
self._params["field"]: {
264-
"gte": filter_value,
265-
"lt": self.DATE_INTERVALS[self._params[interval_type]](
266-
filter_value
267-
),
268-
}
258+
self._params["field"],
259+
{
260+
"gte": filter_value,
261+
"lt": self.DATE_INTERVALS[self._params[interval_type]](filter_value),
269262
},
263+
_expand__to_dot=False,
270264
)
271265

272266

Diff for: elasticsearch_dsl/function.py

+44-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,20 @@
1717

1818
import collections.abc
1919
from copy import deepcopy
20-
from typing import Any, ClassVar, Dict, MutableMapping, Optional, Union, overload
20+
from typing import (
21+
Any,
22+
ClassVar,
23+
Dict,
24+
Literal,
25+
MutableMapping,
26+
Optional,
27+
Union,
28+
overload,
29+
)
2130

22-
from .utils import DslBase
31+
from elastic_transport.client_utils import DEFAULT, DefaultType
32+
33+
from .utils import AttrDict, DslBase
2334

2435

2536
@overload
@@ -123,10 +134,14 @@ class RandomScore(ScoreFunction):
123134
name = "random_score"
124135

125136

126-
class FieldValueFactor(ScoreFunction):
137+
class FieldValueFactorScore(ScoreFunction):
127138
name = "field_value_factor"
128139

129140

141+
class FieldValueFactor(FieldValueFactorScore): # alias of the above
142+
pass
143+
144+
130145
class Linear(ScoreFunction):
131146
name = "linear"
132147

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

138153
class Exp(ScoreFunction):
139154
name = "exp"
155+
156+
157+
class DecayFunction(AttrDict[Any]):
158+
def __init__(
159+
self,
160+
*,
161+
decay: Union[float, "DefaultType"] = DEFAULT,
162+
offset: Any = DEFAULT,
163+
scale: Any = DEFAULT,
164+
origin: Any = DEFAULT,
165+
multi_value_mode: Union[
166+
Literal["min", "max", "avg", "sum"], "DefaultType"
167+
] = DEFAULT,
168+
**kwargs: Any,
169+
):
170+
if decay != DEFAULT:
171+
kwargs["decay"] = decay
172+
if offset != DEFAULT:
173+
kwargs["offset"] = offset
174+
if scale != DEFAULT:
175+
kwargs["scale"] = scale
176+
if origin != DEFAULT:
177+
kwargs["origin"] = origin
178+
if multi_value_mode != DEFAULT:
179+
kwargs["multi_value_mode"] = multi_value_mode
180+
super().__init__(kwargs)

0 commit comments

Comments
 (0)