Aggregation only query returns hits with no aggregations

I have a query that works properly sometimes. This query is simple bool query with nested composite(on terms) aggregations with hits size of zero because I only need the terms agg results so I can group the results by a certain field's value with top hits agg later. In response sometimes with the exactly same query I get the proper results, but, most of the times I have no aggregations field in the body and instead I have a bunch oh hits (that should have been empty corresponding to the query I mentioned.) What is the problem exactly?
P.S. 1: The size of the composite agg is set to 10 (so, it is not too much.)
P.S. 2: When I get the wrong response I get a num_reduce_phases: 3 in the body.
P.S. 3: The elasticsearch version is 7.3.1 and I use latest elasticsearch js API in NodeJS.
Sample Query

{
  "query": {
    "function_score": {
      "functions": [],
      "query": {
        "bool": {
          "must": {
            "term": {
              "supported_version": true
            }
          },
          "must_not": {
            "term": {
              "token": "test"
            }
          }
        }
      }
    }
  },
  "size": 0,
  "aggs": {
    "attempts": {
      "composite": {
        "sources": [
          {
            "attempt_id": {
              "terms": {
                "field": "attempt_id.keyword"
              }
            }
          }
        ],
        "size": 10,
        "after": {
          "attempt_id": "00000608-684a-44ef-a6d2-69ccb7569ea8"
        }
      },
      "aggs": {
        "log": {
          "filter": {
            "term": {
              "endpoint": "log"
            }
          },
          "aggs": {
            "last_log": {
              "top_hits": {
                "sort": [
                  {
                    "datetime": "desc"
                  }
                ],
                "size": 1,
                "_source": {
                  "includes": [
                    "attempt_id",
                    "input",
                    "location"
                  ]
                }
              }
            }
          }
        },
        "fix": {
          "filter": {
            "bool": {
              "minimum_should_match": 1,
              "should": [
                {
                  "term": {
                    "action_code": 1
                  }
                },
                {
                  "term": {
                    "action_code": 2
                  }
                }
              ]
            }
          },
          "aggs": {
            "last_fix": {
              "top_hits": {
                "sort": [
                  {
                    "datetime": "desc"
                  }
                ],
                "size": 1,
                "_source": {
                  "includes": [
                    "attempt_id",
                    "start",
                    "end"
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
} 

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