# "illegal\_state\_exception" occurs top\_hits aggregator is used

**URL:** <https://discuss.elastic.co/t/illegal-state-exception-occurs-top-hits-aggregator-is-used/155267>\
**Category:** Elasticsearch\
**Created:** [November 3, 2018, 5:41pm UTC](https://discuss.elastic.co/t/illegal-state-exception-occurs-top-hits-aggregator-is-used/155267 "2018-11-03T17:41:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![3ms1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/3ms1/32/54472_2.png) [@3ms1](https://discuss.elastic.co/u/3ms1)\
**Post date:** [November 3, 2018, 5:41pm UTC](https://discuss.elastic.co/t/illegal-state-exception-occurs-top-hits-aggregator-is-used/155267/1 "2018-11-03T17:41:37Z")

</div>

Hi, I am having a trouble to find out the actual problem why "illegal\_state\_exception" occurs when top\_hits is used in Elasticsearch 6.4.2 even though same query and aggregations work in the same version but on another machine, which is the local environment.

Any opinions?

BTW, my goal is to sort by text on buckets.

Below you may find partial settings with mapping also the aggregation.

```json
"settings": {
  "analysis": {
    "normalizer": {
      "custom_normalizer": {
        "filter": [
          "lowercase",
          "asciifolding"
        ],
        "type": "custom",
        "char_filter": []
      }
    }
  }
}

"mapping": {
  "my_type": {
    "dynamic": "false",
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "raw": {
            "type": "keyword",
            "normalizer": "custom_normalizer"
          }
        },
        "analyzer": "english"
      },
    }
  }
}

"aggs": {
  "tags": {
    "children": {
      "type": "tags"
    },
    "aggs": {
      "platforms": {
        "terms": {
          "field": "name.raw",
          "size": 100,
          "order": {
            "_key": "asc"
          }
        },
        "aggs": {
          "platform": {
            "top_hits": {
              "size": 1,
              "_source": true
            }
          },
          "bucket_truncate": {
            "bucket_sort": {
              "from": 0,
              "size": 250
            }
          }
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [November 5, 2018, 12:56pm UTC](https://discuss.elastic.co/t/illegal-state-exception-occurs-top-hits-aggregator-is-used/155267/2 "2018-11-05T12:56:18Z")

</div>

please use markdown to properly format the JSON snippet and also share the full stack trace. thanks!

---

<div class="post-metadata">

**Author:** ![3ms1](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/3ms1/32/54472_2.png) [@3ms1](https://discuss.elastic.co/u/3ms1)\
**Post date:** [November 5, 2018, 1:59pm UTC](https://discuss.elastic.co/t/illegal-state-exception-occurs-top-hits-aggregator-is-used/155267/3 "2018-11-05T13:59:11Z")

</div>

I found out that the issue was related to nested queries. Thanks!

---

<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 3, 2018, 1:59pm UTC](https://discuss.elastic.co/t/illegal-state-exception-occurs-top-hits-aggregator-is-used/155267/4 "2018-12-03T13:59:22Z")

</div>

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