我已将字段设置为嵌套,现在我无法对其进行聚合。 样本文件 -
{
"attributes" : [
{ "name" : "snake" , "type" : "reptile" },
{ "name" : "cow" , "type" : "mamal" }
]
}
属性字段是嵌套的。 以下条款查询不适用于此
{
"aggs" : {
"terms" : { "field" : "attributes.name" }
}
}
如何在elasticsearch中进行聚合?
答案 0 :(得分:3)
作为一个简单的例子,我创建了一个索引,其嵌套属性与您发布的内容相匹配:
PUT /test_index
{
"mappings": {
"doc": {
"properties": {
"attributes": {
"type": "nested",
"properties": {
"name": {
"type": "string"
},
"type": {
"type": "string"
}
}
}
}
}
}
}
然后添加了你的文件:
PUT /test_index/doc/1
{
"attributes": [
{ "name": "snake", "type": "reptile" },
{ "name": "cow", "type": "mammal" }
]
}
现在我可以获得"attribute.name"
条款如下:
POST /test_index/_search?search_type=count
{
"aggs": {
"nested_attributes": {
"nested": {
"path": "attributes"
},
"aggs": {
"name_terms": {
"terms": {
"field": "attributes.name"
}
}
}
}
}
}
...
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": []
},
"aggregations": {
"nested_attributes": {
"doc_count": 2,
"name_terms": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "cow",
"doc_count": 1
},
{
"key": "snake",
"doc_count": 1
}
]
}
}
}
}
这是我使用的代码:
http://sense.qbox.io/gist/0e3ed9c700f240e523be08a27551707d4448a9df