Skip to content

Commit 4ba106b

Browse files
committed
Merge remote-tracking branch 'origin/main' into querying-docs
2 parents 2f5db74 + 470896e commit 4ba106b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+4045
-883
lines changed

docs/docset.yml

Lines changed: 0 additions & 478 deletions
Large diffs are not rendered by default.
File renamed without changes.
File renamed without changes.

docs/reference/_how_to_guides.md renamed to docs/reference/dsl_how_to_guides.md

Lines changed: 118 additions & 97 deletions
Large diffs are not rendered by default.

docs/reference/dsl_migrating.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Migrating from the `elasticsearch-dsl` package [_migrating_from_elasticsearch_dsl_package]
2+
3+
In the past the Elasticsearch Python DSL module was distributed as a standalone package called `elasticsearch-dsl`. This package is now deprecated, since all its functionality has been integrated into the main Python client. We recommend all developers to migrate their applications and eliminate their dependency on the `elasticsearch-dsl` package.
4+
5+
To migrate your application, all references to `elasticsearch_dsl` as a top-level package must be changed to `elasticsearch.dsl`. In other words, the underscore from the package name should be replaced by a period.
6+
7+
Here are a few examples:
8+
9+
```python
10+
# from:
11+
from elasticsearch_dsl import Date, Document, InnerDoc, Text, connections
12+
# to:
13+
from elasticsearch.dsl import Date, Document, InnerDoc, Text, connections
14+
15+
# from:
16+
from elasticsearch_dsl.query import MultiMatch
17+
# to:
18+
from elasticsearch.dsl.query import MultiMatch
19+
20+
# from:
21+
import elasticsearch_dsl as dsl
22+
# to:
23+
from elasticsearch import dsl
24+
25+
# from:
26+
import elasticsearch_dsl
27+
# to:
28+
from elasticsearch import dsl as elasticsearch_dsl
29+
30+
# from:
31+
import elasticsearch_dsl
32+
# to:
33+
from elasticsearch import dsl
34+
# and replace all references to "elasticsearch_dsl" in the code with "dsl"
35+
```

docs/reference/_tutorials.md renamed to docs/reference/dsl_tutorials.md

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,17 @@ Let’s rewrite the example using the DSL module:
4747

4848
```python
4949
from elasticsearch import Elasticsearch
50-
from elasticsearch.dsl import Search
50+
from elasticsearch.dsl import Search, query, aggs
5151

5252
client = Elasticsearch("https://localhost:9200")
5353

5454
s = Search(using=client, index="my-index") \
55-
.filter("term", category="search") \
56-
.query("match", title="python") \
57-
.exclude("match", description="beta")
55+
.query(query.Match("title", "python")) \
56+
.filter(query.Term("category", "search")) \
57+
.exclude(query.Match("description", "beta"))
5858

59-
s.aggs.bucket('per_tag', 'terms', field='tags') \
60-
.metric('max_lines', 'max', field='lines')
59+
s.aggs.bucket('per_tag', aggs.Terms(field="tags")) \
60+
.metric('max_lines', aggs.Max(field='lines'))
6161

6262
response = s.execute()
6363

@@ -68,9 +68,9 @@ for tag in response.aggregations.per_tag.buckets:
6868
print(tag.key, tag.max_lines.value)
6969
```
7070

71-
As you see, the library took care of:
71+
As you see, the DSL module took care of:
7272

73-
* creating appropriate `Query` objects by name (eq. "match")
73+
* creating appropriate `Query` objects from classes
7474
* composing queries into a compound `bool` query
7575
* putting the `term` query in a filter context of the `bool` query
7676
* providing a convenient access to response data
@@ -89,19 +89,19 @@ from elasticsearch.dsl import Document, Date, Integer, Keyword, Text, connection
8989
connections.create_connection(hosts="https://localhost:9200")
9090

9191
class Article(Document):
92-
title = Text(analyzer='snowball', fields={'raw': Keyword()})
93-
body = Text(analyzer='snowball')
94-
tags = Keyword()
95-
published_from = Date()
96-
lines = Integer()
92+
title: str = mapped_field(Text(analyzer='snowball', fields={'raw': Keyword()}))
93+
body: str = mapped_field(Text(analyzer='snowball'))
94+
tags: str = mapped_field(Keyword())
95+
published_from: datetime
96+
lines: int
9797

9898
class Index:
9999
name = 'blog'
100100
settings = {
101101
"number_of_shards": 2,
102102
}
103103

104-
def save(self, ** kwargs):
104+
def save(self, **kwargs):
105105
self.lines = len(self.body.split())
106106
return super(Article, self).save(** kwargs)
107107

@@ -127,7 +127,7 @@ print(connections.get_connection().cluster.health())
127127
In this example you can see:
128128

129129
* providing a default connection
130-
* defining fields with mapping configuration
130+
* defining fields with Python type hints and additional mapping configuration when necessary
131131
* setting index name
132132
* defining custom methods
133133
* overriding the built-in `.save()` method to hook into the persistence life cycle
@@ -141,12 +141,6 @@ You can see more in the `persistence` chapter.
141141

142142
If you have your `Document`s defined you can very easily create a faceted search class to simplify searching and filtering.
143143

144-
::::{note}
145-
This feature is experimental and may be subject to change.
146-
147-
::::
148-
149-
150144
```python
151145
from elasticsearch.dsl import FacetedSearch, TermsFacet, DateHistogramFacet
152146

@@ -208,11 +202,12 @@ Using the DSL, we can now express this query as such:
208202
```python
209203
from elasticsearch import Elasticsearch
210204
from elasticsearch.dsl import Search, UpdateByQuery
205+
from elasticsearch.dsl.query import Match
211206

212207
client = Elasticsearch()
213208
ubq = UpdateByQuery(using=client, index="my-index") \
214-
.query("match", title="python") \
215-
.exclude("match", description="beta") \
209+
.query(Match("title", "python")) \
210+
.exclude(Match("description", "beta")) \
216211
.script(source="ctx._source.likes++", lang="painless")
217212

218213
response = ubq.execute()
@@ -232,7 +227,7 @@ body = {...} # insert complicated query here
232227
s = Search.from_dict(body)
233228

234229
# Add some filters, aggregations, queries, ...
235-
s.filter("term", tags="python")
230+
s.filter(query.Term("tags", "python"))
236231

237232
# Convert back to dict to plug back into existing code
238233
body = s.to_dict()

docs/reference/elasticsearch-dsl.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ Elasticsearch DSL is a module of the official Python client that aims to help wi
99

1010
```python
1111
from elasticsearch.dsl import Search
12+
from elasticsearch.dsl.query import Match, Term
1213

1314
s = Search(index="my-index") \
14-
.filter("term", category="search") \
15-
.query("match", title="python") \
16-
.exclude("match", description="beta")
15+
.query(Match("title", "python")) \
16+
.filter(Term("category", "search")) \
17+
.exclude(Match("description", "beta"))
1718
for hit in s:
1819
print(hit.title)
1920
```
@@ -22,12 +23,13 @@ Or with asynchronous Python:
2223

2324
```python
2425
from elasticsearch.dsl import AsyncSearch
26+
from elasticsearch.dsl.query import Match, Term
2527

2628
async def run_query():
2729
s = AsyncSearch(index="my-index") \
28-
.filter("term", category="search") \
29-
.query("match", title="python") \
30-
.exclude("match", description="beta")
30+
.query(Match("title", "python")) \
31+
.filter(Term("category", "search")) \
32+
.exclude(Match("description", "beta"))
3133
async for hit in s:
3234
print(hit.title)
3335
```

docs/reference/getting-started.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ client = Elasticsearch(
4141

4242
Your Elasticsearch endpoint can be found on the **My deployment** page of your deployment:
4343

44-
:::{image} ../images/es-endpoint.jpg
44+
:::{image} images/es-endpoint.jpg
4545
:alt: Finding Elasticsearch endpoint
4646
:::
4747

4848
You can generate an API key on the **Management** page under Security.
4949

50-
:::{image} ../images/create-api-key.png
50+
:::{image} images/create-api-key.png
5151
:alt: Create API key
5252
:::
5353

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)