| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 | [[mapping-index-field]]=== `_index` fieldWhen performing queries across multiple indexes, it is sometimes desirableto add query clauses that are associated with documents of only certainindexes. The `_index` field allows matching on the index a document wasindexed into. Its value is accessible in queries, aggregations, scripts, and when sorting:[source,js]--------------------------# Example documentsPUT index_1/my_type/1{  "text": "Document in index 1"}PUT index_2/my_type/2{  "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": "doc['_index']" <4>    }  }}--------------------------// AUTOSENSE<1> Querying on the `_index` field<2> Aggregating on the `_index` field<3> Sorting on the `_index` field<4> Accessing the `_index` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)
 |