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

# Keyword facets

> Count matching documents by keyword value to build category, brand, and tag filters.

Facets return document counts for values in `keyword` fields, including arrays and nested fields addressed by dotted paths. Use them to show filter choices such as category or brand counts alongside search results.

## Request facets

Add `facets` to the [query request](https://docs.lambdadb.ai/reference/api/endpoint/document-query.md). Each key is an indexed keyword field name. Set its `size` to the maximum number of buckets to return (default 10); see [search limits](https://docs.lambdadb.ai/guides/get-started/limits.md#search-and-reads) for the allowed range.

```json theme={null}
{
  "size": 0,
  "query": {"queryString": {"query": "status:active"}},
  "facets": {
    "category": {"size": 5},
    "tags": {}
  }
}
```

A response includes a `facets` object alongside the usual document results:

```json theme={null}
{
  "took": 4,
  "total": 0,
  "docs": [],
  "isDocsInline": true,
  "facets": {
    "category": {"buckets": [{"value": "books", "count": 12}]},
    "tags": {"buckets": [{"value": "sale", "count": 7}]}
  }
}
```

This **facet-only** request uses `"size": 0` to return no documents. Set a positive `size` within the [query result limit](https://docs.lambdadb.ai/guides/get-started/limits.md#search-and-reads) to include document results. `total` still counts returned documents, so it is zero in that case. Omit `query` to count all documents in the selected read scope.

## Counting rules

* Counts cover **all documents matching the query**, independent of the number of returned documents or buckets. All query and partition filters apply, including filters on the facet's own field.
* A document with `["sale", "sale", "new"]` contributes once to `sale` and once to `new`. Bucket counts can therefore sum to more than the number of matching documents.
* Missing fields, null fields, empty arrays, and values excluded from indexing produce no bucket. Empty strings are indexed values. Existing keyword length limits still apply.
* Buckets are ordered by count descending, then value ascending in Unicode code point order. Values are not analyzed or folded by locale or case.
* Every requested field is present in the response; a field without matching indexed values has an empty `buckets` array. Without a facet request, the response omits `facets`.
* Counts remain inline when document results are downloaded through `docsUrl`.

## Supported queries and read scope

Use facets with `queryString`, `bool` combinations of supported queries, or no query. Vector, sparse vector, and hybrid queries are rejected when facets are requested. Numeric or date range facets, nested bucket aggregations, and excluding a facet's own filter are not supported.

Adding [native reranking](https://docs.lambdadb.ai/guides/search/native-reranking.md) preserves these restrictions. For supported scoring lexical queries with positive `size`, counts still cover all matches and are unchanged by reranking or fallback; they are not counts of the reranked candidate pool. Facet-only `size: 0` requests cannot rerank.

The existing `partitionFilter`, `ref`, and `consistentRead` rules apply. Counts use the selected Branch, Tag, or Alias and the same pending-write visibility as document results. `consistentRead: true` requires a Branch read, either directly (including the default main Branch when `ref` is omitted) or through an Alias that currently targets a Branch; Tags and Aliases that target a Tag reject it. Facets do not introduce a stronger global snapshot guarantee.

## Limits and cost

See [search limits](https://docs.lambdadb.ai/guides/get-started/limits.md#search-and-reads) for field counts, returned buckets, and aggregation budgets. Counts are exact across the selected partitions: all buckets are combined before selecting the returned top buckets.

If the population exceeds the distinct bucket or value-byte budget across requested fields, the request fails with HTTP 400 instead of returning partial counts. The same value in two fields counts twice. Narrow the query or request fewer fields; reducing a facet's return `size` does not reduce the number of values that must be counted.

Separate memory admission limits protect aggregation work and distributed response merging. A request whose estimated memory exceeds its budget returns HTTP 400; request fewer fields or select fewer partitions. This estimate also depends on indexed values outside the matching population, so a narrower query alone may not resolve it. If concurrent facet requests have temporarily exhausted a budget, the request returns HTTP 429; retry with backoff. No partial counts are returned on either failure.

Facets visit all matching documents, so broad queries may cost more than retrieving a small number of top documents. Queries without facets do not perform this additional aggregation.

## Index requirements

Configure each facet field as a [keyword index](https://docs.lambdadb.ai/guides/collections/index-types.md#keyword). Keyword fields accept single values or arrays. The top-level document `id` is always a single string; arrays are not allowed for `id`.

## Client compatibility

The recommended [SDK releases](https://docs.lambdadb.ai/reference/sdk/introduction.md#supported-sdk-versions) accept `facets` in query requests and preserve facet results when documents are downloaded through `docsUrl`; see their facet guides for [Python](https://github.com/lambdadb/lambdadb-python-client/blob/v0.14.1/docs/keyword-facets.md), [TypeScript](https://github.com/lambdadb/lambdadb-typescript-client/blob/v0.11.1/docs/keyword-facets.md), and [Go](https://github.com/lambdadb/go-lambdadb/blob/v0.8.0/docs/keyword-facets.md).

The [CLI](https://docs.lambdadb.ai/guides/get-started/use-with-cli.md) accepts facets in query JSON files, and the [MCP server](https://docs.lambdadb.ai/guides/get-started/use-with-mcp.md) accepts them through `lambdadb_query_collection`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.