The topN metric spec specifies how topN values should be sorted.
## Numeric TopNMetricSpec
The simplest metric specification is a String value indicating the metric to sort topN results by. They are included in a topN query with:
```json
"metric":<metric_value_string>
```
The metric field can also be given as a JSON object. The grammar for dimension values sorted by numeric value is shown below:
```json
"metric":{
"type":"numeric",
"metric":"<metric_value>"
}
```
|property|description|required?|
|--------|-----------|---------|
|type|this indicates a numeric sort|yes|
|metric|the actual metric field in which results will be sorted by|yes|
## Lexicographic TopNMetricSpec
The grammar for dimension values sorted lexicographically is as follows:
```json
"metric":{
"type":"lexicographic",
"previousStop":"<previousStop_value>"
}
```
|property|description|required?|
|--------|-----------|---------|
|type|this indicates a lexicographic sort|yes|
|previousStop|the starting point of the lexicographic sort. For example, if a previousStop value is 'b', all values before 'b' are discarded. This field can be used to paginate through all the dimension values.|no|
TopN queries return a sorted set of results for the values in a given dimension according to some criteria. Conceptually, they can be thought of as an approximate [GroupByQuery](GroupByQuery.html) over a single dimension with an [Ordering](Ordering.html) spec. TopNs are much faster and resource efficient than GroupBys for this use case. These types of queries take a topN query object and return an array of JSON objects where each object represents a value asked for by the topN query.
A topN query object looks like:
```json
"queryType":"topN",
"dataSource":"sample_data",
"dimension":"sample_dim",
"threshold":5,
"metric":"count",
"granularity":"all",
"filter":{
"type":"and",
"fields":[
{
"type":"selector",
"dimension":"dim1",
"value":"some_value"
},
{
"type":"selector",
"dimension":"dim2",
"value":"some_other_val"
}
]
},
"aggregations":[
{
"type":"longSum",
"name":"count",
"fieldName":"count"
},
{
"type":"doubleSum",
"name":"some_metric",
"fieldName":"some_metric"
}
],
"postAggregations":[
{
"type":"arithmetic",
"name":"sample_divide",
"fn":"/",
"fields":[
{
"type":"fieldAccess",
"name":"some_metric",
"fieldName":"some_metric"
},
{
"type":"fieldAccess",
"name":"count",
"fieldName":"count"
}
]
}
],
"intervals":[
"2013-08-31T00:00:00.000/2013-09-03T00:00:00.000"
]
}
```
There are 10 parts to a topN query, but 7 of them are shared with [TimeseriesQuery](TimeseriesQuery.html). Please review [TimeseriesQuery](TimeseriesQuery.html) for meanings of fields not defined below.
|property|description|required?|
|--------|-----------|---------|
|dimension|A JSON object defining the dimension that you want the top taken for. For more info, see [DimensionSpecs](DimensionSpecs.html)|yes|
|threshold|An integer defining the N in the topN (i.e. how many you want in the top list)|yes|
|metric|A JSON object specifying the metric to sort by for the top list. For more info, see [TopNMetricSpec](TopNMetricSpec.html).|yes|
Please note the context JSON object is also available for topN queries and should be used with the same caution as the timeseries case.