gpt4 book ai didi

elasticsearch - 如何跨嵌套文档汇总文档?

转载 作者:行者123 更新时间:2023-12-03 00:31:41 26 4
gpt4 key购买 nike

我有如下文件:

文件1

{
Name: "Test",
Items: [
{ Type: 1, Value: 1 },
{ Type: 2, Value: 2 },
...
]
}

文件2
{
Name: "Test",
Items: [
{ Type: 1, Value: 10 },
{ Type: 2, Value: 20 },
...
]
}

现在,我需要将多个文档的 NameType字段分组,然后取平均值。但是,我正在努力寻找有关如何通过 flex 搜索实现此目的的信息?

例如,在MongoDB中,我将 $unwind Items然后 $group``NameItems.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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com