OLAP Aggregate Queries : Grouping Parameter : Special Grouping Functions : TOP and BOTTOM Functions

TOP and BOTTOM Functions
By default, all group values are returned at each grouping level, and the groups are returned in ascending order of the grouping field value. The TOP and BOTTOM functions can be used to return groups in the order of the metric value. Optionally, they can also be used to limit the number of groups returned to the highest or lowest metric values. The TOP and BOTTOM functions wrap a grouping field expression and specify a limit parameter. For example:
GET /Email/Message/_aggregate?m=SUM(Size)&f=TOP(3,InternalRecipients.Person.Name)&range=0
The first parameter to TOP/BOTTOM is the limit value; the second parameter is the grouping field expression. This aggregate query sums the Size field of message objects, grouped by internal recipient names, but it only returns the groups with the three highest SUM values. Typical results for the example above in XML:
<results>
<aggregate metric="SUM(Size)" query="*" group="TOP(3,InternalRecipients.Person.Name)"/>
<totalobjects>6030</totalobjects>
<summary>190643320</summary>
<totalgroups>836</totalgroups>
<groups>
<group>
<metric>97744099</metric>
<field name="InternalRecipients.Person.Name">(null)</field>
</group>
<group>
<metric>20060808</metric>
<field name="InternalRecipients.Person.Name">Marc Bourauel</field>
</group>
<group>
<metric>16798901</metric>
<field name="InternalRecipients.Person.Name">Nina Cantauw</field>
</group>
</groups>
</results>
In JSON:
{"results": {
"aggregate": {
"metric": "SUM(Size)",
"query": "*",
"group": "TOP(3,InternalRecipients.Person.Name)"
},
"totalobjects": "6030",
"summary": "190643320",
"totalgroups": "836",
"groups": [
{"group": {
"metric": "97744099",
"field": {"InternalRecipients.Person.Name": "(null)"}
}},
{"group": {
"metric": "20060808",
"field": {"InternalRecipients.Person.Name": "Marc Bourauel"}
}},
{"group": {
"metric": "16798901",
"field": {"InternalRecipients.Person.Name": "Nina Cantauw"}
}}
]
}}
The BOTTOM parameter works the same way but returns the groups with the lowest metric values. When either the TOP or BOTTOM function is used, the total number of groups that were actually computed is returned in the element totalgroups, as shown above.
Some numeric metric expressions can produce a result that is not a valid number, such as when division by zero occurs. Such non-numeric values include “not-a-number” (NaN), positive infinity, and negative infinity. All such invalid numeric results sort to the end (last group) for both TOP and BOTTOM.
When the limit parameter is 0, all groups are returned, but they are returned in metric-computation order. Using 0 for the limit parameter essentially means unlimited.
When the aggregate query has multiple grouping fields, a TOP or BOTTOM function can be used with each grouping field. In secondary groups, TOP and BOTTOM return groups whose metric values are computed relative to their parent groups. Below is an example aggregate query 2-level grouping using TOP for the outer level and BOTTOM for the inner level:
GET /Email/Message/_aggregate?m=COUNT(*)&f=TOP(3,Sender.Person),BOTTOM(2,TRUNCATE(SendDate,DAY))
&shards=2014-06-01