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.
@throws ― {TermIndexInvalidInputError} 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.
Matches a string or an object that supports being matched against, and returns an array
containing the results of that search, or null if no matches are found.
@param ― matcher An object that supports being matched against.
match(/[\p{L}\p{N}]+/gu) ?? []
const
constindex:TermIndex
index=
termIndex<{
title:string;
text:string;
}>(records:Iterable<{
title:string;
text:string;
}>, termsOf: (record: {
title:string;
text:string;
}, id:number) =>readonlystring[]):TermIndex
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.
@throws ― {TermIndexInvalidInputError} 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.
Rank documents matching any supplied exact term. Ties in score favor lower IDs.
Repeated query terms contribute once. The returned array and hits are independent of the index.
@throws ― {TermIndexInvalidInputError} With INVALID_LIMIT for a limit outside the nonnegative safe integers
or INVALID_TERMS for an argument other than an array of strings. Eligibility accessor errors propagate.
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@param ― callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
@param ― thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
Rank documents matching any supplied exact term. Ties in score favor lower IDs.
Repeated query terms contribute once. The returned array and hits are independent of the index.
@throws ― {TermIndexInvalidInputError} With INVALID_LIMIT for a limit outside the nonnegative safe integers
or INVALID_TERMS for an argument other than an array of strings. Eligibility accessor errors propagate.
search(['cache'], {
TermSearchOptions.limit?: number |undefined
Maximum number of hits. Defaults to 10. Zero yields no hits.
Build once from an iterable of documents and a token selector. Search exact terms with BM25 and return scored input positions.
Cost per member. Open a row for every operation.
termIndex5 members
Member
Cost
build
O(n + t) expected for n documents and t input tokens
documentLength
O(1)
documentFrequency
O(1) expected
postings
O(1) expected lookup and O(1) per posting
search
O(q + u log(u + 1) + p + c log(k + 1)) expected for q query tokens, u unique known terms, p visited postings, c eligible matching documents, and k retained hits
Measured by the suite in packages/term-index/__tests__/bench. The numbers come from one machine, so run the suite on yours before deciding on speed alone.
Use knotkit/term-index for this task. If Knotkit is missing, follow https://knotkit.jkomyno.dev/getting-started.md#install-from-source. Import from 'knotkit/term-index'.
Exports:
- `termIndex` → `TermIndex`: Build once from an iterable of documents and a token selector. Search exact terms with BM25 and return scored input positions.
Inputs and their sources:
- one array of tokens for each document and its zero-based ID: `(record: R, id: number) => readonly string[]` from `the caller`
- optional document eligibility, checked before top-k: `Pick<ReadonlySet<number>, 'has'>` from `the caller`
Use something else when:
- find records by exact tags without storing occurrence counts → knotkit/inverted-index
- add or remove postings between searches → Map<string, Map<number, number>>
- index raw text with stemming, typo tolerance, or automatic prefix expansion → zbsearch
Errors are TermIndexInvalidInputError with a `code` property. Branch on `error.code`, never on the message.
Full page: https://knotkit.jkomyno.dev/modules/term-index.md
Types: node_modules/knotkit/term-index/ and https://knotkit.jkomyno.dev/reference/knotkit/term-index/