Chapter 95: Logging with Loki, Elastic Stack and CloudWatch
95.10 Kibana KQL versus Elasticsearch Query DSL
Story (fictional teaching scenario). At a fictional Mysuru bakery, Emma from Bristol pastes a JSON query into the Kibana KQL bar. It fails because the interfaces expect different syntax.
Why
Query languages can ask similar questions without being interchangeable.
What
- KQL here means Kibana Query Language, not Microsoft Kusto Query Language.
- Query DSL is Elasticsearch’s JSON-based query language.
- A bool query combines clauses; its filter clauses require matching without relevance scoring.
- A term query matches an exact indexed term; a range query checks a bound.
- Scoring ranks relevance; operational exact filters often do not need it.
How
-
Identify the query interface before pasting syntax.
-
Use field types from the mapping.
-
State a narrow time range in the interface or query.
-
Do not treat KQL as an aggregation pipeline; use the appropriate analytics features.
Example 1: Kibana KQL filter
- Read the command or request.
service: "bakery-api" AND level: "ERROR" AND latency_ms >= 800
- Read the result.
Output (illustrative; NOT executed against Loki, Elastic or CloudWatch)
If the six synthetic events were indexed, two would match.
- Line by line:
serviceandlevelare keyword fields in the example mapping. AND requires every condition.latency_ms >= 800is numeric. Kibana’s time picker also constrains results. This is not Kusto and was not executed in Kibana.
Example 2: Elasticsearch DSL request body
- Read the command or request.
{"query":{"bool":{"filter":[{"term":{"service":"bakery-api"}},{"term":{"level":"ERROR"}},{"range":{"latency_ms":{"gte":800}}}]}}}
- Read the result.
Output (illustrative; NOT executed against Loki, Elastic or CloudWatch)
Illustrative match count for the fixture:2 documents.
- Line by line:
querycontains one bool query.filtercombines the three conditions with logical AND without scoring.termuses exact keyword values.gtemeans greater than or equal. The index/time scope would still need to be selected; no _search request was sent.
Ravindra Bagale's Tip
Always name which KQL you mean.
KQL म्हणताना कोणतं KQL ते स्पष्ट सांगा.
KQL बोलते समय कौन सा KQL है, साफ बताओ.
Did you understand? Let us try it once more.