如何使用多个嵌套对象在Elasticsearch中创建组合聚合?
我想要实现的是按国家/地区分组的数量的总和,该映射看起来像这样
curl -XPUT 'localhost:9200' -d '{
"index": "test",
"type": "orders",
"body": {
"orders": {
"order_number": {
"type": "string",
"index": "not_analyzed"
},
"buyer": {
"properties": {
"country": {
"type": "string",
"index": "not_analyzed"
}
}
},
"products": {
"type": "nested",
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"quantity": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}'
使用此测试数据
curl -XPOST 'localhost:9200/_bulk' -d '
{"index": {"_index": "test", "_type": "orders", "_id": 1}}
{"order_number": "1", "buyer": {"country": "US"}, "products": [{"name": "A product", "quantity": 10}]}
{"index": {"_index": "test", "_type": "orders", "_id": 2}}
{"order_number": "2", "buyer": {"country": "DE"}, "products": [{"name": "A product", "quantity": 10}]}
{"index": {"_index": "test", "_type": "orders", "_id": 3}}
{"order_number": "3", "buyer": {"country": "US"}, "products": [{"name": "A product", "quantity": 10}]}
'
结果应该是已售出10" A产品"在DE和20在美国。当有问题的属性位于单独的嵌套对象上时,如何创建此聚合?
答案 0 :(得分:0)
您可以在其他聚合中进行聚合。
这样的事情应该有效 - 您可能需要指定嵌套路径:
{
"aggs": {
"buyer_country": {
"terms": {
"field": "buyer.country"
}
},
"aggs": {
"quantity_of_product" : { "sum" : { "field" : "products.quantity" } }
}
}
}
和:https://www.elastic.co/blog/intro-to-aggregations-pt-2-sub-aggregations