elasticsearch - 在多个聚合中获取唯一的桶数

时间:2017-08-21 12:01:31

标签: sql elasticsearch group-by

我正在尝试获取我的聚合总数大于某个数量1000的桶的总数。以下是文档样本。

[
  {
    "_index": "orders_stage",
    "_type": "order",
    "_id": "AV3FtHR8lArSPNJl_rcp",
    "_score": 1,
    "_source": {
      "total_amount": 650,
      "custid": "2",
      "client_id": 1
    }
  },
  {
    "_index": "orders_stage",
    "_type": "order",
    "_id": "AV3F5UfjlArSPNJl_rlu",
    "_score": 1,
    "_source": {
      "total_amount": 200,
      "custid": "1",
      "client_id": 1
    }
  },
  {
    "_index": "orders_stage",
    "_type": "order",
    "_id": "AV3F5UfjlArSPNJl_rxm",
    "_score": 1,
    "_source": {
      "total_amount": 1400,
      "custid": "1",
      "client_id": 1
    }
  }
]

首先,我能够在桶中获取大于1000的唯一记录,但我得到的桶数为2而不是1.我使用以下查询:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "client_id": 1
          }
        }
      ]
    }
  },
  "aggs": {
    "customers": {
      "terms": {
        "field": "custid",
        "size": 1
      },
      "aggs": {
        "amount_spent": {
          "sum": {
            "field": "total_amount"
          }
        },
        "amount_spent_filter": {
          "bucket_selector": {
            "buckets_path": {
              "amountSpent": "amount_spent"
            },
            "script": "params.amountSpent >= 1000"
          }
        }
      }
    },
    "uniques": {
      "cardinality": {
        "field": "custid"
      }
    }
  }
}

我只想获得我能够使用上述查询获取的桶数。

由于

0 个答案:

没有答案
相关问题