# Terms aggregation with “missing”

**URL:** <https://discuss.elastic.co/t/terms-aggregation-with-missing/110440>\
**Category:** Elasticsearch\
**Created:** [December 6, 2017, 12:46am UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440 "2017-12-06T00:46:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimyoo](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jimyoo](https://discuss.elastic.co/u/jimyoo)\
**Post date:** [December 6, 2017, 12:46am UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440/1 "2017-12-06T00:46:59Z")

</div>

I am observing these behaviors.

For a query like under works.

```
"size" :0,
  "aggregations" : {
    “keywordTypeField” : {
      "terms" : {
        "field" : “keywordTypeField”,
        "missing": “N/A”,
        "min_doc_count" : 1
      }
   }

```

But if I use non-keyword type fields, such as long or ip, it gives exceptions.

```
 "size" :0,
   "aggregations" : {
     “longTypeField” : {
       "terms" : {
         "field" : “longTypeField”,
         "missing": “N/A”,
         "min_doc_count" : 1
       }
   }

```

Exceptions like:  
(for ip type) unknown\_host\_exception, addr is of illegal length  
(for long type) number\_format\_exception, empty String

However, once I remove the “missing” line, the query works.  
Is this expected, or something in my env?  
I am using Elasticsearch 5.6.4.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [December 8, 2017, 9:59am UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440/2 "2017-12-08T09:59:20Z")

</div>

The `missing` is the value that Elasticsearch will assign to documents that do not have a value for the field that you are aggregating upon (`keywordTypeField` and `longTypeField` in your examples).

It works for `keywordTypeField` because the string `"N/A"` is a valid value for a field of type `keyword`. However, `"N/A"` is not a valid `long` nor a valid `ip`). Maybe you can change missing to a value like `-1` for long type aggregations, and `192.0.2.0` for ip type aggregations.

---

<div class="post-metadata">

**Author:** ![jimyoo](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jimyoo](https://discuss.elastic.co/u/jimyoo)\
**Post date:** [December 8, 2017, 9:58pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440/3 "2017-12-08T21:58:21Z")

</div>

Ahh ... Thanks, great!

FYI, for ip, still get the exception - using tools like "Elasticsearch head" or curl.  
However, with Transport API it works. When print out it's json, it's like (nothing special),

> ```
> "missing" : "0.0.0.0",
> 
> ```

But if I use tools with copy & paste the line, get the exception.  
If you can shed light on, that will be greater ^^. Regardless, thanks again.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [December 9, 2017, 11:18am UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440/4 "2017-12-09T11:18:37Z")

</div>

It looks like you have to provide the IP as an array of the 4 digits of the IP. The following seems to work:

`"missing": "\u0000\u0000\u0000\u0000"`

---

<div class="post-metadata">

**Author:** ![jimyoo](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jimyoo](https://discuss.elastic.co/u/jimyoo)\
**Post date:** [December 11, 2017, 6:45pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440/5 "2017-12-11T18:45:47Z")

</div>

Nice ... 😀  
All working now. Transport API case also using that format seems more reliable.  
Thanks Abdon,

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [December 13, 2017, 9:45am UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440/6 "2017-12-13T09:45:57Z")

</div>

I have raised a GitHub issue about the fact that `missing` does not accept IPs: [https://github.com/elastic/elasticsearch/issues/27788](https://github.com/elastic/elasticsearch/issues/27788)

---

<div class="post-metadata">

**Author:** ![jimyoo](https://avatars.discourse-cdn.com/v4/letter/j/53a042/32.png) [@jimyoo](https://discuss.elastic.co/u/jimyoo)\
**Post date:** [December 13, 2017, 3:11pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440/7 "2017-12-13T15:11:00Z")

</div>

Thanks. Good to see it will be better and also good Elasticsearch inside insight as well. ^^

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [January 10, 2018, 3:11pm UTC](https://discuss.elastic.co/t/terms-aggregation-with-missing/110440/8 "2018-01-10T15:11:10Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
