Search code examples
elasticsearchgeolocationgeospatialgeohashing

Geohash not generated in Elastic search


I have created an index as follows:

POST /cabtrails
{
  "settings" :
{
  "number_of_shards" : 3,
  "number_of_replicas" : 1
 },
"mappings" : {
"cabtrail" :{
  "properties" : {
        "location": {
            "type":               "geo_point",
            "geohash_prefix":     true, 
            "geohash_precision":  "5m" 
          },
          "capture_time": {
                "type" : "long"

            },
          "client_id": {
            "type" : "long"

          }
       }
     }
   }
}

It works fine and an index is created.

I have entered one sample document as:

POST cabtrails/cabtrail
{
    "capture_time": 1431849367077,
    "client_id": 865527029812357,
    "location": "13.0009316,77.5947316"
}

This works fine as well. Here, I am expecting that ElasticSearch will generate a geohash field/entry which I can use.

But, when I query, I get this:

GET cabtrails/_search
{
   "took": 2,
   "timed_out": false,
   "_shards": {
      "total": 3,
      "successful": 3,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 1,
      "hits": [
         {
            "_index": "cabtrails",
            "_type": "cabtrail",
            "_id": "AU2LrEaze2aqxPjHm-UI",
            "_score": 1,
            "_source": {
               "capture_time": 1431849367077,
           "client_id": 865527029812357,
           "location": "13.0009316,77.5947316"
        }
     }
      ]
   }
}

I am expecting a geo-hash string like u10hbp somewhere in the query result, which I can use to query future location points. Or my concepts of geohash+ES are messed up?? Help!!


Solution

  • As per the document enabling geohash flag in geo_point indexes the geohash value .

    There is a difference in what is indexed and what the _source field of the response represents.

    The _source field in the response is the original raw json document that is passed for indexing to elasticsearch.

    When you enable geohash flag the geo_point type is indexed using the geohash representation but the actual source document is not altered

    To get an idea of how geohash flag complements the way geo_point type gets indexed you could probably use the fielddata_fields api:

    For the above example it would look something on these lines :

    **Query**
    POST cabtrails/_search
    {
    
        "fielddata_fields" :["location","location.geohash"]
    }
    

    Response:

    "hits": {
          "total": 1,
          "max_score": 1,
          "hits": [
             {
                "_index": "cabtrails",
                "_type": "cabtrail",
                "_id": "AU2NRn_OsXnJTKeurUsn",
                "_score": 1,
                "_source": {
                   "capture_time": 1431849367077,
                   "client_id": 865527029812357,
                   "location": "13.0009316,77.5947316"
                },
                "fields": {
                   "location": [
                      {
                         "lat": 13.0009316,
                         "lon": 77.5947316
                      }
                   ],
                   "location.geohash": [
                      "t",
                      "td",
                      "tdr",
                      "tdr1",
                      "tdr1v",
                      "tdr1vw",
                      "tdr1vww",
                      "tdr1vwwz",
                      "tdr1vwwzb",
                      "tdr1vwwzbm"
                   ]
                }
             }
          ]
       }