如何使用多个嵌套对象在elasticsearch中创建一个组合聚合?
我想实现的是国家/p>的映射中按国家分组的数量之和
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}]}
'
结果应该是在DE中出售了10个"产品",而在美国有20个。当所讨论的属性在单独的嵌套对象上时,如何创建此聚合?
您可以在其他聚合中具有聚合。
类似的东西应该起作用 - 您可能需要指定嵌套路径:
{
"aggs": {
"buyer_country": {
"terms": {
"field": "buyer.country"
}
},
"aggs": {
"quantity_of_product" : { "sum" : { "field" : "products.quantity" } }
}
}
}
请参阅:https://www.elastic.co/guide/en/elasticsearch/reference/current/current/search-aggregations-bucket-nested-aggregation.html
and:https://www.elastic.co/blog/introto-to-yaggregations-pt-2-sub-aggregations