0
votes

Is there a way to aggregate on a geo_point field and to receive the actual lat long? all i managed to do is get the hash geo. what i did so far: creating the index

PUT geo_test
{
  "mappings": {
    "sharon_test": {
      "properties": {
        "location": {
          "type": "geo_point"
        }
      }
    }
  }
}

adding X docs with different lat long

POST geo_test/sharon_test
{
  "location": { 
    "lat": 45,
    "lon": -7
  }
}

ran this agg:

GET geo_test/sharon_test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ]
    }
  },
  "aggs": {
    "locationsAgg": {
      "geohash_grid": {
        "field": "location",
                "precision" : 12
      }
    }
  }
}

i got this result:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "geo_test",
        "_type": "sharon_test",
        "_id": "fGb4uGEBfEDTRjcEmr6i",
        "_score": 1,
        "_source": {
          "location": {
            "lat": 41.12,
            "lon": -71.34
          }
        }
      },
      {
        "_index": "geo_test",
        "_type": "sharon_test",
        "_id": "oWb4uGEBfEDTRjcE7b6R",
        "_score": 1,
        "_source": {
          "location": {
            "lat": 4,
            "lon": -7
          }
        }
      }
    ]
  },
  "aggregations": {
    "locationsAgg": {
      "buckets": [
        {
          "key": "ebenb8nv8nj9",
          "doc_count": 1
        },
        {
          "key": "drm3btev3e86",
          "doc_count": 1
        }
      ]
    }
  }
}

I want to know if i can get one of the 2: 1. convert the "key" that is currently representing as a geopoint hash to the sources lat/long 2. show the lat, long in the aggregation in the first place

Thanks! P.S I also tried the other geo aggregations but all they give me is the number of docs that fit my aggs conditions, i need the actual values E.G wanted this aggregation to return all the locations i had in my index, but it only returned the count

GET geo_test/sharon_test/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_all": {}
        }
      ]
    }
  },
  "aggs": {
    "distanceRanges": {
      "geo_distance": {
        "field": "location",
        "origin": "50.0338, 36.2242 ",
        "unit": "meters",
        "ranges": [
          {
            "key": "All Locations",
            "from": 1
          }
        ]
      }
    }
  }
}
1

1 Answers

0
votes

You can actually use geo_bounds inside the geo_hash to get a bounding box to narrow it down precisely but to get the exact location you will need to decode the geohash

GET geo_test/sharon_test/_search
{
   "query":{
      "bool":{
         "must":[
            {
               "match_all":{

               }
            }
         ]
      }
   },
   "aggs":{
      "locationsAgg":{
         "geohash_grid":{
            "field":"location",
            "precision":12
         },
         "aggs":{
            "cell":{
               "geo_bounds":{
                  "field":"location"
               }
            }
         }
      }
   }
}