如何从嵌套文档中跨文档聚合?

时间:2015-05-01 08:32:40

标签: elasticsearch group-by aggregate children

我的文件看起来像这样:

文件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字段进行分组,然后平均值。但是我很难找到有关如何通过弹性搜索来实现这一目标的信息?

例如,在MongoDB中,我会$unwind Items然后$group``NameItems.Type然后$avg Value字段。

我期待的结果是:

结果1

{
    Name: "Test,
    Type: 1,
    Average: 5.5
}

结果2

{
    Name: "Test,
    Type: 2,
    Average: 10.5
}

1 个答案:

答案 0 :(得分:0)

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

相关问题