# Missing value in multi-level term aggregation

**URL:** <https://discuss.elastic.co/t/missing-value-in-multi-level-term-aggregation/63916>\
**Category:** Elasticsearch\
**Created:** [October 25, 2016, 6:07pm UTC](https://discuss.elastic.co/t/missing-value-in-multi-level-term-aggregation/63916 "2016-10-25T18:07:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharon.c](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sharon.c/32/16076_2.png) [@sharon.c](https://discuss.elastic.co/u/sharon.c)\
**Post date:** [October 25, 2016, 6:07pm UTC](https://discuss.elastic.co/t/missing-value-in-multi-level-term-aggregation/63916/1 "2016-10-25T18:07:33Z")

</div>

In my aggregation query, I need to get the top 5 of the highest count matching docs. I have a 2 levels aggregation using two terms: userid and username, and in some documents username field is missing. This creates a big problem for me in the aggregation, because if the username is missing, then the aggregation result does not include even the userid.  
For example, if I use this 1 level aggregation on userid

> ```
> "aggs": 
> {
> "userid": 
> {
> "terms": 
> {
> "field": "userid",
> "size": 5
> }
> }
> }
> 
> ```

The result is:

> 

Userid Count  
user1 200  
user2 140  
user3 120  
user4 90  
user5 80

If I use this 2 level aggregation on userid and username

> ```
> "aggs": 
> {
> "userid": 
> {
> "terms": 
> {
> "field": "userid",
> "size": 5
> },
> "aggs": 
> {
> "username": 
> {
> "terms": 
> {
> "field": "username",
> "size": 1
> }
> }
> }
> }
> }
> 
> ```

The result is the following, and user3 is missing, because user3 does not have username field

> 

Userid Username Count  
user1 Dave 200  
user2 Monica 140  
user4 Harry 90  
user5 Nancy 80  
user6 Berry 70

I can use missing value settings like the following in my query,  
"missing": "N/A"  
The result will be like this:

> 

Userid Username Count  
user1 Dave 200  
user2 Monica 140  
user3 N/A 120  
user4 Harry 90  
user5 Nancy 80

But it is just a fixed missing value, is there any way to fall back to the upper level term in the final result as such:

> 

Userid Username Count  
user1 Dave 200  
user2 Monica 140  
user3 user3 120  
user4 Harry 90  
user5 Nancy 80

---

<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:** [July 5, 2017, 10:09pm UTC](https://discuss.elastic.co/t/missing-value-in-multi-level-term-aggregation/63916/2 "2017-07-05T22:09:51Z")

</div>


