From 3195c7df239a232172519bcaa938ca342af2c380 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20Emre=20Arma=C4=9Fan?= Date: Sun, 5 Jan 2025 15:04:08 +0300 Subject: [PATCH 1/3] Add support for the point field --- elasticsearch_dsl/field.py | 4 +++ examples/async/point.py | 71 ++++++++++++++++++++++++++++++++++++++ examples/point.py | 70 +++++++++++++++++++++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 examples/async/point.py create mode 100644 examples/point.py diff --git a/elasticsearch_dsl/field.py b/elasticsearch_dsl/field.py index 55ab4f7f..acbb05e6 100644 --- a/elasticsearch_dsl/field.py +++ b/elasticsearch_dsl/field.py @@ -498,6 +498,10 @@ class GeoPoint(Field): name = "geo_point" +class Point(Field): + name = "point" + + class GeoShape(Field): name = "geo_shape" diff --git a/examples/async/point.py b/examples/async/point.py new file mode 100644 index 00000000..08e78bfe --- /dev/null +++ b/examples/async/point.py @@ -0,0 +1,71 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Example ``Document`` with point datatype. + +In the ``Place`` class we index the place's location as coordinates +to allow spatial queries and distance-based searches. +""" + +import asyncio +import os +from typing import TYPE_CHECKING, Any, Optional + +import elasticsearch_dsl as dsl + + +class Place(dsl.AsyncDocument): + if TYPE_CHECKING: + # definitions here help type checkers understand additional arguments + # that are allowed in the constructor + _id: Optional[int] = dsl.mapped_field(default=None) + + location: Any = dsl.mapped_field(dsl.Point(), default="") + + class Index: + name = "test-point" + + +async def main() -> None: + # initiate the default connection to elasticsearch + dsl.async_connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]]) + + # create the empty index + await Place.init() + + # index some sample data + for id, location in enumerate( + [ + {"type": "Point", "coordinates": [5.43, 32.11]}, + "POINT (-71.34 41.12)", + {"x": -75.22, "y": -3.14}, + [19.12, 52], + "84.12,-54.24", + ], + ): + await Place(_id=id, location=location).save() + + # refresh index manually to make changes live + await Place._index.refresh() + + # close the connection + await dsl.async_connections.get_connection().close() + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/examples/point.py b/examples/point.py new file mode 100644 index 00000000..d7d46630 --- /dev/null +++ b/examples/point.py @@ -0,0 +1,70 @@ +# Licensed to Elasticsearch B.V. under one or more contributor +# license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright +# ownership. Elasticsearch B.V. licenses this file to you under +# the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Example ``Document`` with point datatype. + +In the ``Place`` class we index the place's location as coordinates +to allow spatial queries and distance-based searches. +""" + +import os +from typing import TYPE_CHECKING, Any, Optional + +import elasticsearch_dsl as dsl + + +class Place(dsl.Document): + if TYPE_CHECKING: + # definitions here help type checkers understand additional arguments + # that are allowed in the constructor + _id: Optional[int] = dsl.mapped_field(default=None) + + location: Any = dsl.mapped_field(dsl.Point(), default="") + + class Index: + name = "test-point" + + +def main() -> None: + # initiate the default connection to elasticsearch + dsl.connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]]) + + # create the empty index + Place.init() + + # index some sample data + for id, location in enumerate( + [ + {"type": "Point", "coordinates": [5.43, 32.11]}, + "POINT (-71.34 41.12)", + {"x": -75.22, "y": -3.14}, + [19.12, 52], + "84.12,-54.24", + ], + ): + Place(_id=id, location=location).save() + + # refresh index manually to make changes live + Place._index.refresh() + + # close the connection + dsl.connections.get_connection().close() + + +if __name__ == "__main__": + main() From 068fa3115b785063de363f7d173bd6071e8806df Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Tue, 7 Jan 2025 14:50:22 +0000 Subject: [PATCH 2/3] add shape field, remove unnecessary point examples --- elasticsearch_dsl/field.py | 4 --- examples/async/point.py | 71 -------------------------------------- examples/point.py | 70 ------------------------------------- 3 files changed, 145 deletions(-) delete mode 100644 examples/async/point.py delete mode 100644 examples/point.py diff --git a/elasticsearch_dsl/field.py b/elasticsearch_dsl/field.py index acbb05e6..55ab4f7f 100644 --- a/elasticsearch_dsl/field.py +++ b/elasticsearch_dsl/field.py @@ -498,10 +498,6 @@ class GeoPoint(Field): name = "geo_point" -class Point(Field): - name = "point" - - class GeoShape(Field): name = "geo_shape" diff --git a/examples/async/point.py b/examples/async/point.py deleted file mode 100644 index 08e78bfe..00000000 --- a/examples/async/point.py +++ /dev/null @@ -1,71 +0,0 @@ -# Licensed to Elasticsearch B.V. under one or more contributor -# license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright -# ownership. Elasticsearch B.V. licenses this file to you under -# the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -""" -Example ``Document`` with point datatype. - -In the ``Place`` class we index the place's location as coordinates -to allow spatial queries and distance-based searches. -""" - -import asyncio -import os -from typing import TYPE_CHECKING, Any, Optional - -import elasticsearch_dsl as dsl - - -class Place(dsl.AsyncDocument): - if TYPE_CHECKING: - # definitions here help type checkers understand additional arguments - # that are allowed in the constructor - _id: Optional[int] = dsl.mapped_field(default=None) - - location: Any = dsl.mapped_field(dsl.Point(), default="") - - class Index: - name = "test-point" - - -async def main() -> None: - # initiate the default connection to elasticsearch - dsl.async_connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]]) - - # create the empty index - await Place.init() - - # index some sample data - for id, location in enumerate( - [ - {"type": "Point", "coordinates": [5.43, 32.11]}, - "POINT (-71.34 41.12)", - {"x": -75.22, "y": -3.14}, - [19.12, 52], - "84.12,-54.24", - ], - ): - await Place(_id=id, location=location).save() - - # refresh index manually to make changes live - await Place._index.refresh() - - # close the connection - await dsl.async_connections.get_connection().close() - - -if __name__ == "__main__": - asyncio.run(main()) diff --git a/examples/point.py b/examples/point.py deleted file mode 100644 index d7d46630..00000000 --- a/examples/point.py +++ /dev/null @@ -1,70 +0,0 @@ -# Licensed to Elasticsearch B.V. under one or more contributor -# license agreements. See the NOTICE file distributed with -# this work for additional information regarding copyright -# ownership. Elasticsearch B.V. licenses this file to you under -# the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, -# software distributed under the License is distributed on an -# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY -# KIND, either express or implied. See the License for the -# specific language governing permissions and limitations -# under the License. - -""" -Example ``Document`` with point datatype. - -In the ``Place`` class we index the place's location as coordinates -to allow spatial queries and distance-based searches. -""" - -import os -from typing import TYPE_CHECKING, Any, Optional - -import elasticsearch_dsl as dsl - - -class Place(dsl.Document): - if TYPE_CHECKING: - # definitions here help type checkers understand additional arguments - # that are allowed in the constructor - _id: Optional[int] = dsl.mapped_field(default=None) - - location: Any = dsl.mapped_field(dsl.Point(), default="") - - class Index: - name = "test-point" - - -def main() -> None: - # initiate the default connection to elasticsearch - dsl.connections.create_connection(hosts=[os.environ["ELASTICSEARCH_URL"]]) - - # create the empty index - Place.init() - - # index some sample data - for id, location in enumerate( - [ - {"type": "Point", "coordinates": [5.43, 32.11]}, - "POINT (-71.34 41.12)", - {"x": -75.22, "y": -3.14}, - [19.12, 52], - "84.12,-54.24", - ], - ): - Place(_id=id, location=location).save() - - # refresh index manually to make changes live - Place._index.refresh() - - # close the connection - dsl.connections.get_connection().close() - - -if __name__ == "__main__": - main() From f212f8c2a6fed693ca3acf7312f9e3941ea5df61 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Tue, 7 Jan 2025 15:18:18 +0000 Subject: [PATCH 3/3] add missing Shape declaration --- elasticsearch_dsl/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/elasticsearch_dsl/__init__.py b/elasticsearch_dsl/__init__.py index ebc25ac2..7c0a37b9 100644 --- a/elasticsearch_dsl/__init__.py +++ b/elasticsearch_dsl/__init__.py @@ -73,6 +73,7 @@ RankFeatures, ScaledFloat, SearchAsYouType, + Shape, Short, SparseVector, Text, @@ -183,6 +184,7 @@ "ScaledFloat", "Search", "SearchAsYouType", + "Shape", "Short", "SparseVector", "TermsFacet",