Skip to content

Commit 6415e10

Browse files
authored
Add ES|QL API (#67)
1 parent 613f559 commit 6415e10

File tree

6 files changed

+212
-0
lines changed

6 files changed

+212
-0
lines changed

docs/sphinx/api.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,13 @@ Enrich Policies
5151
Event Query Language (EQL)
5252
--------------------------
5353

54+
.. autoclass:: EqlClient
55+
:members:
56+
57+
58+
ES|QL
59+
-----
60+
5461
.. autoclass:: EqlClient
5562
:members:
5663

elasticsearch_serverless/_async/client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from .cluster import ClusterClient
4040
from .enrich import EnrichClient
4141
from .eql import EqlClient
42+
from .esql import EsqlClient
4243
from .graph import GraphClient
4344
from .indices import IndicesClient
4445
from .inference import InferenceClient
@@ -286,6 +287,7 @@ def __init__(
286287

287288
self.enrich = EnrichClient(self)
288289
self.eql = EqlClient(self)
290+
self.esql = EsqlClient(self)
289291
self.graph = GraphClient(self)
290292
self.license = LicenseClient(self)
291293
self.logstash = LogstashClient(self)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 typing as t
19+
20+
from elastic_transport import ObjectApiResponse
21+
22+
from ._base import NamespacedClient
23+
from .utils import _rewrite_parameters
24+
25+
26+
class EsqlClient(NamespacedClient):
27+
28+
@_rewrite_parameters(
29+
body_fields=("query", "columnar", "filter", "locale", "params"),
30+
ignore_deprecated_options={"params"},
31+
)
32+
async def query(
33+
self,
34+
*,
35+
query: t.Optional[str] = None,
36+
columnar: t.Optional[bool] = None,
37+
delimiter: t.Optional[str] = None,
38+
error_trace: t.Optional[bool] = None,
39+
filter: t.Optional[t.Mapping[str, t.Any]] = None,
40+
filter_path: t.Optional[t.Union[str, t.Sequence[str]]] = None,
41+
format: t.Optional[str] = None,
42+
human: t.Optional[bool] = None,
43+
locale: t.Optional[str] = None,
44+
params: t.Optional[t.Sequence[t.Union[None, bool, float, int, str]]] = None,
45+
pretty: t.Optional[bool] = None,
46+
body: t.Optional[t.Dict[str, t.Any]] = None,
47+
) -> ObjectApiResponse[t.Any]:
48+
"""
49+
Executes an ES|QL request
50+
51+
`<https://www.elastic.co/guide/en/elasticsearch/reference/master/esql-rest.html>`_
52+
53+
:param query: The ES|QL query API accepts an ES|QL query string in the query
54+
parameter, runs it, and returns the results.
55+
:param columnar: By default, ES|QL returns results as rows. For example, FROM
56+
returns each individual document as one row. For the JSON, YAML, CBOR and
57+
smile formats, ES|QL can return the results in a columnar fashion where one
58+
row represents all the values of a certain column in the results.
59+
:param delimiter: The character to use between values within a CSV row. Only
60+
valid for the CSV format.
61+
:param filter: Specify a Query DSL query in the filter parameter to filter the
62+
set of documents that an ES|QL query runs on.
63+
:param format: A short version of the Accept header, e.g. json, yaml.
64+
:param locale:
65+
:param params: To avoid any attempts of hacking or code injection, extract the
66+
values in a separate list of parameters. Use question mark placeholders (?)
67+
in the query string for each of the parameters.
68+
"""
69+
if query is None and body is None:
70+
raise ValueError("Empty value passed for parameter 'query'")
71+
__path = "/_query"
72+
__query: t.Dict[str, t.Any] = {}
73+
__body: t.Dict[str, t.Any] = body if body is not None else {}
74+
if delimiter is not None:
75+
__query["delimiter"] = delimiter
76+
if error_trace is not None:
77+
__query["error_trace"] = error_trace
78+
if filter_path is not None:
79+
__query["filter_path"] = filter_path
80+
if format is not None:
81+
__query["format"] = format
82+
if human is not None:
83+
__query["human"] = human
84+
if pretty is not None:
85+
__query["pretty"] = pretty
86+
if not __body:
87+
if query is not None:
88+
__body["query"] = query
89+
if columnar is not None:
90+
__body["columnar"] = columnar
91+
if filter is not None:
92+
__body["filter"] = filter
93+
if locale is not None:
94+
__body["locale"] = locale
95+
if params is not None:
96+
__body["params"] = params
97+
__headers = {"accept": "application/json", "content-type": "application/json"}
98+
return await self.perform_request( # type: ignore[return-value]
99+
"POST", __path, params=__query, headers=__headers, body=__body
100+
)

elasticsearch_serverless/_sync/client/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
from .cluster import ClusterClient
4040
from .enrich import EnrichClient
4141
from .eql import EqlClient
42+
from .esql import EsqlClient
4243
from .graph import GraphClient
4344
from .indices import IndicesClient
4445
from .inference import InferenceClient
@@ -286,6 +287,7 @@ def __init__(
286287

287288
self.enrich = EnrichClient(self)
288289
self.eql = EqlClient(self)
290+
self.esql = EsqlClient(self)
289291
self.graph = GraphClient(self)
290292
self.license = LicenseClient(self)
291293
self.logstash = LogstashClient(self)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 typing as t
19+
20+
from elastic_transport import ObjectApiResponse
21+
22+
from ._base import NamespacedClient
23+
from .utils import _rewrite_parameters
24+
25+
26+
class EsqlClient(NamespacedClient):
27+
28+
@_rewrite_parameters(
29+
body_fields=("query", "columnar", "filter", "locale", "params"),
30+
ignore_deprecated_options={"params"},
31+
)
32+
def query(
33+
self,
34+
*,
35+
query: t.Optional[str] = None,
36+
columnar: t.Optional[bool] = None,
37+
delimiter: t.Optional[str] = None,
38+
error_trace: t.Optional[bool] = None,
39+
filter: t.Optional[t.Mapping[str, t.Any]] = None,
40+
filter_path: t.Optional[t.Union[str, t.Sequence[str]]] = None,
41+
format: t.Optional[str] = None,
42+
human: t.Optional[bool] = None,
43+
locale: t.Optional[str] = None,
44+
params: t.Optional[t.Sequence[t.Union[None, bool, float, int, str]]] = None,
45+
pretty: t.Optional[bool] = None,
46+
body: t.Optional[t.Dict[str, t.Any]] = None,
47+
) -> ObjectApiResponse[t.Any]:
48+
"""
49+
Executes an ES|QL request
50+
51+
`<https://www.elastic.co/guide/en/elasticsearch/reference/master/esql-rest.html>`_
52+
53+
:param query: The ES|QL query API accepts an ES|QL query string in the query
54+
parameter, runs it, and returns the results.
55+
:param columnar: By default, ES|QL returns results as rows. For example, FROM
56+
returns each individual document as one row. For the JSON, YAML, CBOR and
57+
smile formats, ES|QL can return the results in a columnar fashion where one
58+
row represents all the values of a certain column in the results.
59+
:param delimiter: The character to use between values within a CSV row. Only
60+
valid for the CSV format.
61+
:param filter: Specify a Query DSL query in the filter parameter to filter the
62+
set of documents that an ES|QL query runs on.
63+
:param format: A short version of the Accept header, e.g. json, yaml.
64+
:param locale:
65+
:param params: To avoid any attempts of hacking or code injection, extract the
66+
values in a separate list of parameters. Use question mark placeholders (?)
67+
in the query string for each of the parameters.
68+
"""
69+
if query is None and body is None:
70+
raise ValueError("Empty value passed for parameter 'query'")
71+
__path = "/_query"
72+
__query: t.Dict[str, t.Any] = {}
73+
__body: t.Dict[str, t.Any] = body if body is not None else {}
74+
if delimiter is not None:
75+
__query["delimiter"] = delimiter
76+
if error_trace is not None:
77+
__query["error_trace"] = error_trace
78+
if filter_path is not None:
79+
__query["filter_path"] = filter_path
80+
if format is not None:
81+
__query["format"] = format
82+
if human is not None:
83+
__query["human"] = human
84+
if pretty is not None:
85+
__query["pretty"] = pretty
86+
if not __body:
87+
if query is not None:
88+
__body["query"] = query
89+
if columnar is not None:
90+
__body["columnar"] = columnar
91+
if filter is not None:
92+
__body["filter"] = filter
93+
if locale is not None:
94+
__body["locale"] = locale
95+
if params is not None:
96+
__body["params"] = params
97+
__headers = {"accept": "application/json", "content-type": "application/json"}
98+
return self.perform_request( # type: ignore[return-value]
99+
"POST", __path, params=__query, headers=__headers, body=__body
100+
)

elasticsearch_serverless/client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from ._sync.client.cluster import ClusterClient as ClusterClient # noqa: F401
2626
from ._sync.client.enrich import EnrichClient as EnrichClient # noqa: F401
2727
from ._sync.client.eql import EqlClient as EqlClient # noqa: F401
28+
from ._sync.client.esql import EsqlClient as EsqlClient # noqa: F401
2829
from ._sync.client.graph import GraphClient as GraphClient # noqa: F401
2930
from ._sync.client.indices import IndicesClient as IndicesClient # noqa: F401
3031
from ._sync.client.inference import InferenceClient as InferenceClient # noqa: F401

0 commit comments

Comments
 (0)