index-field.asciidoc 1.6 KB

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