I am going to guess that the keys of the buckets in your "A"
results don't exactly line up to the keys of the buckets in your "A" or "B"
results. When you gave size: 5
in your sub-aggregation, that makes it so the "A" filter is limited to 5, and your "A" or "B"
results are limited to 5. It looks like out of that, 4 keys do correspond across both filters, but 1 key only exists in your "A"
filter, and another key only exists in your "A" or "B"
filter.
I can pretty much reproduce this with some test Apache log data, in the master branch of Kibana:
Now if I look in the "spy" panel and copy the aggregation DSL, and put it into Console (previously known as Sense), I get this:
Here's my actual response data:
"aggregations": {
"2": {
"buckets": {
"\"A\"": {
"3": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 1092,
"buckets": [
{
"key": "\"Mozilla/5.0 (compatible; Ezooms/1.0; help@moz.com)\"",
"doc_count": 736
},
{
"key": "\"Mozilla/5.0 (compatible; BLEXBot/1.0; +http://webmeup-crawler.com/)\"",
"doc_count": 609
},
{
"key": "\"Mozilla/5.0 (compatible; MJ12bot/v1.4.4; http://www.majestic12.co.uk/bot.php?+)\"",
"doc_count": 593
},
{
"key": "\"Mozilla/5.0 (compatible; AhrefsBot/5.0; +http://ahrefs.com/robot/)\"",
"doc_count": 550
},
{
"key": "\"Wotbox/2.01 (+http://www.wotbox.com/bot/)\"",
"doc_count": 278
}
]
},
"doc_count": 3858
},
"\"A\" or \"B\"": {
"3": {
"doc_count_error_upper_bound": 30,
"sum_other_doc_count": 2242,
"buckets": [
{
"key": "\"Mozilla/5.0 (compatible; Ezooms/1.0; help@moz.com)\"",
"doc_count": 738
},
{
"key": "\"Tiny Tiny RSS/1.11 (http://tt-rss.org/)\"",
"doc_count": 662
},
{
"key": "\"Mozilla/5.0 (compatible; BLEXBot/1.0; +http://webmeup-crawler.com/)\"",
"doc_count": 610
},
{
"key": "\"Mozilla/5.0 (compatible; MJ12bot/v1.4.4; http://www.majestic12.co.uk/bot.php?+)\"",
"doc_count": 594
},
{
"key": "\"Mozilla/5.0 (compatible; AhrefsBot/5.0; +http://ahrefs.com/robot/)\"",
"doc_count": 553
}
]
},
"doc_count": 5399
}
}
}
}
Now if you look at each inner-level bucket, they are each limited to 5, but which 5 they are isn't a complete overlap. It looks like that is what is happening in your case.