# Index sorting causes illegal\_state\_exception

**URL:** <https://discuss.elastic.co/t/index-sorting-causes-illegal-state-exception/271592>\
**Category:** Elasticsearch\
**Created:** [April 29, 2021, 6:23am UTC](https://discuss.elastic.co/t/index-sorting-causes-illegal-state-exception/271592 "2021-04-29T06:23:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![buinauskas\_evaldas](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/buinauskas_evaldas/32/31439_2.png) [@buinauskas\_evaldas](https://discuss.elastic.co/u/buinauskas_evaldas)\
**Post date:** [April 29, 2021, 6:23am UTC](https://discuss.elastic.co/t/index-sorting-causes-illegal-state-exception/271592/1 "2021-04-29T06:23:52Z")

</div>

Heavily inspired by the [Introducing Index Sorting in Elasticsearch 6.0 | Elastic Blog](https://www.elastic.co/blog/index-sorting-elasticsearch-6-0) article I went to try index sorting out for faster AND conjunctions.

This is my index structure:

```json
{
  "index_name": {
    "mappings": {
      "dynamic": "false",
      "properties": {
        "brand_id": { "type": "keyword" },
        "category_id": { "type": "keyword" },
        "status_id": { "type": "keyword" },
        "user_id": { "type": "keyword" },
        "price": { "type": "scaled_float", "scaling_factor": 100.0 },
        "created_at": { "type": "date" }
      }
    },
    "settings": {
      "index": {
        "sort": {
          "field": ["category_id", "brand_id", "status_id", "created_at"],
          "order": ["asc", "asc", "asc", "desc"]
        }
      }
    }
  }
}

```

And this is the actual query:

```json
{
  "_source": false,
  "size": 24,
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "category_id": 197
          }
        },
        {
          "term": {
            "brand_id": 51
          }
        },
        {
          "term": {
            "status_id": 2
          }
        },
        {
          "range": {
            "price": {
              "gte": 2.0,
              "lte": 5.0
            }
          }
        }
      ],
      "must_not": {
        "terms": {
          "user_id": [12345]
        }
      }
    }
  },
  "sort": {
    "created_at": "desc"
  }
}

```

The results have been mixed.

Some performed great and their performance has skyrocketed, however, some became completely horrible and started throwing the following exceptions:

```json
{
  "error": {
    "root_cause": [
      {
        "type": "illegal_state_exception",
        "reason": "topDocs already consumed"
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "index_name",
        "node": "lx9S3vwES4y2n2xa-PFyUw",
        "reason": {
          "type": "illegal_state_exception",
          "reason": "topDocs already consumed"
        }
      }
    ],
    "caused_by": {
      "type": "illegal_state_exception",
      "reason": "topDocs already consumed",
      "caused_by": {
        "type": "illegal_state_exception",
        "reason": "topDocs already consumed"
      }
    }
  },
  "status": 500
}

```

This surely must be related to index sorting but I couldn't find anything indicating why this could be raised.

- The index contains 100M documents and weights 10G
- Elasticsearch 7.12.0
- Using built-in JVM
- Running on CentOS servers

---

<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 27, 2021, 6:24am UTC](https://discuss.elastic.co/t/index-sorting-causes-illegal-state-exception/271592/2 "2021-05-27T06:24:19Z")

</div>

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