elasticsearch 9.0.2__py3-none-any.whl → 9.0.4__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- elasticsearch/_async/client/__init__.py +59 -202
- elasticsearch/_async/client/cat.py +1011 -59
- elasticsearch/_async/client/cluster.py +14 -4
- elasticsearch/_async/client/eql.py +10 -2
- elasticsearch/_async/client/esql.py +33 -10
- elasticsearch/_async/client/indices.py +88 -44
- elasticsearch/_async/client/inference.py +108 -3
- elasticsearch/_async/client/ingest.py +0 -7
- elasticsearch/_async/client/license.py +4 -4
- elasticsearch/_async/client/ml.py +6 -17
- elasticsearch/_async/client/monitoring.py +1 -1
- elasticsearch/_async/client/rollup.py +1 -22
- elasticsearch/_async/client/security.py +11 -17
- elasticsearch/_async/client/snapshot.py +6 -0
- elasticsearch/_async/client/sql.py +1 -1
- elasticsearch/_async/client/synonyms.py +1 -0
- elasticsearch/_async/client/transform.py +60 -0
- elasticsearch/_async/client/watcher.py +4 -2
- elasticsearch/_sync/client/__init__.py +59 -202
- elasticsearch/_sync/client/cat.py +1011 -59
- elasticsearch/_sync/client/cluster.py +14 -4
- elasticsearch/_sync/client/eql.py +10 -2
- elasticsearch/_sync/client/esql.py +33 -10
- elasticsearch/_sync/client/indices.py +88 -44
- elasticsearch/_sync/client/inference.py +108 -3
- elasticsearch/_sync/client/ingest.py +0 -7
- elasticsearch/_sync/client/license.py +4 -4
- elasticsearch/_sync/client/ml.py +6 -17
- elasticsearch/_sync/client/monitoring.py +1 -1
- elasticsearch/_sync/client/rollup.py +1 -22
- elasticsearch/_sync/client/security.py +11 -17
- elasticsearch/_sync/client/snapshot.py +6 -0
- elasticsearch/_sync/client/sql.py +1 -1
- elasticsearch/_sync/client/synonyms.py +1 -0
- elasticsearch/_sync/client/transform.py +60 -0
- elasticsearch/_sync/client/watcher.py +4 -2
- elasticsearch/_version.py +1 -1
- elasticsearch/compat.py +5 -0
- elasticsearch/dsl/__init__.py +2 -1
- elasticsearch/dsl/_async/document.py +84 -0
- elasticsearch/dsl/_sync/document.py +84 -0
- elasticsearch/dsl/document_base.py +219 -16
- elasticsearch/dsl/field.py +245 -57
- elasticsearch/dsl/query.py +7 -4
- elasticsearch/dsl/response/aggs.py +1 -1
- elasticsearch/dsl/types.py +125 -88
- elasticsearch/dsl/utils.py +2 -2
- elasticsearch/{dsl/_sync/_sync_check → esql}/__init__.py +3 -0
- elasticsearch/esql/esql.py +1156 -0
- elasticsearch/esql/functions.py +1750 -0
- {elasticsearch-9.0.2.dist-info → elasticsearch-9.0.4.dist-info}/METADATA +1 -3
- {elasticsearch-9.0.2.dist-info → elasticsearch-9.0.4.dist-info}/RECORD +55 -59
- elasticsearch/dsl/_sync/_sync_check/document.py +0 -514
- elasticsearch/dsl/_sync/_sync_check/faceted_search.py +0 -50
- elasticsearch/dsl/_sync/_sync_check/index.py +0 -597
- elasticsearch/dsl/_sync/_sync_check/mapping.py +0 -49
- elasticsearch/dsl/_sync/_sync_check/search.py +0 -230
- elasticsearch/dsl/_sync/_sync_check/update_by_query.py +0 -45
- {elasticsearch-9.0.2.dist-info → elasticsearch-9.0.4.dist-info}/WHEEL +0 -0
- {elasticsearch-9.0.2.dist-info → elasticsearch-9.0.4.dist-info}/licenses/LICENSE +0 -0
- {elasticsearch-9.0.2.dist-info → elasticsearch-9.0.4.dist-info}/licenses/NOTICE +0 -0
|
@@ -0,0 +1,1156 @@
|
|
|
1
|
+
# Licensed to Elasticsearch B.V. under one or more contributor
|
|
2
|
+
# license agreements. See the NOTICE file distributed with
|
|
3
|
+
# this work for additional information regarding copyright
|
|
4
|
+
# ownership. Elasticsearch B.V. licenses this file to you under
|
|
5
|
+
# the Apache License, Version 2.0 (the "License"); you may
|
|
6
|
+
# not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing,
|
|
12
|
+
# software distributed under the License is distributed on an
|
|
13
|
+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
14
|
+
# KIND, either express or implied. See the License for the
|
|
15
|
+
# specific language governing permissions and limitations
|
|
16
|
+
# under the License.
|
|
17
|
+
|
|
18
|
+
import json
|
|
19
|
+
import re
|
|
20
|
+
from abc import ABC, abstractmethod
|
|
21
|
+
from typing import Any, Dict, Optional, Tuple, Type, Union
|
|
22
|
+
|
|
23
|
+
from ..dsl.document_base import DocumentBase, InstrumentedExpression, InstrumentedField
|
|
24
|
+
|
|
25
|
+
FieldType = Union[InstrumentedField, str]
|
|
26
|
+
IndexType = Union[Type[DocumentBase], str]
|
|
27
|
+
ExpressionType = Any
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ESQL(ABC):
|
|
31
|
+
"""The static methods of the ``ESQL`` class provide access to the ES|QL source
|
|
32
|
+
commands, used to create ES|QL queries.
|
|
33
|
+
|
|
34
|
+
These methods return an instance of class ``ESQLBase``, which provides access to
|
|
35
|
+
the ES|QL processing commands.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def from_(*indices: IndexType) -> "From":
|
|
40
|
+
"""The ``FROM`` source command returns a table with data from a data stream, index, or alias.
|
|
41
|
+
|
|
42
|
+
:param indices: A list of indices, data streams or aliases. Supports wildcards and date math.
|
|
43
|
+
|
|
44
|
+
Examples::
|
|
45
|
+
|
|
46
|
+
query1 = ESQL.from_("employees")
|
|
47
|
+
query2 = ESQL.from_("<logs-{now/d}>")
|
|
48
|
+
query3 = ESQL.from_("employees-00001", "other-employees-*")
|
|
49
|
+
query4 = ESQL.from_("cluster_one:employees-00001", "cluster_two:other-employees-*")
|
|
50
|
+
query5 = ESQL.from_("employees").metadata("_id")
|
|
51
|
+
"""
|
|
52
|
+
return From(*indices)
|
|
53
|
+
|
|
54
|
+
@staticmethod
|
|
55
|
+
def row(**params: ExpressionType) -> "Row":
|
|
56
|
+
"""The ``ROW`` source command produces a row with one or more columns with values that you specify.
|
|
57
|
+
This can be useful for testing.
|
|
58
|
+
|
|
59
|
+
:param params: the column values to produce, given as keyword arguments.
|
|
60
|
+
|
|
61
|
+
Examples::
|
|
62
|
+
|
|
63
|
+
query1 = ESQL.row(a=1, b="two", c=None)
|
|
64
|
+
query2 = ESQL.row(a=[1, 2])
|
|
65
|
+
query3 = ESQL.row(a=functions.round(1.23, 0))
|
|
66
|
+
"""
|
|
67
|
+
return Row(**params)
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def show(item: str) -> "Show":
|
|
71
|
+
"""The ``SHOW`` source command returns information about the deployment and its capabilities.
|
|
72
|
+
|
|
73
|
+
:param item: Can only be ``INFO``.
|
|
74
|
+
|
|
75
|
+
Examples::
|
|
76
|
+
|
|
77
|
+
query = ESQL.show("INFO")
|
|
78
|
+
"""
|
|
79
|
+
return Show(item)
|
|
80
|
+
|
|
81
|
+
@staticmethod
|
|
82
|
+
def branch() -> "Branch":
|
|
83
|
+
"""This method can only be used inside a ``FORK`` command to create each branch.
|
|
84
|
+
|
|
85
|
+
Examples::
|
|
86
|
+
|
|
87
|
+
query = ESQL.from_("employees").fork(
|
|
88
|
+
ESQL.branch().where("emp_no == 10001"),
|
|
89
|
+
ESQL.branch().where("emp_no == 10002"),
|
|
90
|
+
)
|
|
91
|
+
"""
|
|
92
|
+
return Branch()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class ESQLBase(ABC):
|
|
96
|
+
"""The methods of the ``ESQLBase`` class provide access to the ES|QL processing
|
|
97
|
+
commands, used to build ES|QL queries.
|
|
98
|
+
"""
|
|
99
|
+
|
|
100
|
+
def __init__(self, parent: Optional["ESQLBase"] = None):
|
|
101
|
+
self._parent = parent
|
|
102
|
+
|
|
103
|
+
def __repr__(self) -> str:
|
|
104
|
+
return self.render()
|
|
105
|
+
|
|
106
|
+
def render(self) -> str:
|
|
107
|
+
return (
|
|
108
|
+
self._parent.render() + "\n| " if self._parent else ""
|
|
109
|
+
) + self._render_internal()
|
|
110
|
+
|
|
111
|
+
@abstractmethod
|
|
112
|
+
def _render_internal(self) -> str:
|
|
113
|
+
pass
|
|
114
|
+
|
|
115
|
+
@staticmethod
|
|
116
|
+
def _format_index(index: IndexType) -> str:
|
|
117
|
+
return index._index._name if hasattr(index, "_index") else str(index)
|
|
118
|
+
|
|
119
|
+
@staticmethod
|
|
120
|
+
def _format_id(id: FieldType, allow_patterns: bool = False) -> str:
|
|
121
|
+
s = str(id) # in case it is an InstrumentedField
|
|
122
|
+
if allow_patterns and "*" in s:
|
|
123
|
+
return s # patterns cannot be escaped
|
|
124
|
+
if re.fullmatch(r"[a-zA-Z_@][a-zA-Z0-9_\.]*", s):
|
|
125
|
+
return s
|
|
126
|
+
# this identifier needs to be escaped
|
|
127
|
+
s.replace("`", "``")
|
|
128
|
+
return f"`{s}`"
|
|
129
|
+
|
|
130
|
+
@staticmethod
|
|
131
|
+
def _format_expr(expr: ExpressionType) -> str:
|
|
132
|
+
return (
|
|
133
|
+
json.dumps(expr)
|
|
134
|
+
if not isinstance(expr, (str, InstrumentedExpression))
|
|
135
|
+
else str(expr)
|
|
136
|
+
)
|
|
137
|
+
|
|
138
|
+
def _is_forked(self) -> bool:
|
|
139
|
+
if self.__class__.__name__ == "Fork":
|
|
140
|
+
return True
|
|
141
|
+
if self._parent:
|
|
142
|
+
return self._parent._is_forked()
|
|
143
|
+
return False
|
|
144
|
+
|
|
145
|
+
def change_point(self, value: FieldType) -> "ChangePoint":
|
|
146
|
+
"""`CHANGE_POINT` detects spikes, dips, and change points in a metric.
|
|
147
|
+
|
|
148
|
+
:param value: The column with the metric in which you want to detect a change point.
|
|
149
|
+
|
|
150
|
+
Examples::
|
|
151
|
+
|
|
152
|
+
query = (
|
|
153
|
+
ESQL.row(key=list(range(1, 26)))
|
|
154
|
+
.mv_expand("key")
|
|
155
|
+
.eval(value=functions.case("key<13", 0, 42))
|
|
156
|
+
.change_point("value")
|
|
157
|
+
.on("key")
|
|
158
|
+
.where("type IS NOT NULL")
|
|
159
|
+
)
|
|
160
|
+
"""
|
|
161
|
+
return ChangePoint(self, value)
|
|
162
|
+
|
|
163
|
+
def completion(
|
|
164
|
+
self, *prompt: ExpressionType, **named_prompt: ExpressionType
|
|
165
|
+
) -> "Completion":
|
|
166
|
+
"""The `COMPLETION` command allows you to send prompts and context to a Large
|
|
167
|
+
Language Model (LLM) directly within your ES|QL queries, to perform text
|
|
168
|
+
generation tasks.
|
|
169
|
+
|
|
170
|
+
:param prompt: The input text or expression used to prompt the LLM. This can
|
|
171
|
+
be a string literal or a reference to a column containing text.
|
|
172
|
+
:param named_prompt: The input text or expresion, given as a keyword argument.
|
|
173
|
+
The argument name is used for the column name. If not
|
|
174
|
+
specified, the results will be stored in a column named
|
|
175
|
+
`completion`. If the specified column already exists, it
|
|
176
|
+
will be overwritten with the new results.
|
|
177
|
+
|
|
178
|
+
Examples::
|
|
179
|
+
|
|
180
|
+
query1 = (
|
|
181
|
+
ESQL.row(question="What is Elasticsearch?")
|
|
182
|
+
.completion("question").with_("test_completion_model")
|
|
183
|
+
.keep("question", "completion")
|
|
184
|
+
)
|
|
185
|
+
query2 = (
|
|
186
|
+
ESQL.row(question="What is Elasticsearch?")
|
|
187
|
+
.completion(answer="question").with_("test_completion_model")
|
|
188
|
+
.keep("question", "answer")
|
|
189
|
+
)
|
|
190
|
+
query3 = (
|
|
191
|
+
ESQL.from_("movies")
|
|
192
|
+
.sort("rating DESC")
|
|
193
|
+
.limit(10)
|
|
194
|
+
.eval(prompt=\"\"\"CONCAT(
|
|
195
|
+
"Summarize this movie using the following information: \\n",
|
|
196
|
+
"Title: ", title, "\\n",
|
|
197
|
+
"Synopsis: ", synopsis, "\\n",
|
|
198
|
+
"Actors: ", MV_CONCAT(actors, ", "), "\\n",
|
|
199
|
+
)\"\"\")
|
|
200
|
+
.completion(summary="prompt").with_("test_completion_model")
|
|
201
|
+
.keep("title", "summary", "rating")
|
|
202
|
+
)
|
|
203
|
+
"""
|
|
204
|
+
return Completion(self, *prompt, **named_prompt)
|
|
205
|
+
|
|
206
|
+
def dissect(self, input: FieldType, pattern: str) -> "Dissect":
|
|
207
|
+
"""``DISSECT`` enables you to extract structured data out of a string.
|
|
208
|
+
|
|
209
|
+
:param input: The column that contains the string you want to structure. If
|
|
210
|
+
the column has multiple values, ``DISSECT`` will process each value.
|
|
211
|
+
:param pattern: A dissect pattern. If a field name conflicts with an existing
|
|
212
|
+
column, the existing column is dropped. If a field name is used
|
|
213
|
+
more than once, only the rightmost duplicate creates a column.
|
|
214
|
+
|
|
215
|
+
Examples::
|
|
216
|
+
|
|
217
|
+
query = (
|
|
218
|
+
ESQL.row(a="2023-01-23T12:15:00.000Z - some text - 127.0.0.1")
|
|
219
|
+
.dissect("a", "%{date} - %{msg} - %{ip}")
|
|
220
|
+
.keep("date", "msg", "ip")
|
|
221
|
+
.eval(date="TO_DATETIME(date)")
|
|
222
|
+
)
|
|
223
|
+
"""
|
|
224
|
+
return Dissect(self, input, pattern)
|
|
225
|
+
|
|
226
|
+
def drop(self, *columns: FieldType) -> "Drop":
|
|
227
|
+
"""The ``DROP`` processing command removes one or more columns.
|
|
228
|
+
|
|
229
|
+
:param columns: The columns to drop, given as positional arguments. Supports wildcards.
|
|
230
|
+
|
|
231
|
+
Examples::
|
|
232
|
+
|
|
233
|
+
query1 = ESQL.from_("employees").drop("height")
|
|
234
|
+
query2 = ESQL.from_("employees").drop("height*")
|
|
235
|
+
"""
|
|
236
|
+
return Drop(self, *columns)
|
|
237
|
+
|
|
238
|
+
def enrich(self, policy: str) -> "Enrich":
|
|
239
|
+
"""``ENRICH`` enables you to add data from existing indices as new columns using an
|
|
240
|
+
enrich policy.
|
|
241
|
+
|
|
242
|
+
:param policy: The name of the enrich policy. You need to create and execute the
|
|
243
|
+
enrich policy first.
|
|
244
|
+
|
|
245
|
+
Examples::
|
|
246
|
+
|
|
247
|
+
query1 = (
|
|
248
|
+
ESQL.row(a="1")
|
|
249
|
+
.enrich("languages_policy").on("a").with_("language_name")
|
|
250
|
+
)
|
|
251
|
+
query2 = (
|
|
252
|
+
ESQL.row(a="1")
|
|
253
|
+
.enrich("languages_policy").on("a").with_(name="language_name")
|
|
254
|
+
)
|
|
255
|
+
"""
|
|
256
|
+
return Enrich(self, policy)
|
|
257
|
+
|
|
258
|
+
def eval(self, *columns: ExpressionType, **named_columns: ExpressionType) -> "Eval":
|
|
259
|
+
"""The ``EVAL`` processing command enables you to append new columns with calculated values.
|
|
260
|
+
|
|
261
|
+
:param columns: The values for the columns, given as positional arguments. Can be literals,
|
|
262
|
+
expressions, or functions. Can use columns defined left of this one.
|
|
263
|
+
:param named_columns: The values for the new columns, given as keyword arguments. The name
|
|
264
|
+
of the arguments is used as column name. If a column with the same
|
|
265
|
+
name already exists, the existing column is dropped. If a column name
|
|
266
|
+
is used more than once, only the rightmost duplicate creates a column.
|
|
267
|
+
|
|
268
|
+
Examples::
|
|
269
|
+
|
|
270
|
+
query1 = (
|
|
271
|
+
ESQL.from_("employees")
|
|
272
|
+
.sort("emp_no")
|
|
273
|
+
.keep("first_name", "last_name", "height")
|
|
274
|
+
.eval(height_feet="height * 3.281", height_cm="height * 100")
|
|
275
|
+
)
|
|
276
|
+
query2 = (
|
|
277
|
+
ESQL.from_("employees")
|
|
278
|
+
.eval("height * 3.281")
|
|
279
|
+
.stats(avg_height_feet=functions.avg("`height * 3.281`"))
|
|
280
|
+
)
|
|
281
|
+
"""
|
|
282
|
+
return Eval(self, *columns, **named_columns)
|
|
283
|
+
|
|
284
|
+
def fork(
|
|
285
|
+
self,
|
|
286
|
+
fork1: "ESQLBase",
|
|
287
|
+
fork2: Optional["ESQLBase"] = None,
|
|
288
|
+
fork3: Optional["ESQLBase"] = None,
|
|
289
|
+
fork4: Optional["ESQLBase"] = None,
|
|
290
|
+
fork5: Optional["ESQLBase"] = None,
|
|
291
|
+
fork6: Optional["ESQLBase"] = None,
|
|
292
|
+
fork7: Optional["ESQLBase"] = None,
|
|
293
|
+
fork8: Optional["ESQLBase"] = None,
|
|
294
|
+
) -> "Fork":
|
|
295
|
+
"""The ``FORK`` processing command creates multiple execution branches to operate on the
|
|
296
|
+
same input data and combines the results in a single output table.
|
|
297
|
+
|
|
298
|
+
:param fork<n>: Up to 8 execution branches, created with the ``ESQL.branch()`` method.
|
|
299
|
+
|
|
300
|
+
Examples::
|
|
301
|
+
|
|
302
|
+
query = (
|
|
303
|
+
ESQL.from_("employees")
|
|
304
|
+
.fork(
|
|
305
|
+
ESQL.branch().where("emp_no == 10001"),
|
|
306
|
+
ESQL.branch().where("emp_no == 10002"),
|
|
307
|
+
)
|
|
308
|
+
.keep("emp_no", "_fork")
|
|
309
|
+
.sort("emp_no")
|
|
310
|
+
)
|
|
311
|
+
"""
|
|
312
|
+
if self._is_forked():
|
|
313
|
+
raise ValueError("a query can only have one fork")
|
|
314
|
+
return Fork(self, fork1, fork2, fork3, fork4, fork5, fork6, fork7, fork8)
|
|
315
|
+
|
|
316
|
+
def grok(self, input: FieldType, pattern: str) -> "Grok":
|
|
317
|
+
"""``GROK`` enables you to extract structured data out of a string.
|
|
318
|
+
|
|
319
|
+
:param input: The column that contains the string you want to structure. If the
|
|
320
|
+
column has multiple values, ``GROK`` will process each value.
|
|
321
|
+
:param pattern: A grok pattern. If a field name conflicts with an existing column,
|
|
322
|
+
the existing column is discarded. If a field name is used more than
|
|
323
|
+
once, a multi-valued column will be created with one value per each
|
|
324
|
+
occurrence of the field name.
|
|
325
|
+
|
|
326
|
+
Examples::
|
|
327
|
+
|
|
328
|
+
query1 = (
|
|
329
|
+
ESQL.row(a="2023-01-23T12:15:00.000Z 127.0.0.1 some.email@foo.com 42")
|
|
330
|
+
.grok("a", "%{TIMESTAMP_ISO8601:date} %{IP:ip} %{EMAILADDRESS:email} %{NUMBER:num}")
|
|
331
|
+
.keep("date", "ip", "email", "num")
|
|
332
|
+
)
|
|
333
|
+
query2 = (
|
|
334
|
+
ESQL.row(a="2023-01-23T12:15:00.000Z 127.0.0.1 some.email@foo.com 42")
|
|
335
|
+
.grok(
|
|
336
|
+
"a",
|
|
337
|
+
"%{TIMESTAMP_ISO8601:date} %{IP:ip} %{EMAILADDRESS:email} %{NUMBER:num:int}",
|
|
338
|
+
)
|
|
339
|
+
.keep("date", "ip", "email", "num")
|
|
340
|
+
.eval(date=functions.to_datetime("date"))
|
|
341
|
+
)
|
|
342
|
+
query3 = (
|
|
343
|
+
ESQL.from_("addresses")
|
|
344
|
+
.keep("city.name", "zip_code")
|
|
345
|
+
.grok("zip_code", "%{WORD:zip_parts} %{WORD:zip_parts}")
|
|
346
|
+
)
|
|
347
|
+
"""
|
|
348
|
+
return Grok(self, input, pattern)
|
|
349
|
+
|
|
350
|
+
def keep(self, *columns: FieldType) -> "Keep":
|
|
351
|
+
"""The ``KEEP`` processing command enables you to specify what columns are returned
|
|
352
|
+
and the order in which they are returned.
|
|
353
|
+
|
|
354
|
+
:param columns: The columns to keep, given as positional arguments. Supports
|
|
355
|
+
wildcards.
|
|
356
|
+
|
|
357
|
+
Examples::
|
|
358
|
+
|
|
359
|
+
query1 = ESQL.from_("employees").keep("emp_no", "first_name", "last_name", "height")
|
|
360
|
+
query2 = ESQL.from_("employees").keep("h*")
|
|
361
|
+
query3 = ESQL.from_("employees").keep("h*", "*")
|
|
362
|
+
"""
|
|
363
|
+
return Keep(self, *columns)
|
|
364
|
+
|
|
365
|
+
def limit(self, max_number_of_rows: int) -> "Limit":
|
|
366
|
+
"""The ``LIMIT`` processing command enables you to limit the number of rows that are
|
|
367
|
+
returned.
|
|
368
|
+
|
|
369
|
+
:param max_number_of_rows: The maximum number of rows to return.
|
|
370
|
+
|
|
371
|
+
Examples::
|
|
372
|
+
|
|
373
|
+
query1 = ESQL.from_("employees").sort("emp_no ASC").limit(5)
|
|
374
|
+
query2 = ESQL.from_("index").stats(functions.avg("field1")).by("field2").limit(20000)
|
|
375
|
+
"""
|
|
376
|
+
return Limit(self, max_number_of_rows)
|
|
377
|
+
|
|
378
|
+
def lookup_join(self, lookup_index: IndexType) -> "LookupJoin":
|
|
379
|
+
"""`LOOKUP JOIN` enables you to add data from another index, AKA a 'lookup' index,
|
|
380
|
+
to your ES|QL query results, simplifying data enrichment and analysis workflows.
|
|
381
|
+
|
|
382
|
+
:param lookup_index: The name of the lookup index. This must be a specific index
|
|
383
|
+
name - wildcards, aliases, and remote cluster references are
|
|
384
|
+
not supported. Indices used for lookups must be configured
|
|
385
|
+
with the lookup index mode.
|
|
386
|
+
|
|
387
|
+
Examples::
|
|
388
|
+
|
|
389
|
+
query1 = (
|
|
390
|
+
ESQL.from_("firewall_logs")
|
|
391
|
+
.lookup_join("threat_list").on("source.IP")
|
|
392
|
+
.where("threat_level IS NOT NULL")
|
|
393
|
+
)
|
|
394
|
+
query2 = (
|
|
395
|
+
ESQL.from_("system_metrics")
|
|
396
|
+
.lookup_join("host_inventory").on("host.name")
|
|
397
|
+
.lookup_join("ownerships").on("host.name")
|
|
398
|
+
)
|
|
399
|
+
query3 = (
|
|
400
|
+
ESQL.from_("app_logs")
|
|
401
|
+
.lookup_join("service_owners").on("service_id")
|
|
402
|
+
)
|
|
403
|
+
query4 = (
|
|
404
|
+
ESQL.from_("employees")
|
|
405
|
+
.eval(language_code="languages")
|
|
406
|
+
.where("emp_no >= 10091 AND emp_no < 10094")
|
|
407
|
+
.lookup_join("languages_lookup").on("language_code")
|
|
408
|
+
)
|
|
409
|
+
"""
|
|
410
|
+
return LookupJoin(self, lookup_index)
|
|
411
|
+
|
|
412
|
+
def mv_expand(self, column: FieldType) -> "MvExpand":
|
|
413
|
+
"""The `MV_EXPAND` processing command expands multivalued columns into one row per
|
|
414
|
+
value, duplicating other columns.
|
|
415
|
+
|
|
416
|
+
:param column: The multivalued column to expand.
|
|
417
|
+
|
|
418
|
+
Examples::
|
|
419
|
+
|
|
420
|
+
query = ESQL.row(a=[1, 2, 3], b="b", j=["a", "b"]).mv_expand("a")
|
|
421
|
+
"""
|
|
422
|
+
return MvExpand(self, column)
|
|
423
|
+
|
|
424
|
+
def rename(self, **columns: FieldType) -> "Rename":
|
|
425
|
+
"""The ``RENAME`` processing command renames one or more columns.
|
|
426
|
+
|
|
427
|
+
:param columns: The old and new column name pairs, given as keyword arguments.
|
|
428
|
+
If a name conflicts with an existing column name, the existing column
|
|
429
|
+
is dropped. If multiple columns are renamed to the same name, all but
|
|
430
|
+
the rightmost column with the same new name are dropped.
|
|
431
|
+
|
|
432
|
+
Examples::
|
|
433
|
+
|
|
434
|
+
query = (
|
|
435
|
+
ESQL.from_("employees")
|
|
436
|
+
.keep("first_name", "last_name", "still_hired")
|
|
437
|
+
.rename(still_hired="employed")
|
|
438
|
+
)
|
|
439
|
+
"""
|
|
440
|
+
return Rename(self, **columns)
|
|
441
|
+
|
|
442
|
+
def sample(self, probability: float) -> "Sample":
|
|
443
|
+
"""The ``SAMPLE`` command samples a fraction of the table rows.
|
|
444
|
+
|
|
445
|
+
:param probability: The probability that a row is included in the sample. The value
|
|
446
|
+
must be between 0 and 1, exclusive.
|
|
447
|
+
|
|
448
|
+
Examples::
|
|
449
|
+
|
|
450
|
+
query = ESQL.from_("employees").keep("emp_no").sample(0.05)
|
|
451
|
+
"""
|
|
452
|
+
return Sample(self, probability)
|
|
453
|
+
|
|
454
|
+
def sort(self, *columns: ExpressionType) -> "Sort":
|
|
455
|
+
"""The ``SORT`` processing command sorts a table on one or more columns.
|
|
456
|
+
|
|
457
|
+
:param columns: The columns to sort on.
|
|
458
|
+
|
|
459
|
+
Examples::
|
|
460
|
+
|
|
461
|
+
query1 = (
|
|
462
|
+
ESQL.from_("employees")
|
|
463
|
+
.keep("first_name", "last_name", "height")
|
|
464
|
+
.sort("height")
|
|
465
|
+
)
|
|
466
|
+
query2 = (
|
|
467
|
+
ESQL.from_("employees")
|
|
468
|
+
.keep("first_name", "last_name", "height")
|
|
469
|
+
.sort("height DESC")
|
|
470
|
+
)
|
|
471
|
+
query3 = (
|
|
472
|
+
ESQL.from_("employees")
|
|
473
|
+
.keep("first_name", "last_name", "height")
|
|
474
|
+
.sort("height DESC", "first_name ASC")
|
|
475
|
+
)
|
|
476
|
+
query4 = (
|
|
477
|
+
ESQL.from_("employees")
|
|
478
|
+
.keep("first_name", "last_name", "height")
|
|
479
|
+
.sort("first_name ASC NULLS FIRST")
|
|
480
|
+
)
|
|
481
|
+
"""
|
|
482
|
+
return Sort(self, *columns)
|
|
483
|
+
|
|
484
|
+
def stats(
|
|
485
|
+
self, *expressions: ExpressionType, **named_expressions: ExpressionType
|
|
486
|
+
) -> "Stats":
|
|
487
|
+
"""The ``STATS`` processing command groups rows according to a common value and
|
|
488
|
+
calculates one or more aggregated values over the grouped rows.
|
|
489
|
+
|
|
490
|
+
:param expressions: A list of expressions, given as positional arguments.
|
|
491
|
+
:param named_expressions: A list of expressions, given as keyword arguments. The
|
|
492
|
+
argument names are used for the returned aggregated values.
|
|
493
|
+
|
|
494
|
+
Note that only one of `expressions` and `named_expressions` must be provided.
|
|
495
|
+
|
|
496
|
+
Examples::
|
|
497
|
+
|
|
498
|
+
query1 = (
|
|
499
|
+
ESQL.from_("employees")
|
|
500
|
+
.stats(count=functions.count("emp_no")).by("languages")
|
|
501
|
+
.sort("languages")
|
|
502
|
+
)
|
|
503
|
+
query2 = (
|
|
504
|
+
ESQL.from_("employees")
|
|
505
|
+
.stats(avg_lang=functions.avg("languages"))
|
|
506
|
+
)
|
|
507
|
+
query3 = (
|
|
508
|
+
ESQL.from_("employees")
|
|
509
|
+
.stats(
|
|
510
|
+
avg_lang=functions.avg("languages"),
|
|
511
|
+
max_lang=functions.max("languages")
|
|
512
|
+
)
|
|
513
|
+
)
|
|
514
|
+
query4 = (
|
|
515
|
+
ESQL.from_("employees")
|
|
516
|
+
.stats(
|
|
517
|
+
avg50s=functions.avg("salary").where('birth_date < "1960-01-01"'),
|
|
518
|
+
avg60s=functions.avg("salary").where('birth_date >= "1960-01-01"'),
|
|
519
|
+
).by("gender")
|
|
520
|
+
.sort("gender")
|
|
521
|
+
)
|
|
522
|
+
query5 = (
|
|
523
|
+
ESQL.from_("employees")
|
|
524
|
+
.eval(Ks="salary / 1000")
|
|
525
|
+
.stats(
|
|
526
|
+
under_40K=functions.count("*").where("Ks < 40"),
|
|
527
|
+
inbetween=functions.count("*").where("40 <= Ks AND Ks < 60"),
|
|
528
|
+
over_60K=functions.count("*").where("60 <= Ks"),
|
|
529
|
+
total=f.count("*")
|
|
530
|
+
)
|
|
531
|
+
)
|
|
532
|
+
query6 = (
|
|
533
|
+
ESQL.row(i=1, a=["a", "b"])
|
|
534
|
+
.stats(functions.min("i")).by("a")
|
|
535
|
+
.sort("a ASC")
|
|
536
|
+
)
|
|
537
|
+
query7 = (
|
|
538
|
+
ESQL.from_("employees")
|
|
539
|
+
.eval(hired=functions.date_format("hire_date", "yyyy"))
|
|
540
|
+
.stats(avg_salary=functions.avg("salary")).by("hired", "languages.long")
|
|
541
|
+
.eval(avg_salary=functions.round("avg_salary"))
|
|
542
|
+
.sort("hired", "languages.long")
|
|
543
|
+
|
|
544
|
+
)
|
|
545
|
+
"""
|
|
546
|
+
return Stats(self, *expressions, **named_expressions)
|
|
547
|
+
|
|
548
|
+
def where(self, *expressions: ExpressionType) -> "Where":
|
|
549
|
+
"""The ``WHERE`` processing command produces a table that contains all the rows
|
|
550
|
+
from the input table for which the provided condition evaluates to `true`.
|
|
551
|
+
|
|
552
|
+
:param expressions: A list of boolean expressions, given as positional arguments.
|
|
553
|
+
These expressions are combined with an ``AND`` logical operator.
|
|
554
|
+
|
|
555
|
+
Examples::
|
|
556
|
+
|
|
557
|
+
query1 = (
|
|
558
|
+
ESQL.from_("employees")
|
|
559
|
+
.keep("first_name", "last_name", "still_hired")
|
|
560
|
+
.where("still_hired == true")
|
|
561
|
+
)
|
|
562
|
+
query2 = (
|
|
563
|
+
ESQL.from_("sample_data")
|
|
564
|
+
.where("@timestamp > NOW() - 1 hour")
|
|
565
|
+
)
|
|
566
|
+
query3 = (
|
|
567
|
+
ESQL.from_("employees")
|
|
568
|
+
.keep("first_name", "last_name", "height")
|
|
569
|
+
.where("LENGTH(first_name) < 4")
|
|
570
|
+
)
|
|
571
|
+
"""
|
|
572
|
+
return Where(self, *expressions)
|
|
573
|
+
|
|
574
|
+
|
|
575
|
+
class From(ESQLBase):
|
|
576
|
+
"""Implementation of the ``FROM`` source command.
|
|
577
|
+
|
|
578
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
579
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
580
|
+
in a single expression.
|
|
581
|
+
"""
|
|
582
|
+
|
|
583
|
+
def __init__(self, *indices: IndexType):
|
|
584
|
+
super().__init__()
|
|
585
|
+
self._indices = indices
|
|
586
|
+
self._metadata_fields: Tuple[FieldType, ...] = tuple()
|
|
587
|
+
|
|
588
|
+
def metadata(self, *fields: FieldType) -> "From":
|
|
589
|
+
"""Continuation of the ``FROM`` source command.
|
|
590
|
+
|
|
591
|
+
:param fields: metadata fields to retrieve, given as positional arguments.
|
|
592
|
+
"""
|
|
593
|
+
self._metadata_fields = fields
|
|
594
|
+
return self
|
|
595
|
+
|
|
596
|
+
def _render_internal(self) -> str:
|
|
597
|
+
indices = [self._format_index(index) for index in self._indices]
|
|
598
|
+
s = f'{self.__class__.__name__.upper()} {", ".join(indices)}'
|
|
599
|
+
if self._metadata_fields:
|
|
600
|
+
s = (
|
|
601
|
+
s
|
|
602
|
+
+ f' METADATA {", ".join([self._format_id(field) for field in self._metadata_fields])}'
|
|
603
|
+
)
|
|
604
|
+
return s
|
|
605
|
+
|
|
606
|
+
|
|
607
|
+
class Row(ESQLBase):
|
|
608
|
+
"""Implementation of the ``ROW`` source command.
|
|
609
|
+
|
|
610
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
611
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
612
|
+
in a single expression.
|
|
613
|
+
"""
|
|
614
|
+
|
|
615
|
+
def __init__(self, **params: ExpressionType):
|
|
616
|
+
super().__init__()
|
|
617
|
+
self._params = {
|
|
618
|
+
self._format_id(k): (
|
|
619
|
+
json.dumps(v)
|
|
620
|
+
if not isinstance(v, InstrumentedExpression)
|
|
621
|
+
else self._format_expr(v)
|
|
622
|
+
)
|
|
623
|
+
for k, v in params.items()
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
def _render_internal(self) -> str:
|
|
627
|
+
return "ROW " + ", ".join([f"{k} = {v}" for k, v in self._params.items()])
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
class Show(ESQLBase):
|
|
631
|
+
"""Implementation of the ``SHOW`` source command.
|
|
632
|
+
|
|
633
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
634
|
+
which makes it possible to chain all the commands that belong to an ES|QL query
|
|
635
|
+
in a single expression.
|
|
636
|
+
"""
|
|
637
|
+
|
|
638
|
+
def __init__(self, item: str):
|
|
639
|
+
super().__init__()
|
|
640
|
+
self._item = item
|
|
641
|
+
|
|
642
|
+
def _render_internal(self) -> str:
|
|
643
|
+
return f"SHOW {self._format_id(self._item)}"
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
class Branch(ESQLBase):
|
|
647
|
+
"""Implementation of a branch inside a ``FORK`` processing command.
|
|
648
|
+
|
|
649
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
650
|
+
which makes it possible to chain all the commands that belong to the branch
|
|
651
|
+
in a single expression.
|
|
652
|
+
"""
|
|
653
|
+
|
|
654
|
+
def _render_internal(self) -> str:
|
|
655
|
+
return ""
|
|
656
|
+
|
|
657
|
+
|
|
658
|
+
class ChangePoint(ESQLBase):
|
|
659
|
+
"""Implementation of the ``CHANGE POINT`` processing command.
|
|
660
|
+
|
|
661
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
662
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
663
|
+
in a single expression.
|
|
664
|
+
"""
|
|
665
|
+
|
|
666
|
+
def __init__(self, parent: ESQLBase, value: FieldType):
|
|
667
|
+
super().__init__(parent)
|
|
668
|
+
self._value = value
|
|
669
|
+
self._key: Optional[FieldType] = None
|
|
670
|
+
self._type_name: Optional[str] = None
|
|
671
|
+
self._pvalue_name: Optional[str] = None
|
|
672
|
+
|
|
673
|
+
def on(self, key: FieldType) -> "ChangePoint":
|
|
674
|
+
"""Continuation of the `CHANGE_POINT` command.
|
|
675
|
+
|
|
676
|
+
:param key: The column with the key to order the values by. If not specified,
|
|
677
|
+
`@timestamp` is used.
|
|
678
|
+
"""
|
|
679
|
+
self._key = key
|
|
680
|
+
return self
|
|
681
|
+
|
|
682
|
+
def as_(self, type_name: str, pvalue_name: str) -> "ChangePoint":
|
|
683
|
+
"""Continuation of the `CHANGE_POINT` command.
|
|
684
|
+
|
|
685
|
+
:param type_name: The name of the output column with the change point type.
|
|
686
|
+
If not specified, `type` is used.
|
|
687
|
+
:param pvalue_name: The name of the output column with the p-value that indicates
|
|
688
|
+
how extreme the change point is. If not specified, `pvalue` is used.
|
|
689
|
+
"""
|
|
690
|
+
self._type_name = type_name
|
|
691
|
+
self._pvalue_name = pvalue_name
|
|
692
|
+
return self
|
|
693
|
+
|
|
694
|
+
def _render_internal(self) -> str:
|
|
695
|
+
key = "" if not self._key else f" ON {self._format_id(self._key)}"
|
|
696
|
+
names = (
|
|
697
|
+
""
|
|
698
|
+
if not self._type_name and not self._pvalue_name
|
|
699
|
+
else f' AS {self._format_id(self._type_name or "type")}, {self._format_id(self._pvalue_name or "pvalue")}'
|
|
700
|
+
)
|
|
701
|
+
return f"CHANGE_POINT {self._value}{key}{names}"
|
|
702
|
+
|
|
703
|
+
|
|
704
|
+
class Completion(ESQLBase):
|
|
705
|
+
"""Implementation of the ``COMPLETION`` processing command.
|
|
706
|
+
|
|
707
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
708
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
709
|
+
in a single expression.
|
|
710
|
+
"""
|
|
711
|
+
|
|
712
|
+
def __init__(
|
|
713
|
+
self, parent: ESQLBase, *prompt: ExpressionType, **named_prompt: ExpressionType
|
|
714
|
+
):
|
|
715
|
+
if len(prompt) + len(named_prompt) > 1:
|
|
716
|
+
raise ValueError(
|
|
717
|
+
"this method requires either one positional or one keyword argument only"
|
|
718
|
+
)
|
|
719
|
+
super().__init__(parent)
|
|
720
|
+
self._prompt = prompt
|
|
721
|
+
self._named_prompt = named_prompt
|
|
722
|
+
self._inference_id: Optional[str] = None
|
|
723
|
+
|
|
724
|
+
def with_(self, inference_id: str) -> "Completion":
|
|
725
|
+
"""Continuation of the `COMPLETION` command.
|
|
726
|
+
|
|
727
|
+
:param inference_id: The ID of the inference endpoint to use for the task. The
|
|
728
|
+
inference endpoint must be configured with the completion
|
|
729
|
+
task type.
|
|
730
|
+
"""
|
|
731
|
+
self._inference_id = inference_id
|
|
732
|
+
return self
|
|
733
|
+
|
|
734
|
+
def _render_internal(self) -> str:
|
|
735
|
+
if self._inference_id is None:
|
|
736
|
+
raise ValueError("The completion command requires an inference ID")
|
|
737
|
+
with_ = {"inference_id": self._inference_id}
|
|
738
|
+
if self._named_prompt:
|
|
739
|
+
column = list(self._named_prompt.keys())[0]
|
|
740
|
+
prompt = list(self._named_prompt.values())[0]
|
|
741
|
+
return f"COMPLETION {self._format_id(column)} = {self._format_id(prompt)} WITH {json.dumps(with_)}"
|
|
742
|
+
else:
|
|
743
|
+
return f"COMPLETION {self._format_id(self._prompt[0])} WITH {json.dumps(with_)}"
|
|
744
|
+
|
|
745
|
+
|
|
746
|
+
class Dissect(ESQLBase):
|
|
747
|
+
"""Implementation of the ``DISSECT`` processing command.
|
|
748
|
+
|
|
749
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
750
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
751
|
+
in a single expression.
|
|
752
|
+
"""
|
|
753
|
+
|
|
754
|
+
def __init__(self, parent: ESQLBase, input: FieldType, pattern: str):
|
|
755
|
+
super().__init__(parent)
|
|
756
|
+
self._input = input
|
|
757
|
+
self._pattern = pattern
|
|
758
|
+
self._separator: Optional[str] = None
|
|
759
|
+
|
|
760
|
+
def append_separator(self, separator: str) -> "Dissect":
|
|
761
|
+
"""Continuation of the ``DISSECT`` command.
|
|
762
|
+
|
|
763
|
+
:param separator: A string used as the separator between appended values,
|
|
764
|
+
when using the append modifier.
|
|
765
|
+
"""
|
|
766
|
+
self._separator = separator
|
|
767
|
+
return self
|
|
768
|
+
|
|
769
|
+
def _render_internal(self) -> str:
|
|
770
|
+
sep = (
|
|
771
|
+
""
|
|
772
|
+
if self._separator is None
|
|
773
|
+
else f" APPEND_SEPARATOR={json.dumps(self._separator)}"
|
|
774
|
+
)
|
|
775
|
+
return (
|
|
776
|
+
f"DISSECT {self._format_id(self._input)} {json.dumps(self._pattern)}{sep}"
|
|
777
|
+
)
|
|
778
|
+
|
|
779
|
+
|
|
780
|
+
class Drop(ESQLBase):
|
|
781
|
+
"""Implementation of the ``DROP`` processing command.
|
|
782
|
+
|
|
783
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
784
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
785
|
+
in a single expression.
|
|
786
|
+
"""
|
|
787
|
+
|
|
788
|
+
def __init__(self, parent: ESQLBase, *columns: FieldType):
|
|
789
|
+
super().__init__(parent)
|
|
790
|
+
self._columns = columns
|
|
791
|
+
|
|
792
|
+
def _render_internal(self) -> str:
|
|
793
|
+
return f'DROP {", ".join([self._format_id(col, allow_patterns=True) for col in self._columns])}'
|
|
794
|
+
|
|
795
|
+
|
|
796
|
+
class Enrich(ESQLBase):
|
|
797
|
+
"""Implementation of the ``ENRICH`` processing command.
|
|
798
|
+
|
|
799
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
800
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
801
|
+
in a single expression.
|
|
802
|
+
"""
|
|
803
|
+
|
|
804
|
+
def __init__(self, parent: ESQLBase, policy: str):
|
|
805
|
+
super().__init__(parent)
|
|
806
|
+
self._policy = policy
|
|
807
|
+
self._match_field: Optional[FieldType] = None
|
|
808
|
+
self._fields: Optional[Tuple[FieldType, ...]] = None
|
|
809
|
+
self._named_fields: Optional[Dict[str, FieldType]] = None
|
|
810
|
+
|
|
811
|
+
def on(self, match_field: FieldType) -> "Enrich":
|
|
812
|
+
"""Continuation of the ``ENRICH`` command.
|
|
813
|
+
|
|
814
|
+
:param match_field: The match field. ``ENRICH`` uses its value to look for records
|
|
815
|
+
in the enrich index. If not specified, the match will be
|
|
816
|
+
performed on the column with the same name as the
|
|
817
|
+
`match_field` defined in the enrich policy.
|
|
818
|
+
"""
|
|
819
|
+
self._match_field = match_field
|
|
820
|
+
return self
|
|
821
|
+
|
|
822
|
+
def with_(self, *fields: FieldType, **named_fields: FieldType) -> "Enrich":
|
|
823
|
+
"""Continuation of the ``ENRICH`` command.
|
|
824
|
+
|
|
825
|
+
:param fields: The enrich fields from the enrich index that are added to the result
|
|
826
|
+
as new columns, given as positional arguments. If a column with the
|
|
827
|
+
same name as the enrich field already exists, the existing column will
|
|
828
|
+
be replaced by the new column. If not specified, each of the enrich
|
|
829
|
+
fields defined in the policy is added. A column with the same name as
|
|
830
|
+
the enrich field will be dropped unless the enrich field is renamed.
|
|
831
|
+
:param named_fields: The enrich fields from the enrich index that are added to the
|
|
832
|
+
result as new columns, given as keyword arguments. The name of
|
|
833
|
+
the keyword arguments are used as column names. If a column has
|
|
834
|
+
the same name as the new name, it will be discarded. If a name
|
|
835
|
+
(new or original) occurs more than once, only the rightmost
|
|
836
|
+
duplicate creates a new column.
|
|
837
|
+
"""
|
|
838
|
+
if fields and named_fields:
|
|
839
|
+
raise ValueError(
|
|
840
|
+
"this method supports positional or keyword arguments but not both"
|
|
841
|
+
)
|
|
842
|
+
self._fields = fields
|
|
843
|
+
self._named_fields = named_fields
|
|
844
|
+
return self
|
|
845
|
+
|
|
846
|
+
def _render_internal(self) -> str:
|
|
847
|
+
on = (
|
|
848
|
+
""
|
|
849
|
+
if self._match_field is None
|
|
850
|
+
else f" ON {self._format_id(self._match_field)}"
|
|
851
|
+
)
|
|
852
|
+
with_ = ""
|
|
853
|
+
if self._named_fields:
|
|
854
|
+
with_ = f' WITH {", ".join([f"{self._format_id(name)} = {self._format_id(field)}" for name, field in self._named_fields.items()])}'
|
|
855
|
+
elif self._fields is not None:
|
|
856
|
+
with_ = (
|
|
857
|
+
f' WITH {", ".join([self._format_id(field) for field in self._fields])}'
|
|
858
|
+
)
|
|
859
|
+
return f"ENRICH {self._policy}{on}{with_}"
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
class Eval(ESQLBase):
|
|
863
|
+
"""Implementation of the ``EVAL`` processing command.
|
|
864
|
+
|
|
865
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
866
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
867
|
+
in a single expression.
|
|
868
|
+
"""
|
|
869
|
+
|
|
870
|
+
def __init__(
|
|
871
|
+
self,
|
|
872
|
+
parent: ESQLBase,
|
|
873
|
+
*columns: ExpressionType,
|
|
874
|
+
**named_columns: ExpressionType,
|
|
875
|
+
):
|
|
876
|
+
if columns and named_columns:
|
|
877
|
+
raise ValueError(
|
|
878
|
+
"this method supports positional or keyword arguments but not both"
|
|
879
|
+
)
|
|
880
|
+
super().__init__(parent)
|
|
881
|
+
self._columns = columns or named_columns
|
|
882
|
+
|
|
883
|
+
def _render_internal(self) -> str:
|
|
884
|
+
if isinstance(self._columns, dict):
|
|
885
|
+
cols = ", ".join(
|
|
886
|
+
[
|
|
887
|
+
f"{self._format_id(name)} = {self._format_expr(value)}"
|
|
888
|
+
for name, value in self._columns.items()
|
|
889
|
+
]
|
|
890
|
+
)
|
|
891
|
+
else:
|
|
892
|
+
cols = ", ".join([f"{self._format_expr(col)}" for col in self._columns])
|
|
893
|
+
return f"EVAL {cols}"
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
class Fork(ESQLBase):
|
|
897
|
+
"""Implementation of the ``FORK`` processing command.
|
|
898
|
+
|
|
899
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
900
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
901
|
+
in a single expression.
|
|
902
|
+
"""
|
|
903
|
+
|
|
904
|
+
def __init__(
|
|
905
|
+
self,
|
|
906
|
+
parent: ESQLBase,
|
|
907
|
+
fork1: ESQLBase,
|
|
908
|
+
fork2: Optional[ESQLBase] = None,
|
|
909
|
+
fork3: Optional[ESQLBase] = None,
|
|
910
|
+
fork4: Optional[ESQLBase] = None,
|
|
911
|
+
fork5: Optional[ESQLBase] = None,
|
|
912
|
+
fork6: Optional[ESQLBase] = None,
|
|
913
|
+
fork7: Optional[ESQLBase] = None,
|
|
914
|
+
fork8: Optional[ESQLBase] = None,
|
|
915
|
+
):
|
|
916
|
+
super().__init__(parent)
|
|
917
|
+
self._branches = [fork1, fork2, fork3, fork4, fork5, fork6, fork7, fork8]
|
|
918
|
+
|
|
919
|
+
def _render_internal(self) -> str:
|
|
920
|
+
cmds = ""
|
|
921
|
+
for branch in self._branches:
|
|
922
|
+
if branch:
|
|
923
|
+
cmd = branch.render()[3:].replace("\n", " ")
|
|
924
|
+
if cmds == "":
|
|
925
|
+
cmds = f"( {cmd} )"
|
|
926
|
+
else:
|
|
927
|
+
cmds += f"\n ( {cmd} )"
|
|
928
|
+
return f"FORK {cmds}"
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
class Grok(ESQLBase):
|
|
932
|
+
"""Implementation of the ``GROK`` processing command.
|
|
933
|
+
|
|
934
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
935
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
936
|
+
in a single expression.
|
|
937
|
+
"""
|
|
938
|
+
|
|
939
|
+
def __init__(self, parent: ESQLBase, input: FieldType, pattern: str):
|
|
940
|
+
super().__init__(parent)
|
|
941
|
+
self._input = input
|
|
942
|
+
self._pattern = pattern
|
|
943
|
+
|
|
944
|
+
def _render_internal(self) -> str:
|
|
945
|
+
return f"GROK {self._format_id(self._input)} {json.dumps(self._pattern)}"
|
|
946
|
+
|
|
947
|
+
|
|
948
|
+
class Keep(ESQLBase):
|
|
949
|
+
"""Implementation of the ``KEEP`` processing command.
|
|
950
|
+
|
|
951
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
952
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
953
|
+
in a single expression.
|
|
954
|
+
"""
|
|
955
|
+
|
|
956
|
+
def __init__(self, parent: ESQLBase, *columns: FieldType):
|
|
957
|
+
super().__init__(parent)
|
|
958
|
+
self._columns = columns
|
|
959
|
+
|
|
960
|
+
def _render_internal(self) -> str:
|
|
961
|
+
return f'KEEP {", ".join([f"{self._format_id(col, allow_patterns=True)}" for col in self._columns])}'
|
|
962
|
+
|
|
963
|
+
|
|
964
|
+
class Limit(ESQLBase):
|
|
965
|
+
"""Implementation of the ``LIMIT`` processing command.
|
|
966
|
+
|
|
967
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
968
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
969
|
+
in a single expression.
|
|
970
|
+
"""
|
|
971
|
+
|
|
972
|
+
def __init__(self, parent: ESQLBase, max_number_of_rows: int):
|
|
973
|
+
super().__init__(parent)
|
|
974
|
+
self._max_number_of_rows = max_number_of_rows
|
|
975
|
+
|
|
976
|
+
def _render_internal(self) -> str:
|
|
977
|
+
return f"LIMIT {json.dumps(self._max_number_of_rows)}"
|
|
978
|
+
|
|
979
|
+
|
|
980
|
+
class LookupJoin(ESQLBase):
|
|
981
|
+
"""Implementation of the ``LOOKUP JOIN`` processing command.
|
|
982
|
+
|
|
983
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
984
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
985
|
+
in a single expression.
|
|
986
|
+
"""
|
|
987
|
+
|
|
988
|
+
def __init__(self, parent: ESQLBase, lookup_index: IndexType):
|
|
989
|
+
super().__init__(parent)
|
|
990
|
+
self._lookup_index = lookup_index
|
|
991
|
+
self._field: Optional[FieldType] = None
|
|
992
|
+
|
|
993
|
+
def on(self, field: FieldType) -> "LookupJoin":
|
|
994
|
+
"""Continuation of the `LOOKUP_JOIN` command.
|
|
995
|
+
|
|
996
|
+
:param field: The field to join on. This field must exist in both your current query
|
|
997
|
+
results and in the lookup index. If the field contains multi-valued
|
|
998
|
+
entries, those entries will not match anything (the added fields will
|
|
999
|
+
contain null for those rows).
|
|
1000
|
+
"""
|
|
1001
|
+
self._field = field
|
|
1002
|
+
return self
|
|
1003
|
+
|
|
1004
|
+
def _render_internal(self) -> str:
|
|
1005
|
+
if self._field is None:
|
|
1006
|
+
raise ValueError("Joins require a field to join on.")
|
|
1007
|
+
index = (
|
|
1008
|
+
self._lookup_index
|
|
1009
|
+
if isinstance(self._lookup_index, str)
|
|
1010
|
+
else self._lookup_index._index._name
|
|
1011
|
+
)
|
|
1012
|
+
return (
|
|
1013
|
+
f"LOOKUP JOIN {self._format_index(index)} ON {self._format_id(self._field)}"
|
|
1014
|
+
)
|
|
1015
|
+
|
|
1016
|
+
|
|
1017
|
+
class MvExpand(ESQLBase):
|
|
1018
|
+
"""Implementation of the ``MV_EXPAND`` processing command.
|
|
1019
|
+
|
|
1020
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
1021
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
1022
|
+
in a single expression.
|
|
1023
|
+
"""
|
|
1024
|
+
|
|
1025
|
+
def __init__(self, parent: ESQLBase, column: FieldType):
|
|
1026
|
+
super().__init__(parent)
|
|
1027
|
+
self._column = column
|
|
1028
|
+
|
|
1029
|
+
def _render_internal(self) -> str:
|
|
1030
|
+
return f"MV_EXPAND {self._format_id(self._column)}"
|
|
1031
|
+
|
|
1032
|
+
|
|
1033
|
+
class Rename(ESQLBase):
|
|
1034
|
+
"""Implementation of the ``RENAME`` processing command.
|
|
1035
|
+
|
|
1036
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
1037
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
1038
|
+
in a single expression.
|
|
1039
|
+
"""
|
|
1040
|
+
|
|
1041
|
+
def __init__(self, parent: ESQLBase, **columns: FieldType):
|
|
1042
|
+
super().__init__(parent)
|
|
1043
|
+
self._columns = columns
|
|
1044
|
+
|
|
1045
|
+
def _render_internal(self) -> str:
|
|
1046
|
+
return f'RENAME {", ".join([f"{self._format_id(old_name)} AS {self._format_id(new_name)}" for old_name, new_name in self._columns.items()])}'
|
|
1047
|
+
|
|
1048
|
+
|
|
1049
|
+
class Sample(ESQLBase):
|
|
1050
|
+
"""Implementation of the ``SAMPLE`` processing command.
|
|
1051
|
+
|
|
1052
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
1053
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
1054
|
+
in a single expression.
|
|
1055
|
+
"""
|
|
1056
|
+
|
|
1057
|
+
def __init__(self, parent: ESQLBase, probability: float):
|
|
1058
|
+
super().__init__(parent)
|
|
1059
|
+
self._probability = probability
|
|
1060
|
+
|
|
1061
|
+
def _render_internal(self) -> str:
|
|
1062
|
+
return f"SAMPLE {json.dumps(self._probability)}"
|
|
1063
|
+
|
|
1064
|
+
|
|
1065
|
+
class Sort(ESQLBase):
|
|
1066
|
+
"""Implementation of the ``SORT`` processing command.
|
|
1067
|
+
|
|
1068
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
1069
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
1070
|
+
in a single expression.
|
|
1071
|
+
"""
|
|
1072
|
+
|
|
1073
|
+
def __init__(self, parent: ESQLBase, *columns: ExpressionType):
|
|
1074
|
+
super().__init__(parent)
|
|
1075
|
+
self._columns = columns
|
|
1076
|
+
|
|
1077
|
+
def _render_internal(self) -> str:
|
|
1078
|
+
sorts = [
|
|
1079
|
+
" ".join([self._format_id(term) for term in str(col).split(" ")])
|
|
1080
|
+
for col in self._columns
|
|
1081
|
+
]
|
|
1082
|
+
return f'SORT {", ".join([f"{sort}" for sort in sorts])}'
|
|
1083
|
+
|
|
1084
|
+
|
|
1085
|
+
class Stats(ESQLBase):
|
|
1086
|
+
"""Implementation of the ``STATS`` processing command.
|
|
1087
|
+
|
|
1088
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
1089
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
1090
|
+
in a single expression.
|
|
1091
|
+
"""
|
|
1092
|
+
|
|
1093
|
+
def __init__(
|
|
1094
|
+
self,
|
|
1095
|
+
parent: ESQLBase,
|
|
1096
|
+
*expressions: ExpressionType,
|
|
1097
|
+
**named_expressions: ExpressionType,
|
|
1098
|
+
):
|
|
1099
|
+
if expressions and named_expressions:
|
|
1100
|
+
raise ValueError(
|
|
1101
|
+
"this method supports positional or keyword arguments but not both"
|
|
1102
|
+
)
|
|
1103
|
+
super().__init__(parent)
|
|
1104
|
+
self._expressions = expressions or named_expressions
|
|
1105
|
+
self._grouping_expressions: Optional[Tuple[ExpressionType, ...]] = None
|
|
1106
|
+
|
|
1107
|
+
def by(self, *grouping_expressions: ExpressionType) -> "Stats":
|
|
1108
|
+
self._grouping_expressions = grouping_expressions
|
|
1109
|
+
return self
|
|
1110
|
+
|
|
1111
|
+
def _render_internal(self) -> str:
|
|
1112
|
+
if isinstance(self._expressions, dict):
|
|
1113
|
+
exprs = [
|
|
1114
|
+
f"{self._format_id(key)} = {self._format_expr(value)}"
|
|
1115
|
+
for key, value in self._expressions.items()
|
|
1116
|
+
]
|
|
1117
|
+
else:
|
|
1118
|
+
exprs = [f"{self._format_expr(expr)}" for expr in self._expressions]
|
|
1119
|
+
expression_separator = ",\n "
|
|
1120
|
+
by = (
|
|
1121
|
+
""
|
|
1122
|
+
if self._grouping_expressions is None
|
|
1123
|
+
else f'\n BY {", ".join([f"{self._format_expr(expr)}" for expr in self._grouping_expressions])}'
|
|
1124
|
+
)
|
|
1125
|
+
return f'STATS {expression_separator.join([f"{expr}" for expr in exprs])}{by}'
|
|
1126
|
+
|
|
1127
|
+
|
|
1128
|
+
class Where(ESQLBase):
|
|
1129
|
+
"""Implementation of the ``WHERE`` processing command.
|
|
1130
|
+
|
|
1131
|
+
This class inherits from :class:`ESQLBase <elasticsearch.esql.esql.ESQLBase>`,
|
|
1132
|
+
to make it possible to chain all the commands that belong to an ES|QL query
|
|
1133
|
+
in a single expression.
|
|
1134
|
+
"""
|
|
1135
|
+
|
|
1136
|
+
def __init__(self, parent: ESQLBase, *expressions: ExpressionType):
|
|
1137
|
+
super().__init__(parent)
|
|
1138
|
+
self._expressions = expressions
|
|
1139
|
+
|
|
1140
|
+
def _render_internal(self) -> str:
|
|
1141
|
+
return f'WHERE {" AND ".join([f"{self._format_expr(expr)}" for expr in self._expressions])}'
|
|
1142
|
+
|
|
1143
|
+
|
|
1144
|
+
def and_(*expressions: InstrumentedExpression) -> "InstrumentedExpression":
|
|
1145
|
+
"""Combine two or more expressions with the AND operator."""
|
|
1146
|
+
return InstrumentedExpression(" AND ".join([f"({expr})" for expr in expressions]))
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
def or_(*expressions: InstrumentedExpression) -> "InstrumentedExpression":
|
|
1150
|
+
"""Combine two or more expressions with the OR operator."""
|
|
1151
|
+
return InstrumentedExpression(" OR ".join([f"({expr})" for expr in expressions]))
|
|
1152
|
+
|
|
1153
|
+
|
|
1154
|
+
def not_(expression: InstrumentedExpression) -> "InstrumentedExpression":
|
|
1155
|
+
"""Negate an expression."""
|
|
1156
|
+
return InstrumentedExpression(f"NOT ({expression})")
|