[[mapping-type-field]]
=== `_type` field

Each document indexed is associated with a <<mapping-type-field,`_type`>> (see
<<mapping-type>>) and an <<mapping-id-field,`_id`>>.  The `_type` field is
indexed in order to make searching by type name fast.

The value of the `_type` field is accessible in queries, aggregations,
scripts, and when sorting:

[source,js]
--------------------------
# Example documents
PUT my_index
{
  "settings": {
    "mapping.single_type": false
  }
}

PUT my_index/type_1/1
{
  "text": "Document with type 1"
}

PUT my_index/type_2/2?refresh=true
{
  "text": "Document with type 2"
}

GET my_index/_search
{
  "query": {
    "terms": {
      "_type": [ "type_1", "type_2" ] <1>
    }
  },
  "aggs": {
    "types": {
      "terms": {
        "field": "_type", <2>
        "size": 10
      }
    }
  },
  "sort": [
    {
      "_type": { <3>
        "order": "desc"
      }
    }
  ],
  "script_fields": {
    "type": {
      "script": {
        "lang": "painless",
        "source": "doc['_type']" <4>
      }
    }
  }
}

--------------------------
// CONSOLE

<1> Querying on the `_type` field
<2> Aggregating on the `_type` field
<3> Sorting on the `_type` field
<4> Accessing the `_type` field in scripts

