top

Displays the top N grouped results. You can get the same result as stats count by FIELD, ... | sort limit=N -count with shorter syntax.

Command properties

PropertyDescription
Command typeProcessing query
Required permissionNone
License usageN/A
Parallel executionSupported
Distributed executionRuns on Data Node (mapper)

Syntax

top [n=INT] FIELD, ...

Options

n=INT
The number of top results to display (default: 10). Must be an integer of 1 or greater.

Target

FIELD, ...
The list of fields to group by. Specify multiple fields separated by commas (,). You must specify at least one field.

Input fields

There are no separately defined input fields. The grouping fields specified in FIELD, ... in the target clause must exist in the input records to be reflected in the grouped results.

Output fields

FieldTypeDescription
(FIELD)(original type)The grouping fields specified in FIELD, ...
countlongNumber of records per group

Error codes

Parsing errors
Error codeMessageDescription
invalid-limitinvalid-limitThe n option value is 0 or less, or cannot be converted to an integer
missing-fieldmissing-fieldNo field to group by was specified
Runtime errors

N/A

Description

The top command never runs. The query planner (TopCommandRewritingPlanner) always rewrites it into the command pair stats count by FIELD, ... | sort limit=N -count before execution, and that command pair actually runs. In other words, it aggregates counts by the specified field combination, sorts them in descending order (largest count first), and returns only the top n results. If the rewrite fails (for example, due to an internal parsing error), the original top command passes through unchanged, forwarding the input without modification.

Examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Find the URIs with the most requests

    table WEB_APACHE_SAMPLE
    | top uri
    

    Groups by uri and returns the top 10 URIs with the most requests.

  2. Query with a specified count

    table WEB_APACHE_SAMPLE
    | top n=5 status, method
    

    Groups by the combination of status and method and returns the top 5 combinations with the most occurrences.

  3. The same query written directly with stats and sort

    table WEB_APACHE_SAMPLE
    | stats count by uri
    | sort limit=10 -count
    

    Returns the same result as example 1.

Change history

The top command is available in the Sonar 4.0 series starting with 4.0.2609.0, where the command previously provided by the Experimental app was built into Sonar core. On versions without the built-in command, install the Experimental app to use it.