Elasticsearch:如何设置 FILTER-Aggregation 的 'doc_count' 相对于总数 'doc_count'

Elasticsearch: How set 'doc_count' of a FILTER-Aggregation in relation to total 'doc_count'

A seemingly very trivial problem prompted me today to read the Elasticsearch documentation again diligently. So far, however, I have not come across the solution....

问题:
是否有一种简单的方法来设置过滤器聚合的 doc_count 相对于总 doc_count?

这是我的搜索请求中的一个片段-json。
feature_occurrences 聚合中,我过滤了文档。
现在我想计算每个时间桶中 filtered/all 文档的比率。

GET my_index/_search
{
  "aggs": {
    "time_buckets": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "1d",
        "min_doc_count": 0
      },
      "aggs": {
        "feature_occurrences": {
          "filter": {
            "term": {
              "x": "y"
            }
          }
        },
        "feature_occurrences_per_doc" : {
             
            // feature_occurences.doc_count / doc_count 
         
       }

       

有什么想法吗?

您可以使用bucket_script来计算比率:

{
  "aggs": {
    "date": {
      "date_histogram": {
        "field": "@timestamp",
        "interval": "hour"
      },
      "aggs": {
        "feature_occurrences": {
          "filter": {
            "term": {
              "cloud.region": "westeurope"
            }
          }
        },
        "ratio": {
          "bucket_script": {
            "buckets_path": {
              "doc_count": "_count",
              "features_count": "feature_occurrences._count"
            },
            "script": "params.features_count / params.doc_count"
          }
        }
      }
    }
  }
}

弹性桶脚本文档:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-pipeline-bucket-script-aggregation.html