Why does elasticsearch terms bucket size effects the doc_count of inner reverse_nested aggregations?

I've been trying to track down missing doc counts from a reverse nested aggregation.

My query

"aggs": {
    "mainGrouping": {
      "nested": {
        "path": "parent.child"
      },
      "aggs": {
        "uniqueCount": {
          "cardinality": {
            "field": "parent.child.id"
          }
        },
        "groupBy": {
          "terms": {
            "field": "parent.child.id",
            "size": 20,  <- If I change this, my doc count for noOfParents changes
            "order": [
              {
                "noOfParents": "desc"
              }
            ]
          },
          "aggs": {
            "noOfParents": {
              "reverse_nested": {}
            }
          }
        }
      }
    }

So I was running it at size:20 . I had one bucket that returned noOfParents of 7 when I know there should be 9 matches. I noticed by accident if I change the size of the terms aggregation to 50 the noOfParents was correctly showing 9 for this bucket.

Why would the size of the terms aggregation affect the doc_count of a reverse aggregation? Is this expected behaviour or a bug? I'm using elasticsearch 5.6.

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