index-field.asciidoc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. [[mapping-index-field]]
  2. === `_index` field
  3. The name of the index that contains the document. This field is not indexed
  4. but can be automatically derived from the index itself.
  5. Its value is accessible in queries, aggregations, scripts, and when sorting:
  6. [source,js]
  7. --------------------------
  8. # Example documents
  9. PUT index_1/my_type/1
  10. {
  11. "text": "Document in index 1"
  12. }
  13. PUT index_2/my_type/2
  14. {
  15. "text": "Document in index 2"
  16. }
  17. GET index_1,index_2/_search
  18. {
  19. "query": {
  20. "terms": {
  21. "_index": ["index_1", "index_2"] <1>
  22. }
  23. },
  24. "aggs": {
  25. "indices": {
  26. "terms": {
  27. "field": "_index", <2>
  28. "size": 10
  29. }
  30. }
  31. },
  32. "sort": [
  33. {
  34. "_index": { <3>
  35. "order": "asc"
  36. }
  37. }
  38. ],
  39. "script_fields": {
  40. "index_name": {
  41. "script": "doc['_index']" <4>
  42. }
  43. }
  44. }
  45. --------------------------
  46. // AUTOSENSE
  47. <1> Querying on the `_index` field
  48. <2> Aggregating on the `_index` field
  49. <3> Sorting on the `_index` field
  50. <4> Accessing the `_index` field in scripts (inline scripts must be <<enable-dynamic-scripting,enabled>> for this example to work)