[英]Not able to aggregate on nested fields in elasticsearch
I have set a field to nested and now i am not able to aggregate on it. 我已将字段设置为嵌套,但现在无法对其进行汇总。 Sample document -
样本文件-
{
"attributes" : [
{ "name" : "snake" , "type" : "reptile" },
{ "name" : "cow" , "type" : "mamal" }
]
}
attributes field is nested. 属性字段是嵌套的。 Following terms query is not working on this
以下字词查询不适用于此
{
"aggs" : {
"terms" : { "field" : "attributes.name" }
}
}
How can I do the aggregation in elasticsearch? 如何在Elasticsearch中进行汇总?
Use a nested aggregation . 使用嵌套聚合 。
As a simple example, I created an index with a nested property matching what you posted: 作为一个简单的示例,我创建了一个具有嵌套属性的索引,该属性与您发布的内容相匹配:
PUT /test_index
{
"mappings": {
"doc": {
"properties": {
"attributes": {
"type": "nested",
"properties": {
"name": {
"type": "string"
},
"type": {
"type": "string"
}
}
}
}
}
}
}
Then added your document: 然后添加您的文档:
PUT /test_index/doc/1
{
"attributes": [
{ "name": "snake", "type": "reptile" },
{ "name": "cow", "type": "mammal" }
]
}
Now I can get "attribute.name"
terms as follows: 现在,我可以获取
"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
}
]
}
}
}
}
Here's the code I used: 这是我使用的代码:
http://sense.qbox.io/gist/0e3ed9c700f240e523be08a27551707d4448a9df http://sense.qbox.io/gist/0e3ed9c700f240e523be08a27551707d4448a9df
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.