Skip to main content
If a text or JSON field is in the GROUP BY or ORDER BY clause, it must use the literal tokenizer.
A terms aggregation counts the number of occurrences for every unique value in a field. For example, the following query groups the mock_items table by rating, and calculates the number of items for each unique rating.
Expected Response
Ordering by the bucketing field is supported:
Ordering by the aggregate value is not yet supported.
For performance reasons, we strongly recommend adding a LIMIT to the GROUP BY. Terms aggregations without a LIMIT consume more memory and are slower to execute. If a query does not have a limit and more than 65000 unique values are found in a field, an error will be returned.

JSON Syntax

The SQL GROUP BY form above is the recommended way to run a terms aggregation. The same aggregation can also be expressed directly with pdb.agg, using the terms JSON syntax this page is named after:
Expected Response
Array fields (such as text[]) are also supported; each element is counted into its respective bucket. See the Tantivy documentation for all available options.