在 Elasticsearch 中使用 GeoTile 聚合在分桶之前过滤文档

Filter documents prior to bucketing using GeoTile Aggregation in Elasticsearch

我正在寻找一个示例,其中在通过 GeoTile 聚合进行分桶之前过滤文档。例如,我希望存储桶中的某些值大于 x 的文档数。任何指针将不胜感激。现在我有:

{
    "aggs": {
        "avg_my_field": {
            "avg": {
                "field": "properties.my_field"
            }
        },
        "aggs": {
            "large-grid": {
                "geotile_grid": {
                    "field": "coordinates",
                    "precision": 8
                }
            }
        }
    }
}

我不知道从这里到哪里去。任何指针将不胜感激。

只需添加一个顶级filter aggregation

在伪代码中:

POST /your-index/_search
{
  aggs: 
    filter_agg_name:
       filter:
          ...actual filters
       aggs:
          ...the rest of your aggs 
}

适用于您的特定用例:

POST _search
{
  "aggs": {
    "my_applicable_filters": {
      "filter": {
        "bool": {
          "must": [
            {
              "range": {
                "some_numeric_or_date_field": {
                  "gte": 42
                }
              }
            }
          ]
        }
      },
      "aggs": {
        "avg_my_field": {
          "avg": {
            "field": "properties.my_field"
          }
        },
        "large-grid": {
          "geotile_grid": {
            "field": "coordinates",
            "precision": 8
          }
        }
      }
    }
  }
}

请注意,您的原始聚合查询在语法上不正确。你很接近,但请记住:

1. 某些 聚合可以具有以下形式的直接子级(子聚合):

POST /your-index/_search
{
  aggs: 
    top_level_agg_name:
       agg_type:
          ...agg_def
       aggs:
          1st_child_name:
             ...1st_child_defs
          2nd_child_name:
             ...2nd_child_defs
          ...
}

我说一些因为avg聚合支持子聚合(因为it's not a bucket aggregation) .这就是我改为应用以下内容的原因:

2。在单个请求中指定时,聚合可以 运行 彼此无关:

POST /your-index/_search
{
  aggs: 
    some_agg_name:
       agg_type:
          ...agg_def
    other_agg_name:   
       agg_type:
          ...agg_def
    ...
}

这样,您可以获得 properties.my_field 的平均值并同时对您的 coordinates 进行地理聚类。


相反,当您意识到 geotile_grid 确实是一个能够接受子聚合的 bucket 聚合时,您可以 首先 将您的文档按相应的 geo hash 和 then 计算平均值。现在想想,这可能是你的初衷。

说到清晰的时刻,您可以学到很多 aggregations relate to each other in my recently released Elasticsearch Handbook