esql-kibana.asciidoc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. [[esql-kibana]]
  2. === Using {esql} in {kib}
  3. ++++
  4. <titleabbrev>Using {esql} in {kib}</titleabbrev>
  5. ++++
  6. You can use {esql} in {kib} to query and aggregate your data, create
  7. visualizations, and set up alerts.
  8. This guide shows you how to use {esql} in Kibana. To follow along with the
  9. queries, load the "Sample web logs" sample data set by selecting **Sample Data**
  10. from the **Integrations** page in {kib}, selecting *Other sample data sets*,
  11. and clicking *Add data* on the *Sample web logs* card.
  12. [discrete]
  13. [[esql-kibana-enable]]
  14. === Enable or disable {esql}
  15. {esql} is enabled by default in {kib}. It can be
  16. disabled using the `enableESQL` setting from the
  17. {kibana-ref}/advanced-options.html[Advanced Settings].
  18. This will hide the {esql} user interface from various applications.
  19. However, users will be able to access existing {esql} artifacts like saved searches and visualizations.
  20. [discrete]
  21. [[esql-kibana-get-started]]
  22. === Get started with {esql}
  23. // tag::esql-mode[]
  24. To get started with {esql} in Discover, open the main menu and select
  25. *Discover*. Next, select *Try ES|QL* from the application menu bar.
  26. // end::esql-mode[]
  27. [discrete]
  28. [[esql-kibana-query-bar]]
  29. === The query bar
  30. After switching to {esql} mode, the query bar shows a sample query. For example:
  31. [source,esql]
  32. ----
  33. from kibana_sample_data_logs | limit 10
  34. ----
  35. Every query starts with a <<esql-commands,source command>>. In this query, the
  36. source command is <<esql-from>>. `FROM` retrieves data from data streams, indices, or
  37. aliases. In this example, the data is retrieved from `kibana_sample_data_logs`.
  38. A source command can be followed by one or more <<esql-commands,processing
  39. commands>>. In this query, the processing command is <<esql-limit>>. `LIMIT`
  40. limits the number of rows that are retrieved.
  41. TIP: Click the **ES|QL help** button to open the
  42. in-product reference documentation for all commands and functions or to get
  43. recommended queries that will help you get started.
  44. // tag::autocomplete[]
  45. To make it easier to write queries, auto-complete offers suggestions with
  46. possible commands and functions:
  47. image::images/esql/esql-kibana-auto-complete.png[align="center"]
  48. // end::autocomplete[]
  49. [NOTE]
  50. ====
  51. {esql} keywords are case-insensitive. The following query is identical to the
  52. previous one:
  53. [source,esql]
  54. ----
  55. FROM kibana_sample_data_logs | LIMIT 10
  56. ----
  57. ====
  58. [discrete]
  59. ==== Make your query readable
  60. For readability, you can put each processing command on a new line. The
  61. following query is identical to the previous one:
  62. [source,esql]
  63. ----
  64. FROM kibana_sample_data_logs
  65. | LIMIT 10
  66. ----
  67. You can do that using the **Add line breaks on pipes** button from the query editor's footer.
  68. image::https://images.contentstack.io/v3/assets/bltefdd0b53724fa2ce/bltd5554518309e10f6/672d153cfeb8f9d479ebcc6e/esql-line-breakdown.gif[Automatic line breaks for ES|QL queries]
  69. // tag::compact[]
  70. You can adjust the editor's height by dragging its bottom border to your liking.
  71. // end::compact[]
  72. [discrete]
  73. ==== Warnings
  74. A query may result in warnings, for example when querying an unsupported field
  75. type. When that happens, a warning symbol is shown in the query bar. To see the
  76. detailed warning, expand the query bar, and click *warnings*.
  77. [discrete]
  78. [[esql-kibana-query-history]]
  79. ==== Query history
  80. You can reuse your recent {esql} queries in the query bar.
  81. In the query bar click *Show recent queries*.
  82. You can then scroll through your recent queries:
  83. image::images/esql/esql-discover-query-history.png[align="center",size="50%"]
  84. [discrete]
  85. [[esql-kibana-results-table]]
  86. === The results table
  87. For the example query, the results table shows 10 rows. Omitting the `LIMIT`
  88. command, the results table defaults to up to 1000 rows. Using `LIMIT`, you can
  89. increase the limit to up to 10,000 rows.
  90. NOTE: the 10,000 row limit only applies to the number of rows that are retrieved
  91. by the query and displayed in Discover. Any query or aggregation runs on the
  92. full data set.
  93. Each row shows two columns for the example query: a column with the `@timestamp`
  94. field and a column with the full document. To display specific fields from the
  95. documents, use the <<esql-keep>> command:
  96. [source,esql]
  97. ----
  98. FROM kibana_sample_data_logs
  99. | KEEP @timestamp, bytes, geo.dest
  100. ----
  101. To display all fields as separate columns, use `KEEP *`:
  102. [source,esql]
  103. ----
  104. FROM kibana_sample_data_logs
  105. | KEEP *
  106. ----
  107. NOTE: The maximum number of columns in Discover is 50. If a query returns more
  108. than 50 columns, Discover only shows the first 50.
  109. [discrete]
  110. ==== Sorting
  111. To sort on one of the columns, click the column name you want to sort on and
  112. select the sort order. Note that this performs client-side sorting. It only
  113. sorts the rows that were retrieved by the query, which may not be the full
  114. dataset because of the (implicit) limit. To sort the full data set, use the
  115. <<esql-sort>> command:
  116. [source,esql]
  117. ----
  118. FROM kibana_sample_data_logs
  119. | KEEP @timestamp, bytes, geo.dest
  120. | SORT bytes DESC
  121. ----
  122. [discrete]
  123. [[esql-kibana-time-filter]]
  124. === Time filtering
  125. To display data within a specified time range, you can use the standard time filter,
  126. custom time parameters, or a WHERE command.
  127. [discrete]
  128. ==== Standard time filter
  129. The standard {kibana-ref}/set-time-filter.html[time filter] is enabled
  130. when the indices you're querying have a field named `@timestamp`.
  131. [discrete]
  132. ==== Custom time parameters
  133. If your indices do not have a field named `@timestamp`, you can use
  134. the `?_tstart` and `?_tend` parameters to specify a time range. These parameters
  135. work with any timestamp field and automatically sync with the {kibana-ref}/set-time-filter.html[time filter].
  136. [source,esql]
  137. ----
  138. FROM my_index
  139. | WHERE custom_timestamp >= ?_tstart AND custom_timestamp < ?_tend
  140. ----
  141. You can also use the `?_tstart` and `?_tend` parameters with the <<esql-bucket>> function
  142. to create auto-incrementing time buckets in {esql} <<esql-kibana-visualizations,visualizations>>.
  143. For example:
  144. [source,esql]
  145. ----
  146. FROM kibana_sample_data_logs
  147. | STATS average_bytes = AVG(bytes) BY BUCKET(@timestamp, 50, ?_tstart, ?_tend)
  148. ----
  149. This example uses `50` buckets, which is the maximum number of buckets.
  150. [discrete]
  151. ==== WHERE command
  152. You can also limit the time range using the <<esql-where>> command and the <<esql-now>> function.
  153. For example, if the timestamp field is called `timestamp`, to query the last 15
  154. minutes of data:
  155. [source,esql]
  156. ----
  157. FROM kibana_sample_data_logs
  158. | WHERE timestamp > NOW() - 15minutes
  159. ----
  160. [discrete]
  161. [[esql-kibana-visualizations]]
  162. === Analyze and visualize data
  163. Between the query bar and the results table, Discover shows a date histogram
  164. visualization. By default, if the indices you're querying do not contain a `@timestamp`
  165. field, the histogram is not shown. But you can use a custom time field with the `?_tstart`
  166. and `?_tend` parameters to enable it.
  167. The visualization adapts to the query. A query's nature determines the type of
  168. visualization. For example, this query aggregates the total number of bytes per
  169. destination country:
  170. [source,esql]
  171. ----
  172. FROM kibana_sample_data_logs
  173. | STATS total_bytes = SUM(bytes) BY geo.dest
  174. | SORT total_bytes DESC
  175. | LIMIT 3
  176. ----
  177. The resulting visualization is a bar chart showing the top 3 countries:
  178. image::images/esql/esql-kibana-bar-chart.png[align="center"]
  179. To make changes to the visualization, like changing the visualization type, axes and colors, click the
  180. pencil button (image:images/esql/esql-icon-edit-visualization.svg[]). This opens
  181. an in-line editor:
  182. image::images/esql/esql-kibana-in-line-editor.png[align="center",width=66%]
  183. You can save the visualization to a new or existing dashboard by clicking the
  184. save button (image:images/esql/esql-icon-save-visualization.svg[]). Once saved
  185. to a dashboard, you'll be taken to the Dashboards page. You can continue to
  186. make changes to the visualization. Click the
  187. options button in the top-right (image:images/esql/esql-icon-options.svg[]) and
  188. select *Edit ES|QL visualization* to open the in-line editor:
  189. image::images/esql/esql-kibana-edit-on-dashboard.png[align="center",width=66%]
  190. [discrete]
  191. [[esql-kibana-dashboard-panel]]
  192. ==== Add a panel to a dashboard
  193. You can use {esql} queries to create panels on your dashboards.
  194. To add a panel to a dashboard, under *Dashboards*, click the *Add panel* button and select {esql}.
  195. image::images/esql/esql-dashboard-panel.png[align="center",width=50%]
  196. Check the {esql} query by clicking the Panel filters button (image:images/esql/dashboard_panel_filter_button.png[Panel filters button on panel header]):
  197. image::images/esql/esql-dashboard-panel-query.png[align="center",width=50%]
  198. You can also edit the {esql} visualization from here.
  199. Click the options button in the top-right (image:images/esql/esql-icon-options.svg[]) and
  200. select *Edit ESQL visualization* to open the in-line editor.
  201. image::images/esql/esql-dashboard-panel-edit-visualization.png[align="center",width=50%]
  202. [discrete]
  203. [[esql-kibana-enrich]]
  204. === Create an enrich policy
  205. The {esql} <<esql-enrich>> command enables you to <<esql-enrich-data,enrich>>
  206. your query dataset with fields from another dataset. Before you can use
  207. `ENRICH`, you need to <<esql-set-up-enrich-policy,create and execute an enrich
  208. policy>>. If a policy exists, it will be suggested by auto-complete. If not,
  209. click *Click to create* to create one.
  210. image::images/esql/esql-kibana-enrich-autocomplete.png[align="center"]
  211. Next, you can enter a policy name, the policy type, source indices, and
  212. optionally a query:
  213. image::images/esql/esql-kibana-enrich-step-1.png[align="center",width="50%"]
  214. Click *Next* to select the match field and enrich fields:
  215. image::images/esql/esql-kibana-enrich-step-2.png[align="center",width="50%"]
  216. Finally, click *Create and execute*.
  217. Now, you can use the enrich policy in an {esql} query:
  218. [source,esql]
  219. ----
  220. FROM kibana_sample_data_logs
  221. | STATS total_bytes = SUM(bytes) BY geo.dest
  222. | SORT total_bytes DESC
  223. | LIMIT 3
  224. | ENRICH countries
  225. ----
  226. [discrete]
  227. [[esql-kibana-alerting-rule]]
  228. === Create an alerting rule
  229. You can use {esql} queries to create alerts. From Discover, click *Alerts* and
  230. select *Create search threshold rule*. This opens a panel that enables you to
  231. create a rule using an {esql} query. Next, you can test the query, add a
  232. connector, and save the rule.
  233. image::images/esql/esql-kibana-create-rule.png[align="center",width=50%]
  234. [discrete]
  235. [[esql-kibana-limitations]]
  236. === Limitations
  237. // tag::limitations[]
  238. * The user interface to filter data is not enabled when Discover is in {esql}
  239. mode. To filter data, write a query that uses the <<esql-where>> command
  240. instead.
  241. * Discover shows no more than 10,000 rows. This limit only applies to the number
  242. of rows that are retrieved by the query and displayed in Discover. Queries and
  243. aggregations run on the full data set.
  244. * Discover shows no more than 50 columns. If a query returns
  245. more than 50 columns, Discover only shows the first 50.
  246. * CSV export from Discover shows no more than 10,000 rows. This limit only applies to the number
  247. of rows that are retrieved by the query and displayed in Discover. Queries and
  248. aggregations run on the full data set.
  249. * Querying many indices at once without any filters can cause an error in
  250. kibana which looks like `[esql] > Unexpected error from Elasticsearch: The
  251. content length (536885793) is bigger than the maximum allowed string
  252. (536870888)`. The response from {esql} is too long. Use <<esql-drop>> or
  253. <<esql-keep>> to limit the number of fields returned.
  254. // end::limitations[]