假设我在同一索引中有很多文档,如下所示:
"_source": {
"customerId": "100",
"firstName": "XAaa",
"lastName": "XBbb",
"price": 10,
"revenue": 5
}
"_source": {
"customerId": "100",
"firstName": "XAaa",
"lastName": "XBbb",
"price": 20,
"revenue": 15
}
"_source": {
"customerId": "200",
"firstName": "YAaa",
"lastName": "yBbb",
"price": 110,
"revenue": 20
}....
如何使Elasticsearch聚合对来自同一customerId的值进行计数?
结果应该是这样的:
"_source": {
"customerId": "100",
"firstName": "XAaa",
"lastName": "XBbb",
"price": 30, // (10+20)
"revenue": 20 //(5+15)
}
"_source": {
"customerId": "200",
"firstName": "YAaa",
"lastName": "yBbb",
"price": 110,
"revenue": 20
}....
谢谢
{
"size": 0,
"aggs": {
"per_customer": {
"terms": {
"field": "customerId",
"size": 10
},
"aggs": {
"total_price": {
"sum" : { "field" : "price" }
},
"total_revenue": {
"sum" : { "field" : "revenue" }
}
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句