Skip to content

Commit b81115d

Browse files
add new ES|QL functions
1 parent 8666fd5 commit b81115d

File tree

2 files changed

+89
-1
lines changed

2 files changed

+89
-1
lines changed

elasticsearch/esql/esql.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,7 +1116,7 @@ def __init__(self, parent: ESQLBase, method: Optional[str] = None):
11161116
super().__init__(parent)
11171117
self.method = method
11181118
self.by_columns: List[FieldType] = []
1119-
self.options = {}
1119+
self.options: Dict[str, Any] = {}
11201120

11211121
def by(self, *columns: FieldType) -> "Fuse":
11221122
self.by_columns += list(columns)

elasticsearch/esql/functions.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ def abs(number: ExpressionType) -> InstrumentedExpression:
3838
return InstrumentedExpression(f"ABS({_render(number)})")
3939

4040

41+
def absent(field: ExpressionType) -> InstrumentedExpression:
42+
"""Returns true if the input expression yields no non-null values within the
43+
current aggregation context.
44+
45+
:param field: Expression that outputs values to be checked for absence.
46+
"""
47+
return InstrumentedExpression(f"ABSENT({_render(field)})")
48+
49+
50+
def absent_over_time(field: ExpressionType) -> InstrumentedExpression:
51+
"""Calculates the absence of a field in the output result over time range."""
52+
return InstrumentedExpression(f"ABSENT_OVER_TIME({_render(field)})")
53+
54+
4155
def acos(number: ExpressionType) -> InstrumentedExpression:
4256
"""Returns the arccosine of `n` as an angle, expressed in radians.
4357
@@ -364,6 +378,11 @@ def exp(number: ExpressionType) -> InstrumentedExpression:
364378
return InstrumentedExpression(f"EXP({_render(number)})")
365379

366380

381+
def first(value: ExpressionType, sort: ExpressionType) -> InstrumentedExpression:
382+
"""Calculates the earliest value of a field."""
383+
return InstrumentedExpression(f"FIRST({_render(value)}, {_render(sort)})")
384+
385+
367386
def first_over_time(field: ExpressionType) -> InstrumentedExpression:
368387
"""The earliest value of a field, where recency determined by the
369388
`@timestamp` field.
@@ -463,6 +482,11 @@ def kql(query: ExpressionType) -> InstrumentedExpression:
463482
return InstrumentedExpression(f"KQL({_render(query)})")
464483

465484

485+
def last(value: ExpressionType, sort: ExpressionType) -> InstrumentedExpression:
486+
"""Calculates the latest value of a field."""
487+
return InstrumentedExpression(f"LAST({_render(value)}, {_render(sort)})")
488+
489+
466490
def last_over_time(field: ExpressionType) -> InstrumentedExpression:
467491
"""The latest value of a field, where recency determined by the
468492
`@timestamp` field.
@@ -697,6 +721,18 @@ def mv_concat(string: ExpressionType, delim: ExpressionType) -> InstrumentedExpr
697721
return InstrumentedExpression(f"MV_CONCAT({_render(string)}, {_render(delim)})")
698722

699723

724+
def mv_contains(
725+
superset: ExpressionType, subset: ExpressionType
726+
) -> InstrumentedExpression:
727+
"""Checks if all values yielded by the second multivalue expression are present in the
728+
values yielded by the first multivalue expression. Returns a boolean. Null values are
729+
treated as an empty set.
730+
"""
731+
return InstrumentedExpression(
732+
f"MV_CONTAINS({_render(superset)}, {_render(subset)})"
733+
)
734+
735+
700736
def mv_count(field: ExpressionType) -> InstrumentedExpression:
701737
"""Converts a multivalued expression into a single valued column containing
702738
a count of the number of values.
@@ -894,6 +930,18 @@ def pow(base: ExpressionType, exponent: ExpressionType) -> InstrumentedExpressio
894930
return InstrumentedExpression(f"POW({_render(base)}, {_render(exponent)})")
895931

896932

933+
def present(field: ExpressionType) -> InstrumentedExpression:
934+
"""Returns true if the input expression yields any non-null values within the current
935+
aggregation context. Otherwise it returns false.
936+
"""
937+
return InstrumentedExpression(f"PRESENT({_render(field)})")
938+
939+
940+
def present_over_time(field: ExpressionType) -> InstrumentedExpression:
941+
"""Calculates the presence of a field in the output result over time range."""
942+
return InstrumentedExpression(f"PRESENT_OVER_TIME({_render(field)})")
943+
944+
897945
def qstr(
898946
query: ExpressionType, options: ExpressionType = None
899947
) -> InstrumentedExpression:
@@ -1452,6 +1500,11 @@ def sum(number: ExpressionType) -> InstrumentedExpression:
14521500
return InstrumentedExpression(f"SUM({_render(number)})")
14531501

14541502

1503+
def sum_over_time(field: ExpressionType) -> InstrumentedExpression:
1504+
"""Calculates the sum over time value of a field."""
1505+
return InstrumentedExpression(f"SUM({_render(field)})")
1506+
1507+
14551508
def tan(angle: ExpressionType) -> InstrumentedExpression:
14561509
"""Returns the tangent of an angle.
14571510
@@ -1483,6 +1536,17 @@ def term(field: ExpressionType, query: ExpressionType) -> InstrumentedExpression
14831536
return InstrumentedExpression(f"TERM({_render(field)}, {_render(query)})")
14841537

14851538

1539+
def text_embedding(
1540+
text: ExpressionType, inference_id: ExpressionType
1541+
) -> InstrumentedExpression:
1542+
"""Generates dense vector embeddings from text input using a specified inference endpoint.
1543+
Use this function to generate query vectors for KNN searches against your vectorized data
1544+
or others dense vector based operations."""
1545+
return InstrumentedExpression(
1546+
f"TEXT_EMBEDDING({_render(text)}, {_render(inference_id)})"
1547+
)
1548+
1549+
14861550
def top(
14871551
field: ExpressionType, limit: ExpressionType, order: ExpressionType
14881552
) -> InstrumentedExpression:
@@ -1596,6 +1660,22 @@ def to_double(field: ExpressionType) -> InstrumentedExpression:
15961660
return InstrumentedExpression(f"TO_DOUBLE({_render(field)})")
15971661

15981662

1663+
def to_geohash(field: ExpressionType) -> InstrumentedExpression:
1664+
"""Converts an input value to a geohash value. A string will only be successfully
1665+
converted if it respects the geohash format, as described for the geohash grid
1666+
aggregation.
1667+
"""
1668+
return InstrumentedExpression(f"TO_GEOHASH({_render(field)})")
1669+
1670+
1671+
def to_geohex(field: ExpressionType) -> InstrumentedExpression:
1672+
"""Converts an input value to a geohex value. A string will only be successfully
1673+
converted if it respects the geohex format, as described for the geohex grid
1674+
aggregation.
1675+
"""
1676+
return InstrumentedExpression(f"TO_GEOHEX({_render(field)})")
1677+
1678+
15991679
def to_geopoint(field: ExpressionType) -> InstrumentedExpression:
16001680
"""Converts an input value to a `geo_point` value. A string will only be
16011681
successfully converted if it respects the WKT Point format.
@@ -1616,6 +1696,14 @@ def to_geoshape(field: ExpressionType) -> InstrumentedExpression:
16161696
return InstrumentedExpression(f"TO_GEOSHAPE({_render(field)})")
16171697

16181698

1699+
def to_geotile(field: ExpressionType) -> InstrumentedExpression:
1700+
"""Converts an input value to a geotile value. A string will only be successfully
1701+
converted if it respects the geotile format, as described for the geotile grid
1702+
aggregation.
1703+
"""
1704+
return InstrumentedExpression(f"TO_GEOTILE({_render(field)})")
1705+
1706+
16191707
def to_integer(field: ExpressionType) -> InstrumentedExpression:
16201708
"""Converts an input value to an integer value. If the input parameter is
16211709
of a date type, its value will be interpreted as milliseconds since the

0 commit comments

Comments
 (0)