4

i am using aggregations and aggregation bucket accept one key value as default then i research and find it

"aggs" : {
        "my_buckets": {
            "composite" : {
                "sources" : [
                    { "category_pk": { "terms": { "field": "category.pk"} } },
                    { "category_name": { "terms": {"field": "category.name" } } }
                ]
            }
        }
    }
}

Above code result two keys , and _doc_count but i can't apply for elasticsearch-dsl Someone help me

  • Thanks

1 Answers1

4

I solved the problem,When We are using Composite

s = ProductDocument.search()
brand_name = A('terms', field='brand.name')
brand_pk = A('terms', field='brand.id')
brand_key_aggs = [
    {'brand_pk': brand_pk},
    {'brand_name': brand_name}
]
s.aggs.bucket('brand_terms', "composite", sources=brand_key_aggs) 

Example Result

 {  
               'key':{  
                  'brand_pk':869,
                  'brand_name':'Uni Baby'
               },
               'doc_count':2
            },
Sviat Lavrinchuk
  • 324
  • 4
  • 10
  • Ok, I see now, you wanted the Python equivalent of the DSL query. Glad you figured it out! – Val Sep 03 '19 at 12:56
  • For those who need the updated answer, the last line of code aggs (at least in my ES-dsl version) changes to: s.aggs.bucket('brand_terms', "composite", sources=brand_key_aggs) – Reyhaneh Torab Jan 11 '21 at 14:10