# invertedIndex

> **invertedIndex**<`R`, `K`>(`records`, `keysOf`): [`InvertedIndex`](/reference/knotkit/inverted-index/interfaces/invertedindex/)<`K`>

Defined in: [packages/inverted-index/src/inverted-index.ts:187](https://github.com/jkomyno/knotkit/blob/ef04dc6fd5182dca281abddf3e1dcb13118c15a4/packages/inverted-index/src/inverted-index.ts#L187)

Index `records` by the exact keys returned by `keysOf(record, id)`. The selector is called once per record. Each iterable is consumed once. Keys use Map equality. Repeated keys on one record yield one posting. IDs denote positions in the input at build time. The index retains keys and IDs after construction.

Expected O(n + t) construction time for n records and t yielded keys, excluding the selector’s own work. With p distinct record/key associations and k distinct keys, both build workspace and retained storage are O(p + k). A single-key lookup costs expected O(1) plus O(1) per returned ID.

## Type Parameters

### R

`R`

### K

`K`

## Parameters

### records

`Iterable`<`R`>

### keysOf

(`record`, `id`) => `Iterable`<`K`>

## Returns

[`InvertedIndex`](/reference/knotkit/inverted-index/interfaces/invertedindex/)<`K`>

## Throws

With code `MAX_CAPACITY_EXCEEDED` when a record ID or the posting count exceeds 2³² − 1, or when the runtime rejects the key table or packed columns. Errors raised by input iterators and the selector propagate to the caller.
