﻿---
title: ES|QL ST_GEOHEX function
description: 
url: https://www.elastic.co/elastic/docs-builder/docs/2995/reference/query-languages/esql/functions-operators/spatial-functions/st_geohex
products:
  - Elasticsearch
---

# ES|QL ST_GEOHEX function
<applies-to>
  - Elastic Cloud Serverless: Preview
  - Elastic Stack: Preview since 9.2
</applies-to>


## Syntax

![Embedded](https://www.elastic.co/elastic/docs-builder/docs/2995/reference/query-languages/esql/images/functions/st_geohex.svg)


## Parameters

<definitions>
  <definition term="geometry">
    Expression of type `geo_point`. If `null`, the function returns `null`.
  </definition>
  <definition term="precision">
    Expression of type `integer`. If `null`, the function returns `null`. Valid values are between [0 and 15](https://h3geo.org/docs/core-library/restable/).
  </definition>
  <definition term="bounds">
    Optional bounds to filter the grid tiles, a `geo_shape` of type `BBOX`. Use [`ST_ENVELOPE`](https://www.elastic.co/elastic/docs-builder/docs/2995/reference/query-languages/esql/functions-operators/spatial-functions/st_envelope) if the `geo_shape` is of any other type.
  </definition>
</definitions>


## Description

Calculates the `geohex`, the H3 cell-id, of the supplied geo_point at the specified precision. The result is long encoded. Use [`TO_STRING`](https://www.elastic.co/elastic/docs-builder/docs/2995/reference/query-languages/esql/functions-operators/type-conversion-functions/to_string) to convert the result to a string, [`TO_LONG`](https://www.elastic.co/elastic/docs-builder/docs/2995/reference/query-languages/esql/functions-operators/type-conversion-functions/to_long) to convert it to a `long`, or [`TO_GEOSHAPE`](https://www.elastic.co/elastic/docs-builder/docs/2995/reference/query-languages/esql/functions-operators/type-conversion-functions/to_geoshape) to calculate the `geo_shape` bounding geometry.  These functions are related to the [`geo_grid` query](https://www.elastic.co/elastic/docs-builder/docs/2995/reference/query-languages/query-dsl/query-dsl-geo-grid-query) and the [`geohex_grid` aggregation](https://www.elastic.co/elastic/docs-builder/docs/2995/reference/aggregations/search-aggregations-bucket-geohexgrid-aggregation).

## Supported types


| geometry  | precision | bounds    | result |
|-----------|-----------|-----------|--------|
| geo_point | integer   | geo_shape | geohex |
| geo_point | integer   |           | geohex |


## Example

```esql
FROM airports
| EVAL geohex = ST_GEOHEX(location, 1)
| STATS
    count = COUNT(geohex),
    centroid = ST_CENTROID_AGG(location)
      BY geohex
| WHERE count >= 10
| EVAL geohexString = TO_STRING(geohex)
| KEEP count, centroid, geohexString
| SORT count DESC, geohexString ASC
```


| count:long | centroid:geo_point                             | geohexString:keyword |
|------------|------------------------------------------------|----------------------|
| 22         | POINT (7.250850197689777 48.21363834643059)    | 811fbffffffffff      |
| 18         | POINT (-80.64959161449224 40.04119813675061)   | 812abffffffffff      |
| 17         | POINT (-0.7606179875266903 52.86413913565304)  | 81197ffffffffff      |
| 13         | POINT (22.53157936179867 41.98255742864254)    | 811efffffffffff      |
| 13         | POINT (78.30096947387435 26.073904778951636)   | 813dbffffffffff      |
| 12         | POINT (-76.39781514415517 45.16300531569868)   | 812bbffffffffff      |
| 12         | POINT (-100.30120467301458 20.114154297625646) | 8149bffffffffff      |
| 11         | POINT (18.037187419831753 48.66540593306788)   | 811e3ffffffffff      |
| 11         | POINT (-83.42379064553164 33.18388901439241)   | 8144fffffffffff      |
| 11         | POINT (-99.4237939513881 27.100012352774765)   | 8148bffffffffff      |
| 10         | POINT (128.01009018346667 35.8699960866943)    | 8130fffffffffff      |