Skip to content

Commit 168ddef

Browse files
unit tests
1 parent 7f6dc00 commit 168ddef

File tree

4 files changed

+667
-731
lines changed

4 files changed

+667
-731
lines changed

elasticsearch/dsl/document_base.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ def __mod__(self, value: Any) -> "InstrumentedExpression":
123123
def __rmod__(self, value: Any) -> "InstrumentedExpression":
124124
return InstrumentedExpression(f"{json.dumps(value)} % {self._expr}")
125125

126+
def where(self, expr: "InstrumentedExpression") -> "InstrumentedExpression":
127+
"""Add a condition to be met for the row to be included.
128+
129+
Use only in expressions given in the ``STATS`` command.
130+
"""
131+
return InstrumentedExpression(f"{self._expr} WHERE {json.dumps(expr)}")
126132

127133
class InstrumentedField(InstrumentedExpression):
128134
"""Proxy object for a mapped document field.
@@ -170,15 +176,31 @@ def __neg__(self) -> str: # type: ignore[override]
170176
return f"-{self._expr}"
171177

172178
def asc(self) -> "InstrumentedField":
179+
"""Return the field name representation for ascending sort order.
180+
181+
For use in ES|QL queries only.
182+
"""
173183
return InstrumentedField(f"{self._expr} ASC", None)
174184

175185
def desc(self) -> "InstrumentedField":
186+
"""Return the field name representation for descending sort order.
187+
188+
For use in ES|QL queries only.
189+
"""
176190
return InstrumentedField(f"{self._expr} DESC", None)
177191

178192
def nulls_first(self) -> "InstrumentedField":
193+
"""Return the field name representation for nulls first sort order.
194+
195+
For use in ES|QL queries only.
196+
"""
179197
return InstrumentedField(f"{self._expr} NULLS FIRST", None)
180198

181199
def nulls_last(self) -> "InstrumentedField":
200+
"""Return the field name representation for nulls last sort order.
201+
202+
For use in ES|QL queries only.
203+
"""
182204
return InstrumentedField(f"{self._expr} NULLS LAST", None)
183205

184206
def __str__(self) -> str:

elasticsearch/esql/esql.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from abc import ABC, abstractmethod
2020
from typing import Any, Dict, Optional, Tuple, Type
2121

22-
from ..dsl.document_base import DocumentBase, InstrumentedField
22+
from ..dsl.document_base import DocumentBase, InstrumentedField, InstrumentedExpression
2323

2424
FieldType = InstrumentedField | str
2525
IndexType = Type[DocumentBase] | str
@@ -537,11 +537,11 @@ class Row(ESQLBase):
537537

538538
def __init__(self, **params: ExpressionType):
539539
super().__init__()
540-
self._params = params
540+
self._params = {k: json.dumps(v) if not isinstance(v, InstrumentedExpression) else v for k, v in params.items()}
541541

542542
def _render_internal(self) -> str:
543543
return "ROW " + ", ".join(
544-
[f"{k} = {json.dumps(v)}" for k, v in self._params.items()]
544+
[f"{k} = {v}" for k, v in self._params.items()]
545545
)
546546

547547

0 commit comments

Comments
 (0)