|
| 1 | +--- |
| 2 | +navigation_title: "KQL" |
| 3 | +mapped_pages: |
| 4 | + - https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-kql-query.html |
| 5 | +applies_to: |
| 6 | + stack: preview 9.0, ga 9.1 |
| 7 | + serverless: all |
| 8 | +--- |
| 9 | + |
| 10 | +# KQL query [query-dsl-kql-query] |
| 11 | + |
| 12 | +Returns documents matching a provided KQL expression. The value of the `query` parameter is parsed using the |
| 13 | +[Kibana Query Language (KQL)](/reference/query-languages/kql.md) and rewritten |
| 14 | +into standard Query DSL. |
| 15 | + |
| 16 | +Use this query when you want to accept KQL input (for example from a Kibana search bar) directly in Elasticsearch. |
| 17 | + |
| 18 | +## Example request [kql-query-example] |
| 19 | + |
| 20 | +The following example returns documents where `service.name` is `"checkout-service"` and `http.response.status_code` is `200`. |
| 21 | + |
| 22 | +```console |
| 23 | +GET /_search |
| 24 | +{ |
| 25 | + "query": { |
| 26 | + "kql": { |
| 27 | + "query": "service.name: \"checkout-service\" AND http.response.status_code: 200" |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +## Top-level parameters for `kql` [kql-top-level-params] |
| 34 | + |
| 35 | +`query` |
| 36 | +: (Required, string) The KQL expression to parse. See the |
| 37 | +[KQL language reference](/reference/query-languages/kql.md) for supported |
| 38 | +syntax. |
| 39 | + |
| 40 | +`case_insensitive` |
| 41 | +: (Optional, boolean) If `true`, performs case-insensitive matching for field names and keyword / text terms. |
| 42 | + Defaults to `false`. |
| 43 | + |
| 44 | +`default_field` |
| 45 | +: (Optional, string) Default field (or field pattern with wildcards) to target when a bare term in the query |
| 46 | + string does not specify a field. Supports wildcards (`*`). |
| 47 | + |
| 48 | + Defaults to the [`index.query.default_field`](/reference/elasticsearch/index-settings/index-modules.md#index-query-default-field) |
| 49 | + index setting (default value is `*`). The value `*` expands to all fields that are eligible for term queries |
| 50 | + (metadata fields excluded). Searching *all* eligible fields can be expensive on mappings with many fields and |
| 51 | + is subject to the `indices.query.bool.max_clause_count` limit. |
| 52 | + |
| 53 | + Like other queries, this implicit expansion does not traverse [nested](/reference/elasticsearch/mapping-reference/nested.md) |
| 54 | + documents. |
| 55 | + |
| 56 | +`time_zone` |
| 57 | +: (Optional, string) [UTC offset](https://en.wikipedia.org/wiki/List_of_UTC_time_offsets) or |
| 58 | + [IANA time zone](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) used to interpret date literals |
| 59 | + (for example `@timestamp > now-2d`). Does not change the value of `now` (which is always system UTC) but affects |
| 60 | + rounding and the interpretation of explicit date strings. |
| 61 | + |
| 62 | +`boost` |
| 63 | +: (Optional, float) Standard query *boost*. Defaults to `1.0`. |
| 64 | + |
| 65 | +`_name` |
| 66 | +: (Optional, string) A name to identify this query in the response's `matched_queries`. |
| 67 | + |
| 68 | +## Syntax reference |
| 69 | + |
| 70 | +The `kql` query accepts a single KQL expression string in the `query` parameter. All language elements (field matching, |
| 71 | +wildcards, boolean logic, ranges, nested scoping) are defined in the |
| 72 | +[KQL language reference](/reference/query-languages/kql.md). This page does not |
| 73 | +duplicate that syntax. |
| 74 | + |
| 75 | +## Additional examples [kql-examples] |
| 76 | + |
| 77 | +### Case-insensitive matching (log level) |
| 78 | + |
| 79 | +```console |
| 80 | +GET /_search |
| 81 | +{ |
| 82 | + "query": { |
| 83 | + "kql": { |
| 84 | + "query": "log.level: ErRoR", |
| 85 | + "case_insensitive": true |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +### Using a default field pattern |
| 92 | + |
| 93 | +Search any field under the `logs` object for the term `timeout`: |
| 94 | + |
| 95 | +```console |
| 96 | +GET /_search |
| 97 | +{ |
| 98 | + "query": { |
| 99 | + "kql": { |
| 100 | + "query": "timeout", |
| 101 | + "default_field": "logs.*" |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +### Date range with time zone |
| 108 | + |
| 109 | +```console |
| 110 | +GET /_search |
| 111 | +{ |
| 112 | + "query": { |
| 113 | + "kql": { |
| 114 | + "query": "@timestamp >= ""2025-10-01"" AND @timestamp < ""2025-10-02""", |
| 115 | + "time_zone": "Europe/Paris" |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +### Nested field query |
| 122 | +```console |
| 123 | +GET /_search |
| 124 | +{ |
| 125 | + "query": { |
| 126 | + "kql": { |
| 127 | + "query": "events.stack:{ file: \"app.js\" AND line: 42 }" |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
| 132 | + |
| 133 | +## When to use `kql` vs `query_string` |
| 134 | + |
| 135 | +Use `kql` for user-facing search boxes where you want a concise, filter-oriented syntax and to avoid Lucene’s |
| 136 | +advanced operators (fuzzy, regexp, proximity, inline boosting). Use [`query_string`](./query-dsl-query-string-query.md) |
| 137 | +or [`simple_query_string`](./query-dsl-simple-query-string-query.md) when those advanced features are required. |
| 138 | + |
| 139 | +## Notes and limitations |
| 140 | + |
| 141 | +* The parsed KQL expression is rewritten into standard Query DSL and participates in scoring unless wrapped in a |
| 142 | + filter context (for example inside a `bool.filter`). Adjust relevance with `boost` if needed. |
| 143 | +* Large wildcard expansions (in field names or terms) can hit the `indices.query.bool.max_clause_count` safeguard. |
| 144 | +* Nested documents require the KQL nested syntax (`path:{ ... }`); terms are not correlated across separate nested |
| 145 | + objects automatically. |
| 146 | +* Unsupported syntax (such as fuzzy operators) results in a parse error. |
| 147 | + |
| 148 | +## See also [kql-see-also] |
| 149 | + |
| 150 | +* [KQL language reference](/reference/query-languages/kql.md) |
| 151 | +* [Default field setting](/reference/elasticsearch/index-settings/index-modules.md#index-query-default-field) |
0 commit comments