Skip to content

Commit 3548a7c

Browse files
add esql+dsl example
1 parent 89e00b4 commit 3548a7c

File tree

8 files changed

+13
-13
lines changed

8 files changed

+13
-13
lines changed

docs/reference/dsl_how_to_guides.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,12 +1479,12 @@ Katherine Ramirez from Kimberlyton is 1.83m tall
14791479
...
14801480
```
14811481

1482-
To search for specific documents you can extend the base query with additional ES|QL commands that narrow the search criteria. The next example searches for documents that include only employees that are 2m tall or more, sorted by their last name. It also limits the results to 4 people:
1482+
To search for specific documents you can extend the base query with additional ES|QL commands that narrow the search criteria. The next example searches for documents that include only employees that are taller than 2 meters, sorted by their last name. It also limits the results to 4 people:
14831483

14841484
```python
14851485
query = (
14861486
Employee.esql_from()
1487-
.where(Employee.height >= 2)
1487+
.where(Employee.height > 2)
14881488
.sort(Employee.last_name)
14891489
.limit(4)
14901490
)

docs/reference/esql-query-builder.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ FROM employees
2828
| LIMIT 3
2929
```
3030

31-
To execute this query, you can cast it to a string and pass the string to the `client.esql.query()` endpoint:
31+
To execute this query, you can pass it to the `client.esql.query()` endpoint:
3232

3333
```python
3434
>>> from elasticsearch import Elasticsearch
3535
>>> client = Elasticsearch(hosts=[os.environ['ELASTICSEARCH_URL']])
36-
>>> response = client.esql.query(query=str(query))
36+
>>> response = client.esql.query(query=query)
3737
```
3838

3939
The response body contains a `columns` attribute with the list of columns included in the results, and a `values` attribute with the list of results for the query, each given as a list of column values. Here is a possible response body returned by the example query given above:
@@ -216,7 +216,7 @@ def find_employee_by_name(name):
216216
.keep("first_name", "last_name", "height")
217217
.where(E("first_name") == E("?"))
218218
)
219-
return client.esql.query(query=str(query), params=[name])
219+
return client.esql.query(query=query, params=[name])
220220
```
221221

222222
Here the part of the query in which the untrusted data needs to be inserted is replaced with a parameter, which in ES|QL is defined by the question mark. When using Python expressions, the parameter must be given as `E("?")` so that it is treated as an expression and not as a literal string.

elasticsearch/esql/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
# under the License.
1717

1818
from ..dsl import E # noqa: F401
19-
from .esql import ESQL, and_, not_, or_ # noqa: F401
19+
from .esql import ESQL, ESQLBase, and_, not_, or_ # noqa: F401

elasticsearch/esql/functions.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ def min_over_time(field: ExpressionType) -> InstrumentedExpression:
649649

650650

651651
def multi_match(
652-
query: ExpressionType, fields: ExpressionType, options: ExpressionType = None
652+
query: ExpressionType, *fields: ExpressionType, options: ExpressionType = None
653653
) -> InstrumentedExpression:
654654
"""Use `MULTI_MATCH` to perform a multi-match query on the specified field.
655655
The multi_match query builds on the match query to allow multi-field queries.
@@ -661,11 +661,11 @@ def multi_match(
661661
"""
662662
if options is not None:
663663
return InstrumentedExpression(
664-
f"MULTI_MATCH({_render(query)}, {_render(fields)}, {_render(options)})"
664+
f'MULTI_MATCH({_render(query)}, {", ".join([_render(c) for c in fields])}, {_render(options)})'
665665
)
666666
else:
667667
return InstrumentedExpression(
668-
f"MULTI_MATCH({_render(query)}, {_render(fields)})"
668+
f'MULTI_MATCH({_render(query)}, {", ".join([_render(c) for c in fields])})'
669669
)
670670

671671

examples/dsl/semantic_text.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
2222
Requirements:
2323
24-
$ pip install "elasticsearch" tqdm
24+
$ pip install elasticsearch tqdm
2525
2626
Before running this example, an ELSER inference endpoint must be created in the
2727
Elasticsearch cluster. This can be done manually from Kibana, or with the

examples/dsl/sparse_vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
Requirements:
2222
23-
$ pip install nltk tqdm "elasticsearch"
23+
$ pip install nltk tqdm elasticsearch
2424
2525
Before running this example, the ELSER v2 model must be downloaded and deployed
2626
to the Elasticsearch cluster, and an ingest pipeline must be defined. This can

examples/dsl/vectors.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
Requirements:
2222
23-
$ pip install nltk sentence_transformers tqdm "elasticsearch"
23+
$ pip install nltk sentence_transformers tqdm elasticsearch
2424
2525
To run the example:
2626

utils/run-unasync-dsl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def main(check=False):
121121
[
122122
"sed",
123123
"-i.bak",
124-
"s/elasticsearch\\[async\\]/elasticsearch/",
124+
's/"elasticsearch\\[async\\]"/elasticsearch/',
125125
f"{output_dir}{file}",
126126
]
127127
)

0 commit comments

Comments
 (0)