keep.asciidoc 2.9 KB

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