Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch aggregation on distinct keys

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?

like image 804
felicienb Avatar asked Oct 18 '22 02:10

felicienb


1 Answers

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
            }
         ]
      }
   }
like image 146
keety Avatar answered Oct 21 '22 00:10

keety