无法在elasticsearch中的嵌套字段上聚合 [英] Not able to aggregate on nested fields in elasticsearch

查看:68
本文介绍了无法在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" }
}
}

如何在Elasticsearch中进行聚合?

How can I do the aggregation in elasticsearch?

推荐答案

使用作为一个简单的示例,我创建了一个带有嵌套属性的索引,该属性与您发布的内容相匹配:

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"
                  }
               }
            }
         }
      }
   }
}

然后添加了您的文档:

PUT /test_index/doc/1
{
   "attributes": [
      { "name": "snake", "type": "reptile" },
      { "name": "cow", "type": "mammal" }
   ]
}

现在,我可以按如下方式获得"attribute.name" 术语:

Now I can get "attribute.name" terms as follows:

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

这篇关于无法在elasticsearch中的嵌套字段上聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆