首页 > 解决方案 > Elasticsearch 过滤聚合以返回一个指标

问题描述

我正在尝试计算在 Elasticsearch 上安装我的应用程序的用户百分比。

为了实现这一点,我做如下:

  1. 在索引上查询所有感兴趣的用户
  2. filters bucket aggregation过滤安装了android和ios应用程序的人
  3. sum_bucket聚合以返回安装应用程序的用户总数。
  4. Javascript在响应中使用total hits和计算百分比。total values

我的问题是是否有一种方法可以在聚合步骤上创建一个过滤器,它允许我只制作一个具有两个条件的过滤器并返回一个指标而不是一个存储桶,所以我可以跳过第 3 步

这是我的查询:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "fieldId": "uuid"
          }
        }
      ]
    }
  },
  "aggs": {
    "byInstalled": {
      "filters": {
        "filters": [
         {
            "range": {
              "firstIOSLoginAt": {
                "gte": "2020-06-15T06:56:10.436Z",
                "lte": "2022-06-15T06:56:10.436Z"
              }
            }
          },
           {
            "range": {
              "firstAndroidLoginAt": {
                "gte": "2020-06-15T06:56:10.436Z",
                "lte": "2022-06-15T06:56:10.436Z"
              }
            }
          }
        ]
      }  
    },
    "totalInstalled": {
      "sum_bucket": {
        "buckets_path": "byInstalled>_count"
      }
    }
  }
}

这是回应:

{
  "took" : 3,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 546,
      "relation" : "gte"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "byInstalled" : {
      "meta" : { },
      "buckets" : [
        {
          "doc_count" : 2
        },
        {
          "doc_count" : 1
        }
      ]
    },
    "totalInstalled" : {
      "value" : 3.0
    }
  }
}

提前致谢。

标签: elasticsearchelasticsearch-aggregation

解决方案


推荐阅读