Bucket Aggregations

$range

$range groups documents into custom ranges.

Use it when bucket boundaries are defined by business logic (for example pricing tiers).

Compatibility

Field TypeSupported
TEXTNo
U64/I64/F64Yes
DATEYes
BOOLNo
KEYWORDNo
FACETNo

Field must be FAST.

Arguments

ArgumentTypeRequiredDescription
fieldstringYesField to bucket on.
rangesArray<{ key?: string, from?: number, to?: number }>YesNon-empty array of range objects.
keyedbooleanNoIf true, returns buckets as an object. Default: false.

from is inclusive. to is exclusive.

await index.aggregate({  aggregations: {    price_tiers: {      $range: {        field: "price",        ranges: [{ to: 30 }, { from: 30, to: 60 }, { from: 60 }],      },    },  },});
index.aggregate(    aggregations={        "price_tiers": {            "$range": {                "field": "price",                "ranges": [{"to": 30}, {"from": 30, "to": 60}, {"from": 60}],            }        }    })
SEARCH.AGGREGATE products '{}' '{"price_tiers": {"$range": {"field": "price", "ranges": [{"to": 30}, {"from": 30, "to": 60}, {"from": 60}]}}}'

Output

{  "price_tiers": {    "buckets": [      { "key": "*-30", "docCount": 3, "to": 30 },      { "key": "30-60", "docCount": 3, "from": 30, "to": 60 },      { "key": "60-*", "docCount": 3, "from": 60 }    ]  }}

Bucket entries can include fromAsString / toAsString and nested sub-aggregation outputs.

Loading search…