keep.asciidoc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. [discrete]
  2. [[esql-keep]]
  3. === `KEEP`
  4. The `KEEP` processing command enables you to specify what columns are returned
  5. and the order in which they are returned.
  6. **Syntax**
  7. [source,esql]
  8. ----
  9. KEEP columns
  10. ----
  11. *Parameters*
  12. `columns`::
  13. A comma-separated list of columns to keep. Supports wildcards.
  14. See below for the behavior in case an existing column matches multiple
  15. given wildcards or column names.
  16. *Description*
  17. The `KEEP` processing command enables you to specify what columns are returned
  18. and the order in which they are returned.
  19. Precedence rules are applied when a field name matches multiple expressions.
  20. Fields are added in the order they appear. If one field matches multiple expressions, the following precedence rules apply (from highest to lowest priority):
  21. 1. Complete field name (no wildcards)
  22. 2. Partial wildcard expressions (for example: `fieldNam*`)
  23. 3. Wildcard only (`*`)
  24. If a field matches two expressions with the same precedence, the rightmost expression wins.
  25. Refer to the examples for illustrations of these precedence rules.
  26. *Examples*
  27. The columns are returned in the specified order:
  28. [source.merge.styled,esql]
  29. ----
  30. include::{esql-specs}/docs.csv-spec[tag=keep]
  31. ----
  32. [%header.monospaced.styled,format=dsv,separator=|]
  33. |===
  34. include::{esql-specs}/docs.csv-spec[tag=keep-result]
  35. |===
  36. Rather than specify each column by name, you can use wildcards to return all
  37. columns with a name that matches a pattern:
  38. [source,esql]
  39. ----
  40. include::{esql-specs}/docs.csv-spec[tag=keepWildcard]
  41. ----
  42. [%header.monospaced.styled,format=dsv,separator=|]
  43. |===
  44. include::{esql-specs}/docs.csv-spec[tag=keep-wildcard-result]
  45. |===
  46. The asterisk wildcard (`*`) by itself translates to all columns that do not
  47. match the other arguments.
  48. This query will first return all columns with a name
  49. that starts with `h`, followed by all other columns:
  50. [source,esql]
  51. ----
  52. include::{esql-specs}/docs.csv-spec[tag=keepDoubleWildcard]
  53. ----
  54. [%header.monospaced.styled,format=dsv,separator=|]
  55. |===
  56. include::{esql-specs}/docs.csv-spec[tag=keep-double-wildcard-result]
  57. |===
  58. The following examples show how precedence rules work when a field name matches multiple expressions.
  59. Complete field name has precedence over wildcard expressions:
  60. [source,esql]
  61. ----
  62. include::{esql-specs}/docs.csv-spec[tag=keepCompleteName]
  63. ----
  64. [%header.monospaced.styled,format=dsv,separator=|]
  65. |===
  66. include::{esql-specs}/docs.csv-spec[tag=keep-complete-name-result]
  67. |===
  68. Wildcard expressions have the same priority, but last one wins (despite being less specific):
  69. [source,esql]
  70. ----
  71. include::{esql-specs}/docs.csv-spec[tag=keepWildcardPrecedence]
  72. ----
  73. [%header.monospaced.styled,format=dsv,separator=|]
  74. |===
  75. include::{esql-specs}/docs.csv-spec[tag=keep-wildcard-precedence-result]
  76. |===
  77. A simple wildcard expression `*` has the lowest precedence.
  78. Output order is determined by the other arguments:
  79. [source,esql]
  80. ----
  81. include::{esql-specs}/docs.csv-spec[tag=keepWildcardLowest]
  82. ----
  83. [%header.monospaced.styled,format=dsv,separator=|]
  84. |===
  85. include::{esql-specs}/docs.csv-spec[tag=keep-wildcard-lowest-result]
  86. |===