id-field.asciidoc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. [[mapping-id-field]]
  2. === `_id` field
  3. Each document indexed is associated with a <<mapping-type-field,`_type`>> (see
  4. <<all-mapping-types,Mapping Types>>) and an <<mapping-id-field,`_id`>>. The
  5. `_id` field is not indexed as its value can be derived automatically from the
  6. <<mapping-uid-field,`_uid`>> field.
  7. The value of the `_id` field is accessible in queries and scripts, but _not_
  8. in aggregations or when sorting, where the <<mapping-uid-field,`_uid`>> field
  9. should be used instead:
  10. [source,js]
  11. --------------------------
  12. # Example documents
  13. PUT my_index/my_type/1
  14. {
  15. "text": "Document with ID 1"
  16. }
  17. PUT my_index/my_type/2
  18. {
  19. "text": "Document with ID 2"
  20. }
  21. GET my_index/_search
  22. {
  23. "query": {
  24. "terms": {
  25. "_id": [ "1", "2" ] <1>
  26. }
  27. },
  28. "script_fields": {
  29. "UID": {
  30. "script": "doc['_id']" <2>
  31. }
  32. }
  33. }
  34. --------------------------
  35. // AUTOSENSE
  36. <1> Querying on the `_id` field (also see the <<query-dsl-ids-query,`ids` query>>)
  37. <2> Accessing the `_id` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)