esql-kibana.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 your previous KQL or Lucene query converted into {esql}. If the query was empty, it 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-starred-queries]]
  86. ==== Starred queries
  87. From the query history, you can mark some queries as favorite to find and access them faster later.
  88. In the query bar, click *Show recent queries*.
  89. From the **Recent** tab, you can star any queries you want.
  90. In the **Starred** tab, find all the queries you have previously starred.
  91. image::images/esql/esql-discover-query-starred.png[align="center",size="50%"]
  92. [discrete]
  93. [[esql-kibana-results-table]]
  94. === The results table
  95. For the example query, the results table shows 10 rows. Omitting the `LIMIT`
  96. command, the results table defaults to up to 1000 rows. Using `LIMIT`, you can
  97. increase the limit to up to 10,000 rows.
  98. NOTE: the 10,000 row limit only applies to the number of rows that are retrieved
  99. by the query and displayed in Discover. Any query or aggregation runs on the
  100. full data set.
  101. Each row shows two columns for the example query: a column with the `@timestamp`
  102. field and a column with the full document. To display specific fields from the
  103. documents, use the <<esql-keep>> command:
  104. [source,esql]
  105. ----
  106. FROM kibana_sample_data_logs
  107. | KEEP @timestamp, bytes, geo.dest
  108. ----
  109. To display all fields as separate columns, use `KEEP *`:
  110. [source,esql]
  111. ----
  112. FROM kibana_sample_data_logs
  113. | KEEP *
  114. ----
  115. NOTE: The maximum number of columns in Discover is 50. If a query returns more
  116. than 50 columns, Discover only shows the first 50.
  117. [discrete]
  118. ==== Sorting
  119. To sort on one of the columns, click the column name you want to sort on and
  120. select the sort order. Note that this performs client-side sorting. It only
  121. sorts the rows that were retrieved by the query, which may not be the full
  122. dataset because of the (implicit) limit. To sort the full data set, use the
  123. <<esql-sort>> command:
  124. [source,esql]
  125. ----
  126. FROM kibana_sample_data_logs
  127. | KEEP @timestamp, bytes, geo.dest
  128. | SORT bytes DESC
  129. ----
  130. [discrete]
  131. [[esql-kibana-time-filter]]
  132. === Time filtering
  133. To display data within a specified time range, you can use the standard time filter,
  134. custom time parameters, or a WHERE command.
  135. [discrete]
  136. ==== Standard time filter
  137. The standard {kibana-ref}/set-time-filter.html[time filter] is enabled
  138. when the indices you're querying have a field named `@timestamp`.
  139. [discrete]
  140. ==== Custom time parameters
  141. If your indices do not have a field named `@timestamp`, you can use
  142. the `?_tstart` and `?_tend` parameters to specify a time range. These parameters
  143. work with any timestamp field and automatically sync with the {kibana-ref}/set-time-filter.html[time filter].
  144. [source,esql]
  145. ----
  146. FROM my_index
  147. | WHERE custom_timestamp >= ?_tstart AND custom_timestamp < ?_tend
  148. ----
  149. You can also use the `?_tstart` and `?_tend` parameters with the <<esql-bucket>> function
  150. to create auto-incrementing time buckets in {esql} <<esql-kibana-visualizations,visualizations>>.
  151. For example:
  152. [source,esql]
  153. ----
  154. FROM kibana_sample_data_logs
  155. | STATS average_bytes = AVG(bytes) BY BUCKET(@timestamp, 50, ?_tstart, ?_tend)
  156. ----
  157. This example uses `50` buckets, which is the maximum number of buckets.
  158. [discrete]
  159. ==== WHERE command
  160. You can also limit the time range using the <<esql-where>> command and the <<esql-now>> function.
  161. For example, if the timestamp field is called `timestamp`, to query the last 15
  162. minutes of data:
  163. [source,esql]
  164. ----
  165. FROM kibana_sample_data_logs
  166. | WHERE timestamp > NOW() - 15minutes
  167. ----
  168. [discrete]
  169. [[esql-kibana-visualizations]]
  170. === Analyze and visualize data
  171. Between the query bar and the results table, Discover shows a date histogram
  172. visualization. By default, if the indices you're querying do not contain a `@timestamp`
  173. field, the histogram is not shown. But you can use a custom time field with the `?_tstart`
  174. and `?_tend` parameters to enable it.
  175. The visualization adapts to the query. A query's nature determines the type of
  176. visualization. For example, this query aggregates the total number of bytes per
  177. destination country:
  178. [source,esql]
  179. ----
  180. FROM kibana_sample_data_logs
  181. | STATS total_bytes = SUM(bytes) BY geo.dest
  182. | SORT total_bytes DESC
  183. | LIMIT 3
  184. ----
  185. The resulting visualization is a bar chart showing the top 3 countries:
  186. image::images/esql/esql-kibana-bar-chart.png[align="center"]
  187. To make changes to the visualization, like changing the visualization type, axes and colors, click the
  188. pencil button (image:images/esql/esql-icon-edit-visualization.svg[]). This opens
  189. an in-line editor:
  190. image::images/esql/esql-kibana-in-line-editor.png[align="center",width=66%]
  191. You can save the visualization to a new or existing dashboard by clicking the
  192. save button (image:images/esql/esql-icon-save-visualization.svg[]). Once saved
  193. to a dashboard, you'll be taken to the Dashboards page. You can continue to
  194. make changes to the visualization. Click the
  195. options button in the top-right (image:images/esql/esql-icon-options.svg[]) and
  196. select *Edit ES|QL visualization* to open the in-line editor:
  197. image::images/esql/esql-kibana-edit-on-dashboard.png[align="center",width=66%]
  198. [discrete]
  199. [[esql-kibana-dashboard-panel]]
  200. ==== Add a panel to a dashboard
  201. You can use {esql} queries to create panels on your dashboards.
  202. To add a panel to a dashboard, under *Dashboards*, click the *Add panel* button and select {esql}.
  203. image::images/esql/esql-dashboard-panel.png[align="center",width=50%]
  204. Check the {esql} query by clicking the Panel filters button (image:images/esql/dashboard_panel_filter_button.png[Panel filters button on panel header]):
  205. image::images/esql/esql-dashboard-panel-query.png[align="center",width=50%]
  206. You can also edit the {esql} visualization from here.
  207. Click the options button in the top-right (image:images/esql/esql-icon-options.svg[]) and
  208. select *Edit ESQL visualization* to open the in-line editor.
  209. image::images/esql/esql-dashboard-panel-edit-visualization.png[align="center",width=50%]
  210. [discrete]
  211. [[esql-kibana-enrich]]
  212. === Create an enrich policy
  213. The {esql} <<esql-enrich>> command enables you to <<esql-enrich-data,enrich>>
  214. your query dataset with fields from another dataset. Before you can use
  215. `ENRICH`, you need to <<esql-set-up-enrich-policy,create and execute an enrich
  216. policy>>. If a policy exists, it will be suggested by auto-complete. If not,
  217. click *Click to create* to create one.
  218. image::images/esql/esql-kibana-enrich-autocomplete.png[align="center"]
  219. Next, you can enter a policy name, the policy type, source indices, and
  220. optionally a query:
  221. image::images/esql/esql-kibana-enrich-step-1.png[align="center",width="50%"]
  222. Click *Next* to select the match field and enrich fields:
  223. image::images/esql/esql-kibana-enrich-step-2.png[align="center",width="50%"]
  224. Finally, click *Create and execute*.
  225. Now, you can use the enrich policy in an {esql} query:
  226. [source,esql]
  227. ----
  228. FROM kibana_sample_data_logs
  229. | STATS total_bytes = SUM(bytes) BY geo.dest
  230. | SORT total_bytes DESC
  231. | LIMIT 3
  232. | ENRICH countries
  233. ----
  234. [discrete]
  235. [[esql-kibana-alerting-rule]]
  236. === Create an alerting rule
  237. You can use {esql} queries to create alerts. From Discover, click *Alerts* and
  238. select *Create search threshold rule*. This opens a panel that enables you to
  239. create a rule using an {esql} query. Next, you can test the query, add a
  240. connector, and save the rule.
  241. image::images/esql/esql-kibana-create-rule.png[align="center",width=50%]
  242. [discrete]
  243. [[esql-kibana-limitations]]
  244. === Limitations
  245. // tag::limitations[]
  246. * The user interface to filter data is not enabled when Discover is in {esql}
  247. mode. To filter data, write a query that uses the <<esql-where>> command
  248. instead.
  249. * Discover shows no more than 10,000 rows. This limit only applies to the number
  250. of rows that are retrieved by the query and displayed in Discover. Queries and
  251. aggregations run on the full data set.
  252. * Discover shows no more than 50 columns. If a query returns
  253. more than 50 columns, Discover only shows the first 50.
  254. * CSV export from Discover shows no more than 10,000 rows. This limit only applies to the number
  255. of rows that are retrieved by the query and displayed in Discover. Queries and
  256. aggregations run on the full data set.
  257. * Querying many indices at once without any filters can cause an error in
  258. kibana which looks like `[esql] > Unexpected error from Elasticsearch: The
  259. content length (536885793) is bigger than the maximum allowed string
  260. (536870888)`. The response from {esql} is too long. Use <<esql-drop>> or
  261. <<esql-keep>> to limit the number of fields returned.
  262. // end::limitations[]