and, or, not
10 min · 2 ejercicio(s)
Real questions have more than one condition. KQL combines them with the boolean operators and, or and not. The operators are case insensitive: and and AND are the same.
event.outcome: failure and service.name: ssh
user.name: root or user.name: admin
not host.name: srv-web-01Precedence
and binds tighter than or. This query
service.name: ssh or service.name: rdp and event.outcome: failureis read as ssh logons (any outcome) or failed rdp logons. If you mean "failed logons via ssh or rdp", use parentheses:
(service.name: ssh or service.name: rdp) and event.outcome: failureAnalyst habit: When you mix
andandor, always add parentheses. Your future self (and your team) will thank you.
Negation
not excludes events. It is how you remove known noise:
event.outcome: failure and not source.ip: 185.220.101.47🔎 Profundiza: funciones, errores habituales y práctica
How Kibana translates boolean logic
| KQL | Query DSL (simplified) |
|---|---|
a and b |
bool.filter: [a, b] |
a or b |
bool.should: [a, b], minimum_should_match: 1 |
not a |
bool.must_not: [a] |
You can see the same translation in the lab: Querynaut compiles your KQL exactly like this before sending it to Elasticsearch. Because and becomes a filter, KQL results have no relevance score. That's why Kibana sorts by time instead.
Negation and missing fields
not service.name: ssh also returns documents that don't have service.name at all. If you only want documents that have the field with a different value, combine it with an existence check:
service.name: * and not service.name: sshReadable queries
- One condition per line is allowed. KQL ignores line breaks, so you can format long queries in a saved search.
- Prefer
and notfor exclusions of known noise and document why the exclusion exists (e.g. "vulnerability scanner 10.0.5.9"). - Exclusions are a security risk too: an attacker who knows your filters can hide behind them. Exclude as narrowly as possible (
source.ipanduser.name, not just one of them).