index-field.asciidoc 1.2 KB

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