How to calculate the sum of one filed in the aggs of another field?


(Red) #1

"aggs": {
    "my_agg": {         
        "terms": {
            "field": "product_id", 
            "size": 3
        }
    },
    "aggs": {
        "my_agg_sum": {
            "sum": {
                "field": "sku_sell_num", 
                "missing": 0
            }
        }
    } 
}, 
"size": 10

}

this is my request body, and it is always incorrect.
can anyone give me a hint?


(Red) #2

ive solved my problem just now.
here is my request body

 "filter": {
 "bool": {
    "must": [
        {
            "range": {
                "start_time": {
                    "format": "YYYY-MM-dd", 
                    "lte": "2016-01-26"
                }
            }
        }, 
        {
            "range": {
                "end_time": {
                    "format": "YYYY-MM-dd", 
                    "gte": "2016-01-26"
                }
            }
        }, 
        {
            "range": {
                "amount": {
                    "gte": 0
                }
            }
        }
    ]
}
}, 
        
        
"size":0,
"aggregations": {
"spu_group": {
    "terms": {
      "field": "product_id",
      "size": 4,
      "order" : {"sum_sku_sell_num": "desc"}
    },
    "aggregations": {
        "sum_sku_sell_num":{
            "sum": {
                "field": "sku_sell_num"
            }
        },
        "max_sku_sell_num_sku":{
           "top_hits": {
                "sort":[
                    {
                        "sku_sell_num": {
                            "order": "desc",
                            "missing": 0
                        }
                    }
                ],
                "_source": {
                    "include": [
                        "sku_id"
                    ]
                },
                "from": 0,
                "size": 1
            }
        }
    }
 }
 }

(system) #3