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

# Indexes

> Declare the field orders that serve selections. Indexes are unnamed and matched by structure.

A table's `index` option lists the field orders that serve its selections.
An index has no name: a selection is matched to an index by the fields it
uses.

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { t, table } from "@bijectionhq/bijection";

export const orders = table(
  { customer: t.string, state: t.one("placed", "paid"), total: t.decimal, placed: t.instant },
  { index: [["customer", "placed"], ["state", "placed"]] },
);
```

## What an index serves

An index `[a, b, c]` serves a selection that fixes a leading run of its
fields by equality, then ranges over or orders by the next one.

| Selection | Served by `["customer", "placed"]` |
| - | - |
| `orders.where({ customer })` | Yes |
| `orders.where({ customer }).by("placed")` | Yes |
| `orders.where({ customer, placed: { from, to } })` | Yes |
| `orders.by("customer")` | Yes |
| `orders.where({ placed: { from, to } })` | No: `placed` is not a leading field |
| `orders.where({ customer: { from, to } }).by("placed")` | No: a range ends the order |

A table's key and each `unique` field set are also orders, so
`customers({ email })` and `customers.by("email")` need no `index` entry when
`unique: [["email"]]` is declared. A series is also ordered by valid time
across its keys.

This table holds for a clear table. On a
[protected table](https://docs.bijection.com/database/protected-tables.md) (labelled, erasable or with a
`lifetime`), an index keeps only a token of its fields at the store. It serves
a selection that fixes every one of its fields by equality with every verb, and
any other selection only as `ctx.list(selection.atMost(n))`.

## A selection no index serves

A selection that no declared order serves is refused, in the editor and at
run time, with the index to add:

```text theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
IndexMissing: no declared index serves where({ state }).by('total'); add index: [['state', 'total']] to this table
```

There is no unindexed scan. Declare the order each question needs.

## Ranked fields

Ranked selections are served by the field's type and, where they filter, by an
index.

| Read | Declare |
| - | - |
| `docs.search("textBody", text)` | `body: t.text()` |
| `docs.near("embedding", vector)` | `embedding: t.vector(1536)` |
| `docs.where({ owner }).near("embedding", vector)` | `index: [["owner", "embedding"]]` on the `t.vector` field |
| `sites.near("site", origin, { distance })`, `.region("site", area)` | `site: t.geometry({ kind: ["Point"] })` last in an index entry: `index: [["region", "site"]]` |

A ranked read whose where fields no index lists before the ranked field is
refused with `FilterMissing` and the entry to add.

## Indexes on a protected table

Every order of a protected table keeps a token of its fields, so it answers
exact lookups: `orders.where({ customer })` on `["customer"]`. A partial
equality, a range or an order over it is evaluated in the kernel, bounded by
`.atMost(n)`; `.first(n)`, `ctx.page` and `ctx.count` of such a selection
refuse `ProtectedShapeUnsupported`. To keep an index's fields clear and in
order, so it serves every shape, declare a reviewed store exposure:

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, t, table } from "@bijectionhq/bijection";

export const ops = label();

export const tickets = table(
  { queue: t.string, opened: t.instant, summary: t.string },
  { label: ops, index: [["queue"], { fields: ["queue", "opened"], store: "clear", message: "The store orders tickets by queue and opening time" }] },
);
```

See [Protected tables](https://docs.bijection.com/database/protected-tables.md) for the shapes each index
serves, what the store sees, and what each costs.

## Wide reads and declared covers (`across`)

Every index is maintained at the fact's own partition. When a function reads a
member-placed or shared table widely—under a family stem (`tenant.all`) or over
a disjunctive share (`label.principals`)—declare a cover with `across` on the
index entry (or on the table, `t.text`, `t.vector`, `total`, or
`changesAcross`):

```ts theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
import { label, t, table } from "@bijectionhq/bijection";

export const tenant = label.family(t.string);

export const orders = table(
  { customer: t.string, state: t.one("placed", "paid"), placed: t.instant },
  {
    label: tenant,
    index: [
      ["customer", "placed"],
      { fields: ["state", "placed"], across: tenant.all },
    ],
  },
);
```

`across` serves reads. For example, `unique: [["email"]]` allows two tenant
members to use the same email. With a table cover at `tenant.all`, a
stem-cleared by-id selection can return both rows. Use `.of(member)` for an
email point. To reserve a handle across the family,
declare `unique: [{ fields: ["handle"], scope: tenant.all, release }]`.
A stem-wide handle point also needs a row cover for its index.
[Shared claims](https://docs.bijection.com/database/protected-tables.md#share-a-unique-claim-release)

* **Pay only for wide paths.** A table-level `across` covers every path, total
  and `.changes`; a path-level `across` covers only that index or ranked field.
  On a strict update, only changed covered values write to the cover.
* **Narrow without a cover.** `.of(member)` narrows any read to one member's
  partition (1 root place, no cover needed).
* **`bijection check` diagnostics.** Every read site and function frame is
  statically bounded to at most 64 root places:
  * `CoverRequired` identifies the missing cover or the one Place a keyed
    point needs. Narrow with `.of(member)`, or use a selection for multiple matches.
  * `ReadPlacesUnbounded` identifies a selector or source walk beyond the
    declared bounds. Bound the selector, choose one member, or use a stem your
    frame already clears and declare its cover.
  * `CoverOverlap` identifies a reduction whose inputs can include the same
    fact and whose plan lacks complete identity evidence. Narrow to a single
    member or select a disjoint plan. Eligible Root counts and ranks can use
    a bounded walk over distinct facts.

## Choosing indexes

* Put the fields a selection fixes first, and the field it orders or ranges
  over last.
* One index serves every selection on a leading run of its fields, so
  `["customer", "placed"]` also serves `.where({ customer })`. On a
  [protected table](https://docs.bijection.com/database/protected-tables.md) that leading run is a partial
  equality, read only with `.atMost(n)`: declare `["customer"]` too when
  `.where({ customer })` takes `.first(n)`, `ctx.page` or `ctx.count`.
* Each index is maintained on every write of the table. Declare the orders
  your functions read, and no others.


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