Elasticsearch:嵌套类型字段内的字段聚合

时间:2017-12-07 12:31:42

标签: elasticsearch elasticsearch-aggregation elasticsearch-mapping

我想汇总位于keyword类型字段内的nested类型字段。嵌套字段的映射如下:

"Nested_field" : {
    "type" : "nested",
    "properties" : {
        "Keyword_field" : {
            "type" : "keyword"
        }
    }
}

我用来汇总的查询部分如下:

"aggregations": {
    "Nested_field": {
        "aggregations": {
            "Keyword_field": {
                "terms": {
                    "field": "Nested_field.Keyword_field"
                }
            }
        },
        "filter": {
            "bool": {}
        }
    },
}

但这并没有返回正确的聚合。即使现有文档有Keyword_field个值,查询也会返回0个桶。所以,我的聚合查询有问题。任何人都可以帮我找到错误吗?

1 个答案:

答案 0 :(得分:3)

我认为你需要在那里提供一个嵌套路径。这在ES 5中有效,但看起来你基于“聚合”和“aggs”使用6,所以让我知道它是否不起作用我将废弃这个答案。试一试:

{
    "aggregations": {
        "nested_level": {
            "nested": {
                "path": "Nested_field"
            },
            "aggregations": {
                "keyword_field": {
                    "terms": {
                        "field": "Nested_field.Keyword_field"
                    }
                }
            }
        }
    }
}
相关问题