elasticsearch 子汇总返回的计数时段

aemubtdh  于 2022-12-03  发布在  ElasticSearch
关注(0)|答案(1)|浏览(124)

我需要从pipe aggregation返回的结果集中计算bucket的数量。问题是我在这里使用script selector的查询:

POST visitor_carts/_search
{
  "size": 0,
  "aggs": {
    "visitors": {
      "terms": {"field" : "visitor_id"},
      "aggs": {
        "one_purchase": {
          "bucket_selector": {
            "buckets_path": {
              "nb_purchases": "_count"
            },
            "script": "params.nb_purchases == 3"
          }
        }
      }
    }
  }
}

会传回类似下列的内容:

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 5,
    "max_score" : 0.0,
    "hits" : [ ]
  },
  "aggregations" : {
    "visitors" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 0,
      "buckets" : [
        {
          "key" : "2",
          "doc_count" : 3
        },
        {
          "key" : "3",
          "doc_count" : 3
        }
      ]
    }
  }
}

buckets项下,我可以看到符合条件的访问者列表(由visitor_id标识的每个访问者必须在visitor_carts索引中正好有三个文档)但这并不是很有帮助,因为它应该处理成千上万的访问者。理论上它可以计算结果集,但是对于大量的访问者来说,这似乎不是最好的主意。有没有办法只输出doc_count_error_upper_boundsum_other_doc_count旁边的有效桶的计数?有点奇怪的是,聚合统计中没有包含bucket_count,因为它似乎非常有用。
或者可以用不同的方式来解决?这个问题是这个问题的后续问题:获取进行了特定次数购买的用户计数
下面是我的visitor_cartsMap:

{
  "mapping": {
    "_doc": {
      "dynamic": "false",
      "properties": {
        "created_dt": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss"
        },
        "order_id": {
          "type": "keyword"
        },
        "visitor_id": {
          "type": "keyword"
        }
      }
    }
  }
}
jckbn6z7

jckbn6z71#

您可以使用Stats Bucket Aggregation来获取bucket的计数。
以下是您的查询方式。

聚合查询:

POST visitor_carts/_search
{
  "size": 0,
  "aggs": {
    "visitors": {
      "terms": {
        "field" : "visitor_id"
      },
      "aggs": {
        "one_purchase": {
          "bucket_selector": {
            "buckets_path": {
              "nb_purchases": "_count"
            },
            "script": "params.nb_purchases == 3"
          }
        }
      }
    },
    "mybucketcount":{
      "stats_bucket": {
        "buckets_path":"visitors._count"
      }
    }
  }
}

聚合结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 8,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "visitors": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "2",
          "doc_count": 3
        },
        {
          "key": "3",
          "doc_count": 3
        }
      ]
    },
    "mybucketcount": {
      "count": 2,              <---- This is the count you are looking for
      "min": 3,
      "max": 3,
      "avg": 3,
      "sum": 6
    }
  }
}

如果有帮助,请告诉我!

相关问题