# Terms aggregation Java API with missing

**URL:** <https://discuss.elastic.co/t/terms-aggregation-java-api-with-missing/131293>\
**Category:** Elasticsearch\
**Created:** [May 10, 2018, 11:23am UTC](https://discuss.elastic.co/t/terms-aggregation-java-api-with-missing/131293 "2018-05-10T11:23:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amugofjava](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amugofjava/32/75350_2.png) [@amugofjava](https://discuss.elastic.co/u/amugofjava)\
**Post date:** [May 10, 2018, 11:23am UTC](https://discuss.elastic.co/t/terms-aggregation-java-api-with-missing/131293/1 "2018-05-10T11:23:16Z")

</div>

Hi,

I am trying to build a Terms bucket aggregation that also includes documents with the term missing. What I'm finding is, without adding a `.missing()`, as you would expect, any documents missing the term are not included in the results. However, if add the `.missing("Unknown")` clause to the builder I then the only bucket I get back is for documents with that term missing. How do I combine the two to get both?

Interestingly, if I set a breakpoint in the code and cut and paste the generated JSON into Postman and fire it off - I get the data back as I would expect: with all terms and the missing one included as 'Unknown'.

Any pointers would be much appreciated. Here is the code I am trying:

```
    AggregationBuilder aggregation =
            AggregationBuilders
                    .terms("by_typename")
                    .missing("Unknown") <-- With this added only missing are returned
                    .field("typename")
                    .size(500)
                    .order(BucketOrder.key(true))
                    .minDocCount(0)
                    .subAggregation(
                            AggregationBuilders.dateHistogram("by_time")
                                    .field("@timestamp")
                                    .dateHistogramInterval(DateHistogramInterval.MONTH)
                                    .extendedBounds(new ExtendedBounds("now-" + time + "M", "now"))
                                    .format("epoch_millis")
                                    .minDocCount(0)
                    );

```

Thanks.

---

<div class="post-metadata">

**Author:** ![amugofjava](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/amugofjava/32/75350_2.png) [@amugofjava](https://discuss.elastic.co/u/amugofjava)\
**Post date:** [May 11, 2018, 6:48am UTC](https://discuss.elastic.co/t/terms-aggregation-java-api-with-missing/131293/2 "2018-05-11T06:48:47Z")

</div>

Turns out I was looking in the wrong place and my index selection routine was at fault! You do feel a fool don't you.

---

<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:** [June 8, 2018, 6:49am UTC](https://discuss.elastic.co/t/terms-aggregation-java-api-with-missing/131293/3 "2018-06-08T06:49:12Z")

</div>

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