Missing value in multi-level term aggregation


#1

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


(system) #2