Elasticsearch 按地理位置聚合

Elasticsearch aggregate by geo location

我想使用形状和边界框按位置对数据进行分组。例如按半球对结果进行分组:

GET cdc/_search
{
   "from": 0,
   "size": 0,
   "query": {
      "match_all": {}
   },
   "aggs": {
      "group_by_geo": {
         "geo_bounding_box": {
            "field": "location",
            "boxes": [
               {
                  "top_left": {
                     "lat": 90,
                     "lon": -180
                  },
                  "bottom_right": {
                     "lat": 0,
                     "lon": 0
                  }
               },
               {
                  "top_left": {
                     "lat": 90,
                     "lon": 0
                  },
                  "bottom_right": {
                     "lat": 0,
                     "lon": 180
                  }
               },
               {
                  "top_left": {
                     "lat": 0,
                     "lon": -180
                  },
                  "bottom_right": {
                     "lat": -90,
                     "lon": 0
                  }
               },
               {
                  "top_left": {
                     "lat": 0,
                     "lon": 0
                  },
                  "bottom_right": {
                     "lat": -90,
                     "lon": 180
                  }
               }
            ]
         },
         "aggs": {
            "over_time": {
               "date_histogram": {
                  "field": "date",
                  "interval": "day",
                  "format": "yyyy-MM-dd"
               },
               "aggs": {
                  "AvgTemp": {
                     "avg": {
                        "field": "hits"
                     }
                  }
               }
            }
         }
      }
   }
}

所以 over_time 部分独立运行但这个查询给了我 "reason": "Could not find aggregator type [geo_bounding_box] in [group_by_geo]"

我的语法受到 range aggregations 的启发,但显然这在这种情况下不起作用。

这可能吗,还是我必须通过 4 个框进行单独的查询过滤?

没有 geo_bounding_box 聚合(不过有 geo_bounding_box 过滤器)。

您可以为每个边界框使用 filters aggregation containing one geo_bounding_box filter 来实现您的需要。基本上是这样的:

{
  "from": 0,
  "size": 0,
  "query": {
    "match_all": {}
  },
  "aggs": {
    "group_by_geo": {
      "filters": {
        "filters": {
          "box1": {
            "geo_bounding_box": {
              "location": {
                "top_left": {
                  "lat": 90,
                  "lon": -180
                },
                "bottom_right": {
                  "lat": 0,
                  "lon": 0
                }
              }
            }
          },
          "box2": {
            "geo_bounding_box": {
              "location": {
                "top_left": {
                  "lat": 90,
                  "lon": 0
                },
                "bottom_right": {
                  "lat": 0,
                  "lon": 180
                }
              }
            }
          },
          "box3": {
            "geo_bounding_box": {
              "location": {
                "top_left": {
                  "lat": 0,
                  "lon": -180
                },
                "bottom_right": {
                  "lat": -90,
                  "lon": 0
                }
              }
            }
          },
          "box4": {
            "geo_bounding_box": {
              "location": {
                "top_left": {
                  "lat": 0,
                  "lon": 0
                },
                "bottom_right": {
                  "lat": -90,
                  "lon": 180
                }
              }
            }
          }
        }
      },
      "aggs": {
        "over_time": {
          "date_histogram": {
            "field": "date",
            "interval": "day",
            "format": "yyyy-MM-dd"
          },
          "aggs": {
            "AvgTemp": {
              "avg": {
                "field": "hits"
              }
            }
          }
        }
      }
    }
  }
}