# Query + composite + top\_hits = exception

**URL:** <https://discuss.elastic.co/t/query-composite-top-hits-exception/227225>\
**Category:** Elasticsearch\
**Created:** [April 9, 2020, 2:39am UTC](https://discuss.elastic.co/t/query-composite-top-hits-exception/227225 "2020-04-09T02:39:51Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jjacob](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jjacob/32/47349_2.png) [@jjacob](https://discuss.elastic.co/u/jjacob)\
**Post date:** [April 9, 2020, 2:39am UTC](https://discuss.elastic.co/t/query-composite-top-hits-exception/227225/1 "2020-04-09T02:39:51Z")

</div>

I have a query that throws an exception, and I can't seem to work around it. My actual configuration is a bit more complex than what I have below, but I've managed to cook up an example that still produces the error.

Running Elasticsearch 7.6.2.

My index and mappings:

```auto
PUT /test
PUT /test/_mappings
{
  "properties": {
    "rootProperty": {
      "type": "keyword"
    },
    "children": {
      "type": "nested",
      "properties": {
        "childPropertyA": {
          "type": "keyword"
        },
        "childPropertyB": {
          "type": "text"
        }
      }
    }
  }
}

```

I then insert a document:

```auto
PUT /test/_doc/1
{
  "rootProperty": "root",
  "children": [
    {
      "childPropertyA": "1",
      "childPropertyB": "abc"
    },
    {
      "childPropertyA": "2",
      "childPropertyB": "def"
    },
    {
      "childPropertyA": "3",
      "childPropertyB": "ghi"
    },
    {
      "childPropertyA": "4",
      "childPropertyB": "jkl"
    }
  ]
}

```

I then perform the following query:

```auto
GET /test/_search
{
  "size": 0,
  "query": {
    "bool": {
      "filter": [
        { "term": { "rootProperty": "root" }}
      ]
    }
  },
  "aggs": {
    "aggsNested": {
      "nested": {
        "path": "children"
      },
      "aggs": {
        "aggsChildPropertyAValues": {
          "composite": {
            "size": 100,
            "sources": [
              { "childPropertyAValues": { "terms": { "field": "children.childPropertyA" }} }
            ]
          },
          "aggs": {
            "aggsAdditionalData": {
              "top_hits": {
                "size": 1,
                "_source": { 
                  "includes": [
                    "children.childPropertyB"
                  ]
                }
              }
            }
          }
        }   
      }
    }
  }
}

```

I receive this exception:

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "array_index_out_of_bounds_exception",
        "reason" : "Index 129 out of bounds for length 129"
      }
    ],
    "type" : "search_phase_execution_exception",
    "reason" : "all shards failed",
    "phase" : "query",
    "grouped" : true,
    "failed_shards" : [
      {
        "shard" : 0,
        "index" : "test",
        "node" : "8vCzoJ9HS7KsMD168FxXqg",
        "reason" : {
          "type" : "array_index_out_of_bounds_exception",
          "reason" : "Index 129 out of bounds for length 129"
        }
      }
    ],
    "caused_by" : {
      "type" : "array_index_out_of_bounds_exception",
      "reason" : "Index 129 out of bounds for length 129",
      "caused_by" : {
        "type" : "array_index_out_of_bounds_exception",
        "reason" : "Index 129 out of bounds for length 129"
      }
    }
  },
  "status" : 500
}

```

Some notes on this setup:

- The query in the search is meant to filter down the data before it is to be aggregated.
- I'm using nested child documents because the length of that array can get large, and I need to maintain the structure of the child data.
- The composite over childPropertyA is meant to extract all the unique childPropertyA values across the data.
- I'm using a composite so I can 'page' through the data to get all unique values
- The size of 100 on the composite was arbitrarily set in my initial tests, but there would be an optimal value for the live system.
- I have a child top\_hits child aggregate because I need additional information for each of the unique values returned. Some additional properties from the child objects.

Some observations while 'playing' with the query:

- If I set the size on the composite to 1, or 2, the query runs, but if I set it to 3 or above, I get the exception.
- If I remove the root level query that filters down the data, the query runs
- If I remove the top\_hits child aggregation, the query runs.

Any help would be much appreciated.

---

<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:** [May 7, 2020, 2:50am UTC](https://discuss.elastic.co/t/query-composite-top-hits-exception/227225/2 "2020-05-07T02:50:22Z")

</div>

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