# Query with different sort on different index

**URL:** <https://discuss.elastic.co/t/query-with-different-sort-on-different-index/124115>\
**Category:** Elasticsearch\
**Created:** [March 15, 2018, 2:44pm UTC](https://discuss.elastic.co/t/query-with-different-sort-on-different-index/124115 "2018-03-15T14:44:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Varan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varan/32/32120_2.png) [@Varan](https://discuss.elastic.co/u/Varan)\
**Post date:** [March 15, 2018, 2:44pm UTC](https://discuss.elastic.co/t/query-with-different-sort-on-different-index/124115/1 "2018-03-15T14:44:10Z")

</div>

Having two index which got different field, could I put a query that sort by index name, and then sort on a field on one and sort on another on the second?

#In the two next query I aim to sort by "code\_index1" if the document belong to index1 and by "this\_field\_of\_index2" if the document belong to index2.

```
GET _search
{
  "sort": [
    {
      "_index": {
        "order": "asc"
      }
    },
    {
      "code_index1": {
        "order": "asc"
      }
    },
    {
      "this_field_of_index2": {
        "order": "asc"
      }
    },
  ]
}

```

Could a script help me?

```
GET index1,index2/_search
{
  "query": {
    "match_all": {}
    },
    "sort": {
    "_script": {
      "type": "string",
      "order": "asc",
      "script": {
        "lang": "painless",
        "source": "if (ctx._source ==~ params.foo) {return \"code_index1\"} "if (ctx._source ==~ params.bar) {return \"this_field_of_index2\"} else {ctx.op = 'noop'}",
            "params": {
           "foo": "index1"
           "bar": "index2"
            }
      }
    }
  }
}

```

Could I even do different sort based on a field of an index?

#Here I want to sort by age if code=child and by city if code=adult

```
GET index1/_search
{
  "query": {
    "match_all": {}
    },
    "sort": {
    "_script": {
      "type": "string",
      "order": "asc",
      "script": {
        "lang": "painless",
        "source": "if (ctx._source.code ==~ params.foo) {return \"age\"} "if (ctx._source.code ==~ params.bar) {return \"city\"} else {ctx.op = 'noop'}",
            "params": {
           "foo": "child"
           "bar": "adult"
            }
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![Varan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varan/32/32120_2.png) [@Varan](https://discuss.elastic.co/u/Varan)\
**Post date:** [March 15, 2018, 2:49pm UTC](https://discuss.elastic.co/t/query-with-different-sort-on-different-index/124115/2 "2018-03-15T14:49:02Z")

</div>

Obviously, none of the above queries work, I just wanted to show were I was at the moment. 🤔  
Maybe I just miss a little something in them.

---

<div class="post-metadata">

**Author:** ![Varan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varan/32/32120_2.png) [@Varan](https://discuss.elastic.co/u/Varan)\
**Post date:** [March 15, 2018, 3:49pm UTC](https://discuss.elastic.co/t/query-with-different-sort-on-different-index/124115/3 "2018-03-15T15:49:54Z")

</div>

I'v answer to the first question. 😉  
Still looking for the second one.

```
GET index1,index2/_search
{
  "query": {
    "match_all": {} 
    },
    "sort": {
    "_script": {
      "type": "string",
      "order": "asc",
      "script": {
        "lang": "painless",
        "source": "if (doc.containsKey('code_index1')) { doc['code_index1'].value } else { doc['this_field_of_index2'].value}"
      }
    }
  }
}
```

---

<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:** [April 12, 2018, 3:50pm UTC](https://discuss.elastic.co/t/query-with-different-sort-on-different-index/124115/4 "2018-04-12T15:50:05Z")

</div>

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