keep.asciidoc 2.8 KB

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