elastic / elasticsearch

Free and Open, Distributed, RESTful Search Engine
https://www.elastic.co/products/elasticsearch
Other
68.55k stars 24.35k forks source link

Support spatial distance search in ES|QL (ST_DISTANCE) #108212

Open craigtaverner opened 2 months ago

craigtaverner commented 2 months ago

Description

The Elasticsearch _search API supports distance filtering and sorting:

In PostGIS there are two related functions:

These features can be implemented in ES|QL, but we believe we do not need to implement both functions, as we can perform Lucene-pushdown optimization identically in both cases, so we'll implement the more generic function only: double ST_DISTANCE(geomA, geomB).

This can be used for calculating a distance, filter and sorting based on the context in the query:

Distance calculations

FROM airports
| EVAL distance = ST_DISTANCE(location, city_location)

Filtering results

FROM airports
| WHERE ST_DISTANCE(location, TO_GEOPOINT("POINT(12 55)")) < 500000

Sorting results

FROM airports
| EVAL distance = ST_DISTANCE(location, TO_GEOPOINT("POINT(12 55)"))
| SORT distance ASC
### Tasks
- [x] Review existing Elasticsearch support for distance filtering and sorting
- [x] Review PostGIS and OGC specifications for these functions
- [x] Implement raw ST_DISTANCE function
- [x] Support distance filtering
- [x] Support distance calculations
- [x] Support distance sorting
- [ ] https://github.com/elastic/elasticsearch/issues/109972
- [ ] https://github.com/elastic/elasticsearch/issues/109973
- [x] Documentation
elasticsearchmachine commented 2 months ago

Pinging @elastic/es-analytical-engine (Team:Analytics)

craigtaverner commented 2 months ago

We should also keep in mind use cases involving speed, as discussed at https://github.com/elastic/elasticsearch/issues/108332#issuecomment-2097795599