Type to search.

ES|QL WHERE command

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

The WHERE processing command produces a table that contains all the rows from the input table for which the provided condition evaluates to true.

Tip

In case of value exclusions, fields with null values will be excluded from search results. In this context a null means either there is an explicit null value in the document or there is no value at all. For example: WHERE field != "value" will be interpreted as WHERE field != "value" AND field IS NOT NULL.

For more information, refer to Working with NULL values in ES|QL.

ES|QL
WHERE expression
expression
A boolean expression.

The following examples show common ways to filter rows with WHERE.

ES|QL
FROM employees
| KEEP first_name, last_name, still_hired
| WHERE still_hired == true

Which, if still_hired is a boolean field, can be simplified to:

ES|QL
FROM employees
| KEEP first_name, last_name, still_hired
| WHERE still_hired

Use date math to retrieve data from a specific time range. For example, to retrieve the last hour of logs:

ES|QL
FROM sample_data
| WHERE @timestamp > NOW() - 1 hour

WHERE supports various functions. For example the LENGTH function:

ES|QL
FROM employees
| KEEP first_name, last_name, height
| WHERE LENGTH(first_name) < 4

For a complete list of all functions, refer to Functions overview.

For NULL comparison, use the IS NULL and IS NOT NULL predicates. To learn how NULL affects WHERE, comparisons, and boolean logic, refer to Working with NULL values in ES|QL.

Example

ES|QL
FROM employees
| WHERE birth_date IS NULL
first_name:keyword last_name:keyword
Basil Tramer
Florian Syrotiuk
Lucien Rosenbaum

Example

ES|QL
FROM employees
| WHERE is_rehired IS NOT NULL
| STATS COUNT(emp_no)
COUNT(emp_no):long
84

Use MATCH to perform a match query on a specified field. MATCH works on text fields as well as boolean, date, and numeric types.

ES|QL
FROM books
| WHERE MATCH(author, "Faulkner")
book_no:keyword author:text
2378 [Carol Faulkner, Holly Byers Ochoa, Lucretia Mott]
2713 William Faulkner
2847 Colleen Faulkner
2883 William Faulkner
3293 Danny Faulkner
ES|QL
FROM books
| WHERE MATCH(title, "Hobbit Back Again", {"operator": "AND"})
| KEEP title;
title:text
The Hobbit or There and Back Again
Tip

You can also use the shorthand match operator : instead of MATCH.

Use LIKE to filter data based on string patterns using wildcards. LIKE usually acts on a field placed on the left-hand side of the operator, but it can also act on a constant (literal) expression. The right-hand side of the operator represents the pattern.

The following wildcard characters are supported:

  • * matches zero or more characters.
  • ? matches one character.

Supported types

str pattern result
keyword keyword boolean
text keyword boolean

Example

ES|QL
FROM employees
| WHERE first_name LIKE """?b*"""
| KEEP first_name, last_name
first_name:keyword last_name:keyword
Ebbe Callaway
Eberhardt Terkki

When used on text fields, LIKE treats the field as a keyword and does not use the analyzer. This means the pattern matching is case-sensitive and must match the exact string indexed. To perform full-text search, use the MATCH or QSTR functions.

Matching the exact characters * and ? will require escaping. The escape character is backslash \. Since backslash is also special in string literals, it will require further escaping. To match a literal backslash in the data, write \\ in the pattern (escape plus backslash). Windows-style paths such as C:\Windows\System32 are much easier with triple-quoted string literals (three consecutive " characters as delimiters): the value can contain single backslashes as in C:\Windows\..., and a pattern that matches C:\Windows followed by anything uses \\ before Windows inside that literal (see the example below). When you send ES|QL inside a JSON request body (for example with cURL), JSON string escaping adds another layer: each backslash in the ES|QL text is often written as \\ in JSON, so a single literal backslash in the pattern can require many backslashes by the time it reaches the parser.

ES|QL
ROW message = "foo * bar"
| WHERE message LIKE "foo \\* bar"

To reduce the overhead of escaping, we suggest using triple quotes strings """

ES|QL
ROW message = "foo * bar"
| WHERE message LIKE """foo \* bar"""
ES|QL
ROW path = """C:\Windows\System32\foo.txt"""
| WHERE path LIKE """C:\\Windows*"""

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack 9.1+

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available since 9.1

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

Both a single pattern or a list of patterns are supported. If a list of patterns is provided, the expression will return true if any of the patterns match.

ES|QL
ROW message = "foobar"
| WHERE message like ("foo*", "bar?")

Patterns may be specified with REST query placeholders as well

ES|QL
FROM employees
| WHERE first_name LIKE ?pattern
| KEEP first_name, last_name

Stack 9.3+

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available since 9.3

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

Stack Planned

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Planned

    We plan to add this functionality in a future Elastic Stack update. Subject to changes.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

The pattern can also be any constant expression, such as a call to CONCAT or TO_LOWER.

ES|QL
from employees | where first_name like concat("Eber", "*") | keep emp_no, first_name

Use RLIKE to filter data based on string patterns using using regular expressions. RLIKE usually acts on a field placed on the left-hand side of the operator, but it can also act on a constant (literal) expression. The right-hand side of the operator represents the pattern.

Supported types

str pattern result
keyword keyword boolean
text keyword boolean

Example

ES|QL
FROM employees
| WHERE first_name RLIKE """.leja.*"""
| KEEP first_name, last_name
first_name:keyword last_name:keyword
Alejandro McAlpine

When used on text fields, RLIKE treats the field as a keyword and does not use the analyzer. This means the pattern matching is case-sensitive and must match the exact string indexed. To perform full-text search, use the MATCH or QSTR functions.

Matching special characters (eg. ., *, (...) will require escaping. The escape character is backslash \. Since also backslash is a special character in string literals, it will require further escaping.

ES|QL
ROW message = "foo ( bar"
| WHERE message RLIKE "foo \\( bar"

To reduce the overhead of escaping, we suggest using triple quotes strings """

ES|QL
ROW message = "foo ( bar"
| WHERE message RLIKE """foo \( bar"""

Serverless

Elastic Cloud Serverless projects are autoscaled environments, fully managed by Elastic and available on Elastic Cloud.

Availability

  • Generally available

    This functionality is generally available and ready for production usage.

Serverless interfaces and procedures might differ from classic Elastic Stack deployments.

Stack 9.2+

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available since 9.2

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

Both a single pattern or a list of patterns are supported. If a list of patterns is provided, the expression will return true if any of the patterns match.

ES|QL
ROW message = "foobar"
| WHERE message RLIKE ("foo.*", "bar.")

Patterns may be specified with REST query placeholders as well

ES|QL
FROM employees
| WHERE first_name RLIKE ?pattern
| KEEP first_name, last_name

Stack 9.3+

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Generally available since 9.3

    This functionality is generally available and ready for production usage.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

Stack Planned

The Elastic Stack includes Elastic's core products such as Elasticsearch, Kibana, Logstash, and Beats.

Availability

  • Planned

    We plan to add this functionality in a future Elastic Stack update. Subject to changes.

Unless stated otherwise on the page, this functionality is available when your Elastic Stack is deployed on Elastic Cloud Hosted, Elastic Cloud Enterprise, Elastic Cloud on Kubernetes, and self-managed environments.

This documentation corresponds to the latest patch available for each minor version. If you're not using the latest patch, check the release notes for changes.

The pattern can also be any constant expression, such as a call to CONCAT or TO_UPPER.

ES|QL
from employees | where first_name rlike concat("Eber", ".*") | keep emp_no, first_name

The IN operator allows testing whether a field or expression equals an element in a list of literals, fields or expressions:

Example

ES|QL
ROW a = 1, b = 4, c = 3
| WHERE c-a IN (3, b / 2, a)
a:integer b:integer c:integer
1 4 3

You can also use a subquery on the right-hand side of IN or NOT IN to filter rows against the results of another query, including a multi-column (a, b) IN (...) tuple. To learn more, refer to Use subqueries with IN and NOT IN.

For a complete list of all operators, refer to Operators.