id-field.asciidoc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. [[mapping-id-field]]
  2. === `_id` field
  3. Each document has an `_id` that uniquely identifies it, which is indexed
  4. so that documents can be looked up either with the <<docs-get,GET API>> or the
  5. <<query-dsl-ids-query,`ids` query>>.
  6. NOTE: This was not the case with pre-6.0 indices due to the fact that they
  7. supported multiple types, so the `_type` and `_id` were merged into a composite
  8. primary key called `_uid`.
  9. The value of the `_id` field is accessible in certain queries (`term`,
  10. `terms`, `match`, `query_string`, `simple_query_string`).
  11. [source,js]
  12. --------------------------
  13. # Example documents
  14. PUT my_index/my_type/1
  15. {
  16. "text": "Document with ID 1"
  17. }
  18. PUT my_index/my_type/2&refresh=true
  19. {
  20. "text": "Document with ID 2"
  21. }
  22. GET my_index/_search
  23. {
  24. "query": {
  25. "terms": {
  26. "_id": [ "1", "2" ] <1>
  27. }
  28. }
  29. }
  30. --------------------------
  31. // CONSOLE
  32. <1> Querying on the `_id` field (also see the <<query-dsl-ids-query,`ids` query>>)
  33. The value of the `_id` field is also accessible in aggregations or for sorting,
  34. but doing so is discouraged as it requires to load a lot of data in memory. In
  35. case sorting or aggregating on the `_id` field is required, it is advised to
  36. duplicate the content of the `_id` field in another field that has `doc_values`
  37. enabled.