2013-08-28 19:24:34 -04:00
[[mapping-uid-field]]
2015-07-19 19:24:29 -04:00
=== `_uid` field
2013-08-28 19:24:34 -04:00
2017-07-05 06:30:19 -04:00
deprecated[6.0.0, Now that types have been removed, documents are uniquely identified by their `_id` and the `_uid` field has only been kept as a view over the `_id` field for backward compatibility.]
2017-05-09 10:33:52 -04:00
2015-07-19 19:24:29 -04:00
Each document indexed is associated with a <<mapping-type-field,`_type`>> (see
2015-08-06 11:24:29 -04:00
<<mapping-type>>) and an <<mapping-id-field,`_id`>>. These values are
combined as `{type}#{id}` and indexed as the `_uid` field.
2015-07-19 19:24:29 -04:00
The value of the `_uid` field is accessible in queries, aggregations, scripts,
and when sorting:
[source,js]
--------------------------
# Example documents
2017-12-14 11:47:53 -05:00
PUT my_index/_doc/1
2015-07-19 19:24:29 -04:00
{
"text": "Document with ID 1"
}
2017-12-14 11:47:53 -05:00
PUT my_index/_doc/2?refresh=true
2015-07-19 19:24:29 -04:00
{
"text": "Document with ID 2"
}
2017-05-09 10:33:52 -04:00
--------------------------
// CONSOLE
2015-07-19 19:24:29 -04:00
2017-05-09 10:33:52 -04:00
[source,js]
--------------------------
2015-07-19 19:24:29 -04:00
GET my_index/_search
{
"query": {
"terms": {
2017-12-14 11:47:53 -05:00
"_uid": [ "_doc#1", "_doc#2" ] <1>
2015-07-19 19:24:29 -04:00
}
},
"aggs": {
"UIDs": {
"terms": {
"field": "_uid", <2>
"size": 10
}
}
},
"sort": [
{
"_uid": { <3>
"order": "desc"
}
}
],
"script_fields": {
"UID": {
2016-06-27 09:55:16 -04:00
"script": {
"lang": "painless",
2017-06-09 11:29:25 -04:00
"source": "doc['_uid']" <4>
2016-06-27 09:55:16 -04:00
}
2015-07-19 19:24:29 -04:00
}
}
}
--------------------------
2016-05-09 09:42:23 -04:00
// CONSOLE
2017-05-09 10:33:52 -04:00
// TEST[continued]
// TEST[warning:Fielddata access on the _uid field is deprecated, use _id instead]
2015-07-19 19:24:29 -04:00
<1> Querying on the `_uid` field (also see the <<query-dsl-ids-query,`ids` query>>)
<2> Aggregating on the `_uid` field
<3> Sorting on the `_uid` field
2016-06-27 09:55:16 -04:00
<4> Accessing the `_uid` field in scripts