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"
            }
      }
    }
  }
}
            
