# Custom sorting query results by index name with scripting

**URL:** <https://discuss.elastic.co/t/custom-sorting-query-results-by-index-name-with-scripting/306176>\
**Category:** Elasticsearch\
**Created:** [June 2, 2022, 1:29am UTC](https://discuss.elastic.co/t/custom-sorting-query-results-by-index-name-with-scripting/306176 "2022-06-02T01:29:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![corndog](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/corndog/32/96803_2.png) [@corndog](https://discuss.elastic.co/u/corndog)\
**Post date:** [June 2, 2022, 1:29am UTC](https://discuss.elastic.co/t/custom-sorting-query-results-by-index-name-with-scripting/306176/1 "2022-06-02T01:29:55Z")

</div>

I have 4 indexes: first, second, third, fourth.

I have a query that searches text on a field called "name" against all indexes.

```auto
POST /_all/_search?pretty
{
  "_source": ["name"], 
  "query": {
    "match": {
      "name": {
        "query": "testing"
      }
    }
  },
  "sort": {
    ????
  }
}

```

I want my query results to be sorted like so:

- All matching docs from index "first"
- All matching docs from index "second"
- All matching docs from index "third"
- All matching docs from index "fourth"

How can I do this, what do I put in the place of ???? above!

---

<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:** [June 30, 2022, 1:30am UTC](https://discuss.elastic.co/t/custom-sorting-query-results-by-index-name-with-scripting/306176/2 "2022-06-30T01:30:20Z")

</div>

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