Skip to main content
$dateHistogram groups date values into fixed time buckets. Use it for time-series analytics like events per hour/day.

Compatibility

Field TypeSupported
TEXTNo
U64/I64/F64No
DATEYes
BOOLNo
KEYWORDNo
FACETNo
Field must be FAST.

Arguments

ArgumentTypeRequiredDescription
fieldstringYesDate field to bucket on.
fixedIntervalstringYesInterval string such as "1m", "1h", "1d".
offsetstringNoShift bucket boundaries.
minDocCountnumberNoExclude buckets with fewer docs.
hardBounds{ min: number, max: number }NoHard clamp for bucket range.
extendedBounds{ min: number, max: number }NoEmit buckets across this range, including empty ones.
keyedbooleanNoIf true, returns buckets as an object. Default: false.
For hardBounds and extendedBounds, both min and max are required.
await index.aggregate({
  aggregations: {
    by_day: {
      $dateHistogram: {
        field: "createdAt",
        fixedInterval: "1d",
      },
    },
  },
});

Output

{
  "by_day": {
    "buckets": [
      { "key": 1704067200000, "keyAsString": "2024-01-01T00:00:00Z", "docCount": 14 },
      { "key": 1704153600000, "keyAsString": "2024-01-02T00:00:00Z", "docCount": 9 }
    ]
  }
}