top

This page explains how to use the top operator function in APL.

The top operator in Axiom Processing Language (APL) allows you to retrieve the top N rows from a dataset based on specified criteria. It’s particularly useful when you need to analyze the highest values in large datasets or want to quickly identify trends, such as the highest request durations in logs or top error occurrences in traces. You can apply it in scenarios like log analysis, security investigations, or tracing system performance.

Usage

Syntax

| top N by Expression [asc | desc]

Parameters

  • N: The number of rows to return.
  • Expression: A scalar expression used for sorting. The type of the values must be numeric, date, time, or string.
  • [asc | desc]: Optional. Use to sort in ascending or descending order. The default is descending.

Returns

The top operator returns the top N rows from the dataset based on the specified sorting criteria.

Use case examples

The top operator helps you find the HTTP requests with the longest durations.

Query

['sample-http-logs']
| top 5 by req_duration_ms

Run in Playground

Output

_timereq_duration_msidstatusurimethodgeo.citygeo.country
2024-10-01 10:12:345000123200/api/get-dataGETNew YorkUS
2024-10-01 11:14:204900124200/api/post-dataPOSTChicagoUS
2024-10-01 12:15:454800125200/api/update-itemPUTLondonUK

This query returns the top 5 HTTP requests that took the longest time to process.

The top operator is useful for identifying the spans with the longest duration in distributed tracing systems.

Query

['otel-demo-traces']
| top 5 by duration

Run in Playground

Output

_timedurationspan_idtrace_idservice.namekindstatus_code
2024-10-01 10:12:34300msspan123trace456frontendserver200
2024-10-01 10:13:20290msspan124trace457cartserviceclient200
2024-10-01 10:15:45280msspan125trace458checkoutserviceserver500

This query returns the top 5 spans with the longest durations from the OpenTelemetry traces.

The top operator is useful for identifying the most frequent HTTP status codes in security logs.

Query

['sample-http-logs']
| summarize count() by status
| top 3 by count_

Run in Playground

Output

statuscount_
200500
40450
50020

This query shows the top 3 most common HTTP status codes in security logs.

  • order: Use when you need full control over row ordering without limiting the number of results.
  • summarize: Useful when aggregating data over fields and obtaining summarized results.
  • take: Returns the first N rows without sorting. Use when ordering isn’t necessary.

Other query languages