弹性搜索聚合在不同的键 [英] Elasticsearch aggregation on distinct keys

查看:132
本文介绍了弹性搜索聚合在不同的键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过字段类别中的不同键汇总我的文档。
这里有两个文件:

I want to aggregate my documents over the different keys in the field "categories". Here are two documents:

      "date": 1470271301,
      "categories": {
        "1": [blabla],
        "2": [blala]
      }


      "date": 144343545,
      "categories": {
        "1": [blabla],
        "2": [coco]
        "3": [rat, saouth]
      }

类别映射:

"categories" : {
    "properties" : {
        "1" : {
            "type" : "long"

我想得到这样的东西:

 "buckets" : [ {
    "key" : "1",
    "doc_count" : 2
  }, {
    "key" : "2",
    "doc_count" : 2
    {
    "key" : "3",
    "doc_count" : 1
  }

有没有一个很好的方法来做这个改变我的文档的映射?

Is there a good way to do this whithout changing the mapping of my documents?

推荐答案

p>可以使用meta-f为此目的, _field_names

One could use the meta-field _field_names for this purpose.

如下面的示例所示,运行聚合将为您提供文档计数。

Running aggregate on this as shown in the example below would give you the document count.

示例:

put test/test/1 
{
    "date": 1470271301,
      "categories": {
        "1": ["blabla"],
        "2": ["blala"]
      }
}
put test/test/2 
{
   "date": 144343545,
      "categories": {
        "1": ["blabla"],
        "2": ["coco"],
        "3": ["rat", "saouth"]
      }
}

POST test/_search
{
   "size": 0,
   "aggs": {
      "field_documents": {
         "terms": {
            "field": "_field_names",
            "include" : "categories.*",
            "size": 0
         }
      }
   }
}

结果:

  "aggregations": {
      "field_documents": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "categories",
               "doc_count": 2
            },
            {
               "key": "categories.1",
               "doc_count": 2
            },
            {
               "key": "categories.2",
               "doc_count": 2
            },
            {
               "key": "categories.3",
               "doc_count": 1
            }
         ]
      }
   }

这篇关于弹性搜索聚合在不同的键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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