Back to Paradedb

Date Histogram

docs/documentation/aggregates/bucket/datehistogram.mdx

0.24.02.0 KB
Original Source

The date histogram aggregation constructs a histogram for date fields.

<CodeGroup> ```sql SQL SELECT pdb.agg('{"date_histogram": {"field": "created_at", "fixed_interval": "30d"}}') FROM mock_items WHERE id @@@ pdb.all(); ```
ts
import { search } from "@paradedb/drizzle-paradedb";

await db
  .select({
    agg: search.agg({
      date_histogram: { field: "created_at", fixed_interval: "30d" },
    }),
  })
  .from(mockItems)
  .where(search.all(mockItems.id));
python
from paradedb import Agg, All, ParadeDB

MockItem.objects.filter(
    id=ParadeDB(All())
).aggregate(agg=Agg('{"date_histogram": {"field": "created_at", "fixed_interval": "30d"}}'))
python
from sqlalchemy import select
from sqlalchemy.orm import Session
from paradedb.sqlalchemy import facets, pdb, search

stmt = (
    select(pdb.agg(facets.date_histogram(field="created_at", fixed_interval="30d")))
    .select_from(MockItem)
    .where(search.all(MockItem.id))
)

with Session(engine) as session:
    session.execute(stmt).all()
ruby
MockItem.search(:id)
        .match_all
        .facets_agg(agg: ParadeDB::Aggregations.date_histogram(:created_at, fixed_interval: "30d"))
cs
await dbContext
    .MockItems.Where(item => EF.Functions.All(item.Id))
    .Select(item =>
        EF.Functions.Agg(new { date_histogram = new { field = "created_at", fixed_interval = "30d" } })
    )
    .ToListAsync();
</CodeGroup>
ini

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"buckets": [{"key": 1679616000000.0, "doc_count": 14, "key_as_string": "2023-03-24T00:00:00Z"}, {"key": 1682208000000.0, "doc_count": 27, "key_as_string": "2023-04-23T00:00:00Z"}]}
(1 row)

See the Tantivy documentation for all available options.