代码之家  ›  专栏  ›  技术社区  ›  darren

在geo_点字段类型上使用最大/最小聚合

  •  0
  • darren  · 技术社区  · 8 年前

    我试过了 max 直接映射到类型为的坐标属性上 geo_point

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

    这可以理解地返回一个 ClassCastException lat lon 字段 coordinate 都是双色的。

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

    现在我没有收到classcastexoption,查询返回200,但是聚合为null。

    答复:

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

    使用elasticsearch v1.7

    1 回复  |  直到 8 年前
        1
  •  0
  •   Carl    8 年前

    您可以使用以下脚本聚合:

    {
      "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" } }
      }
    }
    

    我不确定您是如何使用结果的,但以下内容也可能有助于避免使用脚本:

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