我有如下文件:
文件1
{
Name: "Test",
Items: [
{ Type: 1, Value: 1 },
{ Type: 2, Value: 2 },
...
]
}
文件2
{
Name: "Test",
Items: [
{ Type: 1, Value: 10 },
{ Type: 2, Value: 20 },
...
]
}
现在,我需要将多个文档的
Name
和Type
字段分组,然后取平均值。但是,我正在努力寻找有关如何通过 flex 搜索实现此目的的信息?例如,在MongoDB中,我将
$unwind
Items
然后$group``Name
和Items.Type
然后$avg
Value
字段。我期望的结果是:
结果1
{
Name: "Test,
Type: 1,
Average: 5.5
}
结果2
{
Name: "Test,
Type: 2,
Average: 10.5
}
最佳答案
For nested documents you can run aggregation like the below one...
"aggs" : {
"resellers" : {
"nested" : {
"path" : "resellers"
},
"aggs" : {
"min_price" : { "min" : { "field" : "resellers.price" } }
}
}
}
用于像这样的数据映射
"product" : {
"properties" : {
"resellers" : {
"type" : "nested",
"properties" : {
"name" : { "type" : "string" },
"price" : { "type" : "double" }
}
}
}
}
资料来源:http://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-nested-aggregation.html
关于elasticsearch - 如何跨嵌套文档汇总文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29983824/