darren
darren

Reputation: 19399

use max/min aggregation on a geo_point field type

Is it possible to use a max or min aggregation on a geo_point field?

I've tried to max directly onto my coordinate property which is of type geo_point

{
    "size": 0,
    "aggs" : {
        "max_lat" : { "max" : { "field" : "coordinate" } }
    }
}

this understandably returns a ClassCastException and so I tried to run the query directly on the lat or lon fields of coordinate which are of type double.

{
    "size": 0,
    "aggs" : {
        "max_lat" : { "max" : { "field" : "coordinate.lat" } },
        "max_lon" : { "max" : { "field" : "coordinate.lon" } }
    }
}

Now I don't receive the ClassCastExcxeption and the query returns a 200 however the aggregations are null.

Response:

{
    "aggregations": {
        "max_lat": {
            "value": null
        },
        "max_lon": {
            "value": null
        }
    }
}

Using elasticsearch v1.7

Upvotes: 0

Views: 235

Answers (1)

Carl
Carl

Reputation: 324

You can use the following script aggregations:

{
  "size": 0,
  "aggs": {
    "min_lat": { "min": { "script": "doc['coordinate'].lat" } },
    "max_lat": { "max": { "script": "doc['coordinate'].lat" } },
    "min_lon": { "min": { "script": "doc['coordinate'].lon" } },
    "max_lon": { "max": { "script": "doc['coordinate'].lon" } }
  }
}

I am not sure how you are using the results, but the following may also be helpful to avoid using a script:

"viewport": { "geo_bounds": { "field": "coordinate" } }
"centroid": { "geo_centroid": { "field": "coordinate" } }

Upvotes: 0

Related Questions