简体   繁体   中英

Composite Elasticsearch Aggregations using Elasticsearch DSL Python

I have the following query which is working fine here:

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

I am trying to convert it into Elasticsearch DSL Python, but I am getting parallel aggregations not composite:

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()

What I am looking for as an output is something like this:

"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
              },
...

Try like this:

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

More info can be found here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM