topk
This page explains how to use the topk aggregation function in APL.
The topk aggregation in Axiom Processing Language (APL) allows you to identify the top k results based on a specified field. This is especially useful when you want to quickly analyze large datasets and extract the most significant values, such as the top-performing queries, most frequent errors, or highest latency requests.
Use topk to find the most common or relevant entries in datasets, especially in log analysis, telemetry data, and monitoring systems. This aggregation helps you focus on the most important data points, filtering out the noise.
Usage
Syntax
Parameters
Field: A string field or expression to rank values by. Non-string inputs return an error.k: The number of top values to return (maximum100,000; larger values return an error).
Returns
Returns one array column per group (named topk_<Field> by default) containing up to k objects with the shape {"key": string, "count": int, "error": int} from an approximate sketch. You can access individual elements by index, for example tk[0].key and tk[0].count.
Use case examples
When analyzing HTTP logs, you can use the topk function to find the top 5 most frequent HTTP status codes.
Query
Output
| topk_status |
|---|
[{"key":"200","count":1500,"error":0},{"key":"404","count":400,"error":0},{"key":"500","count":200,"error":0},{"key":"301","count":150,"error":0},{"key":"302","count":100,"error":0}] |
This query returns one array containing the 5 most frequent HTTP status codes along with their estimated counts and error bounds. To extract the top item into scalar columns, assign an alias and index into the array:
In OpenTelemetry traces, you can use topk to find the top five status codes by service.
Query
Output
| service.name | topk_attributes.http.status_code |
|---|---|
| frontendproxy | [{"key":"200","count":34862088,"error":0},{"key":"203","count":3095223,"error":0},{"key":"404","count":154417,"error":0},{"key":"500","count":153823,"error":0},{"key":"504","count":3497,"error":0}] |
This query returns one array per service containing the top five status codes.
You can use topk in security log analysis to find the top 5 cities generating the most HTTP requests.
Query
Output
| topk_geo.city |
|---|
[{"key":"New York","count":500,"error":0},{"key":"London","count":400,"error":0},{"key":"Paris","count":350,"error":0},{"key":"Tokyo","count":300,"error":0},{"key":"Berlin","count":250,"error":0}] |
This query returns an array of the top 5 cities based on the number of HTTP requests.
List of related aggregations
- top: Returns the top values based on a field without requiring a specific number of results (
k), making it useful when you’re unsure how many top values to retrieve. - topkif: Returns the top
kresults without filtering. Use topk when you don’t need to restrict your analysis to a subset. - sort: Orders the dataset based on one or more fields, which is useful if you need a complete ordered list rather than the top
kvalues. - extend: Adds calculated fields to your dataset, which can be useful in combination with
topkto create custom rankings. - count: Aggregates the dataset by counting occurrences, often used in conjunction with
topkto find the most common values.