> ## Documentation Index
> Fetch the complete documentation index at: https://docs.paradedb.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Range

> Count the number of occurrences over user-defined buckets

The range aggregation counts the number of occurrences over user-defined buckets. The buckets must be continuous and cannot overlap.

<CodeGroup>
  ```sql SQL theme={null}
  SELECT pdb.agg('{"range": {"field": "rating", "ranges": [{"to": 3.0 }, {"from": 3.0, "to": 6.0} ]}}')
  FROM mock_items
  WHERE id @@@ pdb.all();
  ```

  ```ts Drizzle theme={null}
  import { search } from "@paradedb/drizzle-paradedb";

  await db
    .select({
      agg: search.agg({
        range: {
          field: "rating",
          ranges: [{ to: 3.0 }, { from: 3.0, to: 6.0 }],
        },
      }),
    })
    .from(mockItems)
    .where(search.all(mockItems.id));
  ```

  ```python Django theme={null}
  from paradedb import Agg, All, ParadeDB

  MockItem.objects.filter(
      id=ParadeDB(All())
  ).aggregate(agg=Agg('{"range": {"field": "rating", "ranges": [{"to": 3.0}, {"from": 3.0, "to": 6.0}]}}'))
  ```

  ```python SQLAlchemy theme={null}
  from sqlalchemy import select
  from sqlalchemy.orm import Session
  from paradedb.sqlalchemy import facets, pdb, search

  stmt = (
      select(
          pdb.agg(
              facets.range(
                  field="rating",
                  ranges=[{"to": 3.0}, {"from": 3.0, "to": 6.0}],
              )
          )
      )
      .select_from(MockItem)
      .where(search.all(MockItem.id))
  )

  with Session(engine) as session:
      session.execute(stmt).all()
  ```

  ```ruby Rails theme={null}
  MockItem.search(:id)
          .match_all
          .facets_agg(
            agg: ParadeDB::Aggregations.range(
              :rating,
              ranges: [{ to: 3.0 }, { from: 3.0, to: 6.0 }]
            )
          )
  ```

  ```cs EF Core theme={null}
  await dbContext
      .MockItems.Where(item => EF.Functions.All(item.Id))
      .Select(item =>
          EF.Functions.Agg(
              new
              {
                  range = new
                  {
                      field = "rating",
                      ranges = new object[] { new { to = 3.0 }, new { @from = 3.0, to = 6.0 } }
                  }
              }
          )
      )
      .ToListAsync();
  ```
</CodeGroup>

```ini Expected Response theme={null}
                                                                              agg
----------------------------------------------------------------------------------------------------------------------------------------------------------------
 {"buckets": [{"to": 3.0, "key": "*-3", "doc_count": 4}, {"to": 6.0, "key": "3-6", "from": 3.0, "doc_count": 37}, {"key": "6-*", "from": 6.0, "doc_count": 0}]}
(1 row)
```

See the [Tantivy documentation](https://docs.rs/tantivy/latest/tantivy/aggregation/bucket/struct.RangeAggregation.html)
for all available options.
