| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 | [[mapping-index-field]]=== `_index` fieldWhen performing queries across multiple indexes, it is sometimes desirable toadd query clauses that are associated with documents of only certain indexes.The `_index` field allows matching on the index a document was indexed into.Its value is accessible in certain queries and aggregations, and when sortingor scripting:[source,console]--------------------------PUT index_1/_doc/1{  "text": "Document in index 1"}PUT index_2/_doc/2?refresh=true{  "text": "Document in index 2"}GET index_1,index_2/_search{  "query": {    "terms": {      "_index": ["index_1", "index_2"] <1>    }  },  "aggs": {    "indices": {      "terms": {        "field": "_index", <2>        "size": 10      }    }  },  "sort": [    {      "_index": { <3>        "order": "asc"      }    }  ],  "script_fields": {    "index_name": {      "script": {        "lang": "painless",        "source": "doc['_index']" <4>      }    }  }}--------------------------<1> Querying on the `_index` field<2> Aggregating on the `_index` field<3> Sorting on the `_index` field<4> Accessing the `_index` field in scriptsThe `_index` field is exposed virtually -- it is not added to the Lucene indexas a real field. This means that you can use the `_index` field in a `term` or`terms` query (or any query that is rewritten to a `term` query, such as the`match`,  `query_string` or `simple_query_string` query), as well as `prefix`and `wildcard` queries. However, it does not support `regexp` and `fuzzy`queries.Queries on the `_index` field accept index aliases in addition to concreteindex names.NOTE: When specifying a remote index name such as `cluster_1:index_3`, thequery must contain the separator character `:`. For example, a `wildcard` queryon `cluster_*:index_3` would match documents from the remote index. However, aquery on `cluster*index_1` is only matched against local indices, since noseparator is present. This behavior aligns with the usual resolution rules forremote index names.
 |