Elasticsearch aggregation on distinct keys

眉间皱痕 提交于 2019-12-12 10:54:38

问题


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

Mapping for categories:

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

And i want get something like this:

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


回答1:


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.

Example :

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

Result :

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


来源:https://stackoverflow.com/questions/39228585/elasticsearch-aggregation-on-distinct-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!