The reverse command reverses the display order of the search results. It returns the same results but in the opposite order.
Note: The
reversecommand processes the entire dataset. If applied directly to millions of records, it consumes significant coordinating node memory resources. Only apply thereversecommand to smaller datasets, typically after aggregation operations.
The reverse command uses an optimized implementation that intelligently reverses existing sort collations instead of using a ROW_NUMBER() approach. The behavior depends on the context:
- Existing sort collation: If a preceding
sortcommand is detected,reverseflips the sort direction of each field (e.g., ASC becomes DESC and vice versa). This leverages database-native sort reversal for significantly better performance. @timestampfield: If no explicit sort exists but the data source has an@timestampfield,reversesorts by@timestampin descending order.- No sort or
@timestamp: If neither an explicit sort nor an@timestampfield is found,reverseis a no-op (ignored).
The optimization also supports backtracking through non-blocking operators like where, eval, and fields to find an upstream sort. However, blocking operators such as stats (aggregation), join, and set operations destroy the collation, so reverse after these operators is a no-op unless a new sort is added after them.
The reverse command has the following syntax:
reverseThe following query reverses the order of all documents in the results:
source=otellogs
| fields severityText, `resource.attributes.service.name`
| head 5
| reverse
The query returns the following results:
fetched rows / total rows = 5/5
+--------------+----------------------------------+
| severityText | resource.attributes.service.name |
|--------------+----------------------------------|
| INFO | frontend |
| INFO | cart |
| WARN | product-catalog |
| ERROR | payment |
| DEBUG | cart |
+--------------+----------------------------------+
The following query reverses results after sorting by severityNumber in ascending order, effectively implementing descending order:
source=otellogs
| sort severityNumber
| fields severityText, severityNumber
| head 5
| reverse
The query returns the following results:
fetched rows / total rows = 5/5
+--------------+----------------+
| severityText | severityNumber |
|--------------+----------------|
| INFO | 9 |
| INFO | 9 |
| DEBUG | 5 |
| DEBUG | 5 |
| DEBUG | 5 |
+--------------+----------------+
The following query uses the reverse command together with the head command to retrieve the last two records from the original result order:
source=otellogs
| reverse
| head 2
| fields severityText, `resource.attributes.service.name`
The query returns the following results:
fetched rows / total rows = 2/2
+--------------+----------------------------------+
| severityText | resource.attributes.service.name |
|--------------+----------------------------------|
| ERROR | checkout |
| DEBUG | cart |
+--------------+----------------------------------+
The following query shows that applying reverse twice returns documents in the original order:
source=otellogs
| reverse
| reverse
| fields severityText, `resource.attributes.service.name`
| head 5
The query returns the following results:
fetched rows / total rows = 5/5
+--------------+----------------------------------+
| severityText | resource.attributes.service.name |
|--------------+----------------------------------|
| INFO | frontend |
| INFO | cart |
| WARN | product-catalog |
| ERROR | payment |
| DEBUG | cart |
+--------------+----------------------------------+
The following query uses the reverse command with filtering and field selection:
source=otellogs
| where severityText = 'ERROR'
| fields severityText, `resource.attributes.service.name`
| reverse
The query returns the following results:
fetched rows / total rows = 7/7
+--------------+----------------------------------+
| severityText | resource.attributes.service.name |
|--------------+----------------------------------|
| ERROR | payment |
| ERROR | checkout |
| ERROR | payment |
| ERROR | frontend-proxy |
| ERROR | recommendation |
| ERROR | product-catalog |
| ERROR | checkout |
+--------------+----------------------------------+