esql-kibana.asciidoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. Discover suggests possible commands and functions to autocomplete your query:
  46. image::images/esql/esql-kibana-auto-complete.png[align="center"]
  47. // end::autocomplete[]
  48. [NOTE]
  49. ====
  50. {esql} keywords are case-insensitive. The following query is identical to the
  51. previous one:
  52. [source,esql]
  53. ----
  54. FROM kibana_sample_data_logs | LIMIT 10
  55. ----
  56. ====
  57. [discrete]
  58. ==== Make your query readable
  59. For readability, you can put each processing command on a new line. The
  60. following query is identical to the previous one:
  61. [source,esql]
  62. ----
  63. FROM kibana_sample_data_logs
  64. | LIMIT 10
  65. ----
  66. You can do that using the **Add line breaks on pipes** button from the query editor's footer.
  67. image::https://images.contentstack.io/v3/assets/bltefdd0b53724fa2ce/bltd5554518309e10f6/672d153cfeb8f9d479ebcc6e/esql-line-breakdown.gif[Automatic line breaks for ES|QL queries]
  68. // tag::compact[]
  69. You can adjust the editor's height by dragging its bottom border to your liking.
  70. // end::compact[]
  71. [discrete]
  72. ==== Warnings
  73. A query may result in warnings, for example when querying an unsupported field
  74. type. When that happens, a warning symbol is shown in the query bar. To see the
  75. detailed warning, expand the query bar, and click *warnings*.
  76. [discrete]
  77. [[esql-kibana-query-history]]
  78. ==== Query history
  79. You can reuse your recent {esql} queries in the query bar.
  80. In the query bar, click *Show recent queries*.
  81. You can then scroll through your recent queries:
  82. image::images/esql/esql-discover-query-history.png[align="center",size="50%"]
  83. [discrete]
  84. [[esql-kibana-starred-queries]]
  85. ==== Starred queries
  86. From the query history, you can mark some queries as favorite to find and access them faster later.
  87. In the query bar, click *Show recent queries*.
  88. From the **Recent** tab, you can star any queries you want.
  89. In the **Starred** tab, find all the queries you have previously starred.
  90. image::images/esql/esql-discover-query-starred.png[align="center",size="50%"]
  91. [discrete]
  92. [[esql-kibana-results-table]]
  93. === The results table
  94. For the example query, the results table shows 10 rows. Omitting the `LIMIT`
  95. command, the results table defaults to up to 1000 rows. Using `LIMIT`, you can
  96. increase the limit to up to 10,000 rows.
  97. NOTE: the 10,000 row limit only applies to the number of rows that are retrieved
  98. by the query and displayed in Discover. Any query or aggregation runs on the
  99. full data set.
  100. Each row shows two columns for the example query: a column with the `@timestamp`
  101. field and a column with the full document. To display specific fields from the
  102. documents, use the <<esql-keep>> command:
  103. [source,esql]
  104. ----
  105. FROM kibana_sample_data_logs
  106. | KEEP @timestamp, bytes, geo.dest
  107. ----
  108. To display all fields as separate columns, use `KEEP *`:
  109. [source,esql]
  110. ----
  111. FROM kibana_sample_data_logs
  112. | KEEP *
  113. ----
  114. NOTE: The maximum number of columns in Discover is 50. If a query returns more
  115. than 50 columns, Discover only shows the first 50.
  116. [discrete]
  117. ==== Sorting
  118. To sort on one of the columns, click the column name you want to sort on and
  119. select the sort order. Note that this performs client-side sorting. It only
  120. sorts the rows that were retrieved by the query, which may not be the full
  121. dataset because of the (implicit) limit. To sort the full data set, use the
  122. <<esql-sort>> command:
  123. [source,esql]
  124. ----
  125. FROM kibana_sample_data_logs
  126. | KEEP @timestamp, bytes, geo.dest
  127. | SORT bytes DESC
  128. ----
  129. [discrete]
  130. [[esql-kibana-time-filter]]
  131. === Time filtering
  132. To display data within a specified time range, you can use the standard time filter,
  133. custom time parameters, or a WHERE command.
  134. [discrete]
  135. ==== Standard time filter
  136. The standard {kibana-ref}/set-time-filter.html[time filter] is enabled
  137. when the indices you're querying have a field named `@timestamp`.
  138. [discrete]
  139. ==== Custom time parameters
  140. If your indices do not have a field named `@timestamp`, you can use
  141. the `?_tstart` and `?_tend` parameters to specify a time range. These parameters
  142. work with any timestamp field and automatically sync with the {kibana-ref}/set-time-filter.html[time filter].
  143. [source,esql]
  144. ----
  145. FROM my_index
  146. | WHERE custom_timestamp >= ?_tstart AND custom_timestamp < ?_tend
  147. ----
  148. You can also use the `?_tstart` and `?_tend` parameters with the <<esql-bucket>> function
  149. to create auto-incrementing time buckets in {esql} <<esql-kibana-visualizations,visualizations>>.
  150. For example:
  151. [source,esql]
  152. ----
  153. FROM kibana_sample_data_logs
  154. | STATS average_bytes = AVG(bytes) BY BUCKET(@timestamp, 50, ?_tstart, ?_tend)
  155. ----
  156. This example uses `50` buckets, which is the maximum number of buckets.
  157. [discrete]
  158. ==== WHERE command
  159. You can also limit the time range using the <<esql-where>> command and the <<esql-now>> function.
  160. For example, if the timestamp field is called `timestamp`, to query the last 15
  161. minutes of data:
  162. [source,esql]
  163. ----
  164. FROM kibana_sample_data_logs
  165. | WHERE timestamp > NOW() - 15minutes
  166. ----
  167. ==== Keyboard shortcuts
  168. The ES|QL editor supports several shortcuts to help you write and run your queries faster:
  169. |===
  170. | Mac | Windows/Linux | Description
  171. | `Cmd + Enter`
  172. | `Ctrl + Enter`
  173. | Run a query
  174. | `Cmd + /`
  175. | `Ctrl + /`
  176. | Comment or uncomment a line
  177. |===
  178. TIP: You can find the list of shortcuts directly from the editor. Look for the image:images/keyboard.svg[title=keyboard,width="2%"] icon.
  179. [float]
  180. === LOOKUP JOINs
  181. The ES|QL editor supports {ref}/esql-commands.html#esql-lookup-join[`LOOKUP JOIN`] commands and suggests lookup mode indices and join condition fields.
  182. image::https://images.contentstack.io/v3/assets/bltefdd0b53724fa2ce/blte43a30a93241d650/67c23670045f5839e5bfd1e4/lookup-join-demo.gif[Using the LOOKUP JOIN command to autocomplete an ES|QL query]
  183. [discrete]
  184. [[esql-kibana-visualizations]]
  185. === Analyze and visualize data
  186. Between the query bar and the results table, Discover shows a date histogram
  187. visualization. By default, if the indices you're querying do not contain a `@timestamp`
  188. field, the histogram is not shown. But you can use a custom time field with the `?_tstart`
  189. and `?_tend` parameters to enable it.
  190. The visualization adapts to the query. A query's nature determines the type of
  191. visualization. For example, this query aggregates the total number of bytes per
  192. destination country:
  193. [source,esql]
  194. ----
  195. FROM kibana_sample_data_logs
  196. | STATS total_bytes = SUM(bytes) BY geo.dest
  197. | SORT total_bytes DESC
  198. | LIMIT 3
  199. ----
  200. The resulting visualization is a bar chart showing the top 3 countries:
  201. image::images/esql/esql-kibana-bar-chart.png[align="center"]
  202. To make changes to the visualization, like changing the visualization type, axes and colors, click the
  203. pencil button (image:images/esql/esql-icon-edit-visualization.svg[]). This opens
  204. an in-line editor:
  205. image::images/esql/esql-kibana-in-line-editor.png[align="center",width=66%]
  206. You can save the visualization to a new or existing dashboard by clicking the
  207. save button (image:images/esql/esql-icon-save-visualization.svg[]). Once saved
  208. to a dashboard, you'll be taken to the Dashboards page. You can continue to
  209. make changes to the visualization. Click the
  210. options button in the top-right (image:images/esql/esql-icon-options.svg[]) and
  211. select *Edit ES|QL visualization* to open the in-line editor:
  212. image::images/esql/esql-kibana-edit-on-dashboard.png[align="center",width=66%]
  213. [discrete]
  214. [[esql-kibana-dashboard-panel]]
  215. ==== Add a panel to a dashboard
  216. You can use {esql} queries to create panels on your dashboards.
  217. To add a panel to a dashboard, under *Dashboards*, click the *Add panel* button and select {esql}.
  218. image::images/esql/esql-dashboard-panel.png[align="center",width=50%]
  219. Check the {esql} query by clicking the Panel filters button (image:images/esql/dashboard_panel_filter_button.png[Panel filters button on panel header]):
  220. image::images/esql/esql-dashboard-panel-query.png[align="center",width=50%]
  221. You can also edit the {esql} visualization from here.
  222. Click the options button in the top-right (image:images/esql/esql-icon-options.svg[]) and
  223. select *Edit ESQL visualization* to open the in-line editor.
  224. image::images/esql/esql-dashboard-panel-edit-visualization.png[align="center",width=50%]
  225. [discrete]
  226. [[esql-kibana-enrich]]
  227. === Create an enrich policy
  228. The {esql} <<esql-enrich>> command enables you to <<esql-enrich-data,enrich>>
  229. your query dataset with fields from another dataset. Before you can use
  230. `ENRICH`, you need to <<esql-set-up-enrich-policy,create and execute an enrich
  231. policy>>. If a policy exists, it will be suggested by auto-complete. If not,
  232. click *Click to create* to create one.
  233. image::images/esql/esql-kibana-enrich-autocomplete.png[align="center"]
  234. Next, you can enter a policy name, the policy type, source indices, and
  235. optionally a query:
  236. image::images/esql/esql-kibana-enrich-step-1.png[align="center",width="50%"]
  237. Click *Next* to select the match field and enrich fields:
  238. image::images/esql/esql-kibana-enrich-step-2.png[align="center",width="50%"]
  239. Finally, click *Create and execute*.
  240. Now, you can use the enrich policy in an {esql} query:
  241. [source,esql]
  242. ----
  243. FROM kibana_sample_data_logs
  244. | STATS total_bytes = SUM(bytes) BY geo.dest
  245. | SORT total_bytes DESC
  246. | LIMIT 3
  247. | ENRICH countries
  248. ----
  249. [discrete]
  250. [[esql-kibana-alerting-rule]]
  251. === Create an alerting rule
  252. You can use {esql} queries to create alerts. From Discover, click *Alerts* and
  253. select *Create search threshold rule*. This opens a panel that enables you to
  254. create a rule using an {esql} query. Next, you can test the query, add a
  255. connector, and save the rule.
  256. image::images/esql/esql-kibana-create-rule.png[align="center",width=50%]
  257. [discrete]
  258. [[esql-kibana-limitations]]
  259. === Limitations
  260. // tag::limitations[]
  261. * The user interface to filter data is not enabled when Discover is in {esql}
  262. mode. To filter data, write a query that uses the <<esql-where>> command
  263. instead.
  264. * Discover shows no more than 10,000 rows. This limit only applies to the number
  265. of rows that are retrieved by the query and displayed in Discover. Queries and
  266. aggregations run on the full data set.
  267. * Discover shows no more than 50 columns. If a query returns
  268. more than 50 columns, Discover only shows the first 50.
  269. * CSV export from Discover shows no more than 10,000 rows. This limit only applies to the number
  270. of rows that are retrieved by the query and displayed in Discover. Queries and
  271. aggregations run on the full data set.
  272. * Querying many indices at once without any filters can cause an error in
  273. kibana which looks like `[esql] > Unexpected error from Elasticsearch: The
  274. content length (536885793) is bigger than the maximum allowed string
  275. (536870888)`. The response from {esql} is too long. Use <<esql-drop>> or
  276. <<esql-keep>> to limit the number of fields returned.
  277. // end::limitations[]