painless-runtime-fields.asciidoc 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. [[painless-runtime-fields]]
  2. === Use Painless scripts in runtime fields
  3. A runtime field is a field that is evaluated at query time. When you define a
  4. runtime field, you can immediately use it in search requests, aggregations,
  5. filtering, and sorting.
  6. When defining a runtime field, you can include a Painless script that is
  7. evaluated at query time. This script has access to the entire context of a
  8. document, including the original `_source` and any mapped fields plus their
  9. values. At query time, the script runs and generates values for each scripted
  10. field that is included in the query.
  11. You can map a runtime field in the `runtime` section under the mapping
  12. definition, or define runtime fields that exist only as part of a search
  13. request. The script syntax is the same, regardless of where you define the
  14. runtime field.
  15. IMPORTANT: When defining a Painless script to use with runtime fields, you must
  16. include `emit` to return calculated values.
  17. [discrete]
  18. [[painless-runtime-fields-mapping]]
  19. ==== Define a runtime field in the mapping
  20. Add a `runtime` section under the {ref}/runtime-mapping-fields.html[mapping definition] to explore your data without indexing fields.
  21. The script in the following request extracts the day of the week from the
  22. `@timestamp` field, which is defined as a `date` type. The script calculates
  23. the day of the week based on the value of `timestamp`, and uses `emit` to
  24. return the calculated value.
  25. [source,console]
  26. ----
  27. PUT my-index/
  28. {
  29. "mappings": {
  30. "runtime": {
  31. "day_of_week": {
  32. "type": "keyword",
  33. "script": {
  34. "source":
  35. """emit(doc['@timestamp'].value.dayOfWeekEnum
  36. .getDisplayName(TextStyle.FULL, Locale.ROOT))"""
  37. }
  38. }
  39. },
  40. "properties": {
  41. "timestamp": {"type": "date"}
  42. }
  43. }
  44. }
  45. ----
  46. [discrete]
  47. [[painless-runtime-fields-query]]
  48. ==== Define a runtime field only in a search request
  49. Use runtime fields in a search request to create a field that exists
  50. {ref}/runtime-search-request.html[only as part of the query]. You can also {ref}/runtime-override-values.html[override field values] at query time for existing fields without
  51. modifying the field itself.
  52. This flexibility allows you to experiment with your data schema and fix
  53. mistakes in your index mapping without reindexing your data.
  54. In the following request, the values for the `day_of_week` field are calculated
  55. dynamically, and only within the context of this search request:
  56. [source,console]
  57. ----
  58. GET my-index/_search
  59. {
  60. "runtime_mappings": {
  61. "day_of_week": {
  62. "type": "keyword",
  63. "script": {
  64. "source":
  65. """emit(doc['@timestamp'].value.dayOfWeekEnum
  66. .getDisplayName(TextStyle.FULL, Locale.ROOT))"""
  67. }
  68. }
  69. },
  70. "aggs": {
  71. "day_of_week": {
  72. "terms": {
  73. "field": "day_of_week"
  74. }
  75. }
  76. }
  77. }
  78. ----
  79. //TEST[continued]