7

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
}

1 Answer 1

11

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
      }
   }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.