【问题标题】:Elasticsearch aggregate by geo locationElasticsearch 按地理位置聚合
【发布时间】:2015-11-13 13:13:18
【问题描述】:

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

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_timepart 可以很好地隔离,但这个查询给了我"reason": "Could not find aggregator type [geo_bounding_box] in [group_by_geo]"

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

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

【问题讨论】:

    标签: search elasticsearch search-engine


    【解决方案1】:

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

    您可以使用filters 聚合来实现您所需要的,其中每个边界框都包含一个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"
                  }
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢。为什么过滤器加倍?
    • 就是这样it is defined。另请参阅 my other answer 关于这个主题。
    • 啊,我明白了,谢谢四,你的帮助起初看起来有点难看和困难;)
    • 确实,第二个filters 本来可以命名为别的。
    猜你喜欢
    • 1970-01-01
    • 2016-06-16
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    相关资源
    最近更新 更多