Skip to content

Commit 34ca004

Browse files
committed
Add support for the point field
1 parent 5a2cc86 commit 34ca004

File tree

4 files changed

+147
-0
lines changed

4 files changed

+147
-0
lines changed

elasticsearch_dsl/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
Nested,
6868
Object,
6969
Percolator,
70+
Point,
7071
RangeField,
7172
RankFeature,
7273
RankFeatures,
@@ -169,6 +170,7 @@
169170
"NestedFacet",
170171
"Object",
171172
"Percolator",
173+
"Point",
172174
"Q",
173175
"Query",
174176
"Range",

elasticsearch_dsl/field.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,10 @@ class GeoPoint(Field):
490490
name = "geo_point"
491491

492492

493+
class Point(Field):
494+
name = "point"
495+
496+
493497
class GeoShape(Field):
494498
name = "geo_shape"
495499

examples/async/point.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
"""
19+
Example ``Document`` with point datatype.
20+
21+
In the ``Place`` class we index the place's location as coordinates
22+
to allow spatial queries and distance-based searches.
23+
"""
24+
25+
import asyncio
26+
import os
27+
from typing import TYPE_CHECKING, Any, Optional
28+
29+
import elasticsearch_dsl as dsl
30+
31+
32+
class Place(dsl.AsyncDocument):
33+
if TYPE_CHECKING:
34+
# definitions here help type checkers understand additional arguments
35+
# that are allowed in the constructor
36+
_id: Optional[int] = dsl.mapped_field(default=None)
37+
38+
location: Any = dsl.mapped_field(dsl.Point(), default="")
39+
40+
class Index:
41+
name = "test-point"
42+
43+
44+
async def main() -> None:
45+
# initiate the default connection to elasticsearch
46+
dsl.async_connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
47+
48+
# create the empty index
49+
await Place.init()
50+
51+
# index some sample data
52+
for id, location in enumerate(
53+
[
54+
{"type": "Point", "coordinates": [5.43, 32.11]},
55+
"POINT (-71.34 41.12)",
56+
{"x": -75.22, "y": -3.14},
57+
[19.12, 52],
58+
"84.12,-54.24",
59+
],
60+
):
61+
await Place(_id=id, location=location).save()
62+
63+
# refresh index manually to make changes live
64+
await Place._index.refresh()
65+
66+
# close the connection
67+
await dsl.async_connections.get_connection().close()
68+
69+
70+
if __name__ == "__main__":
71+
asyncio.run(main())

examples/point.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
"""
19+
Example ``Document`` with point datatype.
20+
21+
In the ``Place`` class we index the place's location as coordinates
22+
to allow spatial queries and distance-based searches.
23+
"""
24+
25+
import os
26+
from typing import TYPE_CHECKING, Any, Optional
27+
28+
import elasticsearch_dsl as dsl
29+
30+
31+
class Place(dsl.Document):
32+
if TYPE_CHECKING:
33+
# definitions here help type checkers understand additional arguments
34+
# that are allowed in the constructor
35+
_id: Optional[int] = dsl.mapped_field(default=None)
36+
37+
location: Any = dsl.mapped_field(dsl.Point(), default="")
38+
39+
class Index:
40+
name = "test-point"
41+
42+
43+
def main() -> None:
44+
# initiate the default connection to elasticsearch
45+
dsl.connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]])
46+
47+
# create the empty index
48+
Place.init()
49+
50+
# index some sample data
51+
for id, location in enumerate(
52+
[
53+
{"type": "Point", "coordinates": [5.43, 32.11]},
54+
"POINT (-71.34 41.12)",
55+
{"x": -75.22, "y": -3.14},
56+
[19.12, 52],
57+
"84.12,-54.24",
58+
],
59+
):
60+
Place(_id=id, location=location).save()
61+
62+
# refresh index manually to make changes live
63+
Place._index.refresh()
64+
65+
# close the connection
66+
dsl.connections.get_connection().close()
67+
68+
69+
if __name__ == "__main__":
70+
main()

0 commit comments

Comments
 (0)