# termIndex

> **termIndex**<`R`>(`records`, `termsOf`): [`TermIndex`](/reference/knotkit/term-index/interfaces/termindex/)

Defined in: [packages/term-index/src/term-index.ts:205](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/term-index/src/term-index.ts#L205)

Build a read-only counted index from caller-tokenized documents. The selector runs once per input record. The index retains terms, counts, and document positions. It can discard the source records after construction.

Expected O(n + t) build time for n documents and t token occurrences, plus the selector’s work. Build workspace and retained storage are O(n + v + p) for v vocabulary terms and p distinct postings.

## Type Parameters

### R

`R`

## Parameters

### records

`Iterable`<`R`>

### termsOf

(`record`, `id`) => readonly `string`\[]

## Returns

[`TermIndex`](/reference/knotkit/term-index/interfaces/termindex/)

## Throws

With `INVALID_TERMS` for a selector result other than an array of strings or `MAX_CAPACITY_EXCEEDED` when corpus counts or packed storage exceed their bounds. Errors thrown by the input iterable and selector propagate.
