# Aggregation only query returns hits with no aggregations

**URL:** <https://discuss.elastic.co/t/aggregation-only-query-returns-hits-with-no-aggregations/209520>\
**Category:** Elasticsearch\
**Created:** [November 26, 2019, 1:16pm UTC](https://discuss.elastic.co/t/aggregation-only-query-returns-hits-with-no-aggregations/209520 "2019-11-26T13:16:46Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![KamyarGhajar](https://avatars.discourse-cdn.com/v4/letter/k/65b543/32.png) [@KamyarGhajar](https://discuss.elastic.co/u/KamyarGhajar)\
**Post date:** [November 26, 2019, 1:16pm UTC](https://discuss.elastic.co/t/aggregation-only-query-returns-hits-with-no-aggregations/209520/1 "2019-11-26T13:16:46Z")

</div>

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**

```auto
{
  "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"
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
} 

```

---

<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:** [December 24, 2019, 1:16pm UTC](https://discuss.elastic.co/t/aggregation-only-query-returns-hits-with-no-aggregations/209520/2 "2019-12-24T13:16:46Z")

</div>

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