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