使用Elasticsearch DSL Python的复合Elasticsearch聚合

时间:2019-05-21 05:54:02

标签: python elasticsearch elasticsearch-dsl elasticsearch-dsl-py

我有以下查询在这里工作正常:

{
   "aggs":{
      "category_terms":{
         "terms":{
            "field":"Category"
         },
         "aggs":{
            "style_per_category":{
               "terms":{
                  "field":"Style"
               }
            }
         }
      }
   }
}

我正在尝试将其转换为Elasticsearch DSL Python,但是我得到的并行聚合不是复合的:

a_cat = A('terms', field='Category')
a_style = A('terms', field='Style')

s.aggs.bucket('category_terms', a_cat)
s.aggs.bucket('style_per_category', a_style)

response = s.execute()

我正在寻找的输出是这样的:

"aggregations": {
    "category_terms": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 2727399,
      "buckets": [
        {
          "key": "Tops",
          "doc_count": 3131952,
          "style_per_category": {
            "doc_count_error_upper_bound": 14,
            "sum_other_doc_count": 129758,
            "buckets": [
              {
                "key": "T-Shirts",
                "doc_count": 940725
              },
...

1 个答案:

答案 0 :(得分:1)

尝试这样:

s.aggs.bucket('category_terms', a_cat)
   .bucket('style_per_category', a_style)

可以找到更多信息here