sql - elasticsearch - sum of total amount is greater than some amount using aggregation -


i trying records aggregation total greater amount 1000. below documents sample.

 [   {     "_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     }   } ] 

so first of all, grouping(agg) records using custid , want records sum of total_amount greater amount 1000. have tried following query:

{   "query": {     "bool": {       "must": [         {           "term": {             "client_id": 1           }         },         {           "range": {             "amount_spent": {               "gte": 1000             }           }         }       ]     }   },   "aggs": {     "customers": {       "terms": {         "field": "custid"       },       "aggs": {         "amount_spent": {           "sum": {             "field": "total_amount"           }         }       }     }   } } 

when run query not getting anything, can please guide me filter on aggregation results.

thanks

you need use bucket_selector pipeline aggregation, cannot in query part:

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

Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -