Skip to content

ES|QL Support for ST_GEOHASH, ST_GEOTILE and ST_GEOHEX #125143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 51 commits into from
Jun 3, 2025

Conversation

craigtaverner
Copy link
Contributor

@craigtaverner craigtaverner commented Mar 18, 2025

In support of the feature request at #123903

Checklist:

  • Prototype
    • Implement basic ST_GEOHASH on points
    • Add support for optional bounds parameter
    • Support multivalues (incoming and outgoing)
    • Add support for ST_GEOTILE
    • Add support for ST_GEOHEX
  • Productize:
    • Use bounded predicates to filter by tile, not geometry
      • Use GeoTileBoundedPredicate for correct geotile bounds filtering
      • Use GeohashBoundedPredicate for correct geohash bounds filtering
      • Copy GeoHexCellIdSource.GeoHexPredicate and H3SphericalUtil classes into esql module and use them for correct H3 bounds filtering
    • Refine API
      • Change functions to return long grid-ids instead of keyword for performance and add new functions for keyword alternatives
      • Create conversion functions between long and keyword versions of the grid-ids
      • Create functions to generate geometries from the grid-ids
    • Additional refinements
      • Add unit tests and generate docs
      • Tests for geo_grid queries (| WHERE ST_GEOHEX(location, 1) == ST_GEOHEX_TO_LONG("812bbffffffffff"))
      • Check input parameters for validity (eg. negative precision)
      • Errors tests
      • Precision range validation
      • Remove support for _TO_GEOSHAPE functions
      • Remove support for dynamic precision (from field or calculated values)
      • Test coverage of bounds (and get bounds types into the docs)
      • Restrict bounds parameter to only geo_shape of an envelope (no hidden envelope calculation)
      • Test that prove docs-values code path works (ie. function before spatial stats)
      • Add license checks for geohex grid
      • Docs links to query dsl
  • Extra features (could be done here or in later PRs)
    • Bring back support for _TO_GEOSHAPE functions, including enhanced WKB (include grid-id)
    • Lucene pushdown for geo_grid queries (see GeoGridQueryBuilder, and copy to ES|QL)
    • Expand support to geo_shape fields (including license checks)
    • Add pushdown to Lucene for grid aggregations
    • Bring back support for dynamic precision (from field or calculated values)
    • Non-foldable bounds

Fixes #123903

}

protected static BytesRef calculateGeotile(Point point, int precision, Rectangle bounds) {
// For points, filtering the point is as good as filtering the tile
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. The current code was a quick-and dirty placeholder for the draft PR. I'll definitely switch over to the better code you suggest.

@iverase
Copy link
Contributor

iverase commented Apr 9, 2025

I was trying queries like:

ROW location=TO_GEOPOINT("POINT(0 0)")| EVAL hex=ST_GEOHEX(location, -1)

In case of geohex and geotile we provide a good error although looking into the stack trace it feels to me that we are not checking the input while parsing and we only fail when trying to apply the function to the point. More over, for geohash it does not throw an error.

Let's improve the checking of input parameters.

FROM airports
| EVAL points = ["POINT(0.0 30.0)", "POINT(12.0 60.0)"]
| EVAL geohash = ST_GEOHASH(location, 2, TO_GEOPOINT(points))
| WHERE geohash IS NOT NULL
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I understand now. It is strange how the bounds are defined here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Support for bounds defined by points is now removed.

Copy link
Contributor

@iverase iverase left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I think we need to strength the test coverage for multi-value case but we can do it in a follow up

@craigtaverner craigtaverner added v8.19.0 auto-backport Automatically create backport pull requests when merged labels Jun 3, 2025
@craigtaverner craigtaverner merged commit 11f0c55 into elastic:main Jun 3, 2025
18 checks passed
@elasticsearchmachine
Copy link
Collaborator

💔 Backport failed

Status Branch Result
8.19 Commit could not be cherrypicked due to conflicts

You can use sqren/backport to manually backport by running backport --upstream elastic/elasticsearch --pr 125143

joshua-adams-1 pushed a commit to joshua-adams-1/elasticsearch that referenced this pull request Jun 3, 2025
Added support for the three primary scalar grid functions:
* `ST_GEOHASH(geom, precision)`
* `ST_GEOTILE(geom, precision)`
* `ST_GEOHEX(geom, precision)`

As well as versions of these three that take an optional `geo_shape` boundary (must be a `BBOX` ie. `Rectangle`).

And also supporting conversion functions that convert the grid-id from long to string and back to long.

This work represents the core of the feature to support geo-grid aggregations in ES|QL.
craigtaverner added a commit to craigtaverner/elasticsearch that referenced this pull request Jun 4, 2025
Added support for the three primary scalar grid functions:
* `ST_GEOHASH(geom, precision)`
* `ST_GEOTILE(geom, precision)`
* `ST_GEOHEX(geom, precision)`

As well as versions of these three that take an optional `geo_shape` boundary (must be a `BBOX` ie. `Rectangle`).

And also supporting conversion functions that convert the grid-id from long to string and back to long.

This work represents the core of the feature to support geo-grid aggregations in ES|QL.
craigtaverner added a commit that referenced this pull request Jun 5, 2025
… (#128938)

* ES|QL Support for ST_GEOHASH, ST_GEOTILE and ST_GEOHEX (#125143)

Added support for the three primary scalar grid functions:
* `ST_GEOHASH(geom, precision)`
* `ST_GEOTILE(geom, precision)`
* `ST_GEOHEX(geom, precision)`

As well as versions of these three that take an optional `geo_shape` boundary (must be a `BBOX` ie. `Rectangle`).

And also supporting conversion functions that convert the grid-id from long to string and back to long.

This work represents the core of the feature to support geo-grid aggregations in ES|QL.

* Remove license checking mechanism from tests (unsupported in 8.x)

* Add 8.x level generated docs

* Link new spatial functions into the docs

* Remove unused imports after removal of license level tests

* Fix list syntax

* Fix asciidoc links
Samiul-TheSoccerFan pushed a commit to Samiul-TheSoccerFan/elasticsearch that referenced this pull request Jun 5, 2025
Added support for the three primary scalar grid functions:
* `ST_GEOHASH(geom, precision)`
* `ST_GEOTILE(geom, precision)`
* `ST_GEOHEX(geom, precision)`

As well as versions of these three that take an optional `geo_shape` boundary (must be a `BBOX` ie. `Rectangle`).

And also supporting conversion functions that convert the grid-id from long to string and back to long.

This work represents the core of the feature to support geo-grid aggregations in ES|QL.
alex-spies added a commit that referenced this pull request Jun 23, 2025
#125143 added 9 spatial grid functions and released them into Serverless. We think this is not the best long-term approach and the functions in #129581 are likely better.

As a first step, rmove the spatial grid functions added in #125143 from release builds so they don't get released into 8.19/9.1.

---------

Co-authored-by: Craig Taverner <[email protected]>
alex-spies added a commit to alex-spies/elasticsearch that referenced this pull request Jun 23, 2025
elastic#125143 added 9 spatial grid functions and released them into Serverless. We think this is not the best long-term approach and the functions in elastic#129581 are likely better.

As a first step, rmove the spatial grid functions added in elastic#125143 from release builds so they don't get released into 8.19/9.1.

---------

Co-authored-by: Craig Taverner <[email protected]>
(cherry picked from commit efb1397)

# Conflicts:
#	docs/reference/query-languages/esql/_snippets/lists/spatial-functions.md
#	docs/reference/query-languages/esql/functions-operators/spatial-functions.md
alex-spies added a commit that referenced this pull request Jun 23, 2025
#125143 added 9 spatial grid functions and released them into Serverless. We think this is not the best long-term approach and the functions in #129581 are likely better.

As a first step, remove the spatial grid functions added in #125143 from release builds so they don't get released into 8.19/9.1.

---------

Co-authored-by: Craig Taverner <[email protected]>
(cherry picked from commit efb1397)

# Conflicts:
#	docs/reference/query-languages/esql/_snippets/lists/spatial-functions.md
#	docs/reference/query-languages/esql/functions-operators/spatial-functions.md
mridula-s109 pushed a commit to mridula-s109/elasticsearch that referenced this pull request Jun 25, 2025
elastic#125143 added 9 spatial grid functions and released them into Serverless. We think this is not the best long-term approach and the functions in elastic#129581 are likely better.

As a first step, rmove the spatial grid functions added in elastic#125143 from release builds so they don't get released into 8.19/9.1.

---------

Co-authored-by: Craig Taverner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Analytics/ES|QL AKA ESQL :Analytics/Geo Indexing, search aggregations of geo points and shapes auto-backport Automatically create backport pull requests when merged backport pending >enhancement Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v8.19.0 v9.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Spatial ES|QL Grid Aggregations
3 participants