geohash

edit

Geohashes are a form of lat/lon encoding which divides the earth up into a grid. Each cell in this grid is represented by a geohash string. Each cell in turn can be further subdivided into smaller cells which are represented by a longer string. So the longer the geohash, the smaller (and thus more accurate) the cell is.

Because geohashes are just strings, they can be stored in an inverted index like any other string, which makes querying them very efficient.

If you enable the geohash option, a geohash “sub-field” will be indexed as, eg .geohash. The length of the geohash is controlled by the geohash_precision parameter.

If the geohash_prefix option is enabled, the geohash option will be enabled automatically.

For example:

PUT my_index
{
  "mappings": {
    "my_type": {
      "properties": {
        "location": {
          "type": "geo_point", 
          "geohash": true
        }
      }
    }
  }
}


PUT my_index/my_type/1
{
  "location": {
    "lat": 41.12,
    "lon": -71.34
  }
}

GET my_index/_search?fielddata_fields=location.geohash 
{
  "query": {
    "prefix": {
      "location.geohash": "drm3b" 
    }
  }
}

A location.geohash field will be indexed for each geo-point.

The geohash can be retrieved with doc_values.

A prefix query can find all geohashes which start with a particular prefix.

A prefix query on geohashes is expensive. Instead, consider using the geohash_prefix to pay the expense once at index time instead of on every query.