OLAP Aggregate Queries : Grouping Parameter : Special Grouping Functions : BATCH Function

BATCH Function
The BATCH function divides a scalar field’s values into specific ranges. Each range becomes a grouping field value, and objects contribute to the metric computation for the ranges for which it has values. The BATCH function’s first value must be a scalar field. The remaining values must be literal values compatible with the field’s type (text, timestamp, or numeric), and they must be given in ascending order. Example:
GET /Email/Message/_aggregate?m=COUNT(*)&f=BATCH(Size,100,1000,10000,100000)&shards=2014-06-01
This query counts messages grouped by specific ranges of the Size field from the shard named 2014‑06‑01. The ranges are divided at the given literal values: 100, 1000, 10000, and 100000. The lowest value implicitly creates an extra less than group; the highest value is open-ended and creates a greater than or equal to group. The query in the example above defines the following 5 groups:
Group 1: Size < 100
Group 2: Size >= 100 AND Size < 1000
Group 3: Size >= 1000 AND Size < 10000
Group 4: Size >= 10000 AND Size < 100000
Group 5: Size >= 100000
The example above returns a result such as the following:
<results>
<aggregate metric="COUNT(Size)" group="BATCH(Size,100,1000,10000,100000)"/>
<groups>
<group>
<field name="Size">&lt;100</field>
<metric>0</metric>
</group>
<group>
<field name="Size">100-1000</field>
<metric>125</metric>
</group>
<group>
<field name="Size">1000-10000</field>
<metric>4651</metric>
</group>
<group>
<field name="Size">10000-100000</field>
<metric>1149</metric>
</group>
<group>
<field name="Size">&gt;=100000</field>
<metric>105</metric>
</group>
</groups>
<summary>6030</summary>
</results>
In JSON:
{"results": {
"aggregate": {"group": "BATCH(Size,100,1000,10000,100000)", "metric": "COUNT(Size)"},
"groups": [
{"group": {
"field": {"Size": "<100"},
"metric": "0"
}},
{"group": {
"field": {"Size": "100-1000"},
"metric": "125"
}},
{"group": {
"field": {"Size": "1000-10000"},
"metric": "4651"
}},
{"group": {
"field": {"Size":"10000-100000"},
"metric": "1149"
}},
{"group": {
"field": {"Size": ">=100000"},
"metric": "105"
}}
],
"summary":"6030"
}}
As shown above in the <100 group, if no selected object has a value that falls into one of the specified groups, that group is still returned: the group’s metric is 0 for the COUNT function and empty for all other metric functions. As with all grouped aggregate queries, a summary value is returned that applies the metric function across all groups.