id-field.asciidoc 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. [[mapping-id-field]]
  2. === `_id` field
  3. Each document indexed is associated with a <<mapping-type-field,`_type`>> (see
  4. <<mapping-type>>) and an <<mapping-id-field,`_id`>>. The `_id` field is not
  5. 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 certain queries (`term`,
  8. `terms`, `match`, `query_string`, `simple_query_string`) and scripts, but
  9. _not_ in aggregations or when sorting, where the <<mapping-uid-field,`_uid`>>
  10. field should be used instead:
  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
  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. "script_fields": {
  30. "UID": {
  31. "script": "doc['_id']" <2>
  32. }
  33. }
  34. }
  35. --------------------------
  36. // AUTOSENSE
  37. <1> Querying on the `_id` field (also see the <<query-dsl-ids-query,`ids` query>>)
  38. <2> Accessing the `_id` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)