Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search convert geohash_grid key to lat lon

Using geohash_grid aggs in elasticsearch return this result:

"aggregations": {
  "Geo-hash": {
     "buckets": [
        {
           "key": "gcw05r6xz7cb",
           "doc_count": 305
        },
        {
           "key": "gcw2hzkpf7b8",
           "doc_count": 12
        }
     ]
  }

}

1)The key represent the center point of cell?

2)How to convert this key to lat lon? - I found some plugins is there anyway to avoid using plugins (not support all version , needs maintenance,etc.) and query directly geo ponts from elasticsearch?

like image 329
User1234 Avatar asked Oct 30 '22 05:10

User1234


2 Answers

To answer your question:

  1. The geohashes in the key represent an area. Please read this for more details about geohashes: https://www.elastic.co/guide/en/elasticsearch/guide/current/geohashes.html

  2. From the last section in this elasticsearch documentation about geohash grid aggregation, you need a library to convert a geohash into the equivalent bounding box or central point. https://www.elastic.co/guide/en/elasticsearch/guide/current/geohash-grid-agg.html#geohash-grid-agg

You may explore Geo Bounds Aggregation to perform similar job, but I prefer convert it at client side.

like image 174
philipskokoh Avatar answered Nov 15 '22 07:11

philipskokoh


The only way as i described in my question is to use one of many plugins a cross the web, as this one

Any way if your data-set and cardinality is not too large you can use top hits aggs as sub aggs and then extract the geo-point field from the document. That can be very expensive operation, use it wisely on small data-sets :)

   {
  "size": 0, 
  "aggs": {
    "geo": {
      "geohash_grid": {
        "field": "locationField",
        "precision": 3
      }
      ,
  "aggs": {
    "NAME": {
      "top_hits": {
        "size": 1
      }
    }
  }
    }
  }
}
like image 32
User1234 Avatar answered Nov 15 '22 09:11

User1234