Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch aggregation group by null key

here is the data in my elasticsearch server:

{"system": "aaa"},
{"system": "bbb"},
{"system": null}

I want to get the statistics for system. then I did the query:

{
    "aggs" : {
        "myAggrs" : {
            "terms" : { "field" : "system" }
        } 
}

it gives me the result:

    {
       "key": "aaa",
       "doc_count": 1
    },
    {
       "key": "bbb",
       "doc_count": 1
    }

but the "key" : null is not included in the result, how can I get it? here is my expect result:

{
   "key": "aaa",
   "doc_count": 1
},
{
   "key": "bbb",
   "doc_count": 1
},
{
   "key": null,
   "doc_count": 1
}
like image 334
fudy Avatar asked Jun 17 '15 10:06

fudy


1 Answers

I don't think you can do this with terms. Try with another aggregation:

{
  "aggs": {
    "myAggrs": {
      "terms": {
        "field": "system"
      }
    },
    "missing_system": {
      "missing": {
        "field": "system"
      }
    }
  }
}

And the result will be:

   "aggregations": {
      "myAggrs": {
         "doc_count_error_upper_bound": 0,
         "sum_other_doc_count": 0,
         "buckets": [
            {
               "key": "aaa",
               "doc_count": 1
            },
            {
               "key": "bbb",
               "doc_count": 1
            }
         ]
      },
      "missing_system": {
         "doc_count": 1
      }
   }
like image 193
Andrei Stefan Avatar answered Nov 09 '22 12:11

Andrei Stefan