Skip to content

InvertedIndex

Defined in: packages/data/src/multimap/inverted-index.ts:23

A token-to-documents index: the structure behind full-text search.

An IMultimap<Token, DocId> read backwards — instead of “what does this document contain”, it answers “which documents contain this”. That inversion is what turns a scan over every document into a lookup plus a set intersection.

Postings are a Set per token, so indexing the same token twice for one document is idempotent and hasEntry is O(1). Tokenisation is the caller’s job: this indexes whatever tokens it is handed, which keeps stemming, case-folding and language rules out of the data structure.

Time complexity: O(1) for add, has and hasEntry; search is O(smallest posting list × number of tokens), because it intersects starting from the rarest token; deleteDocument is O(tokens indexed).

Token

The token type, usually string.

DocId

The document identifier type.

new InvertedIndex<Token, DocId>(iterable?): InvertedIndex<Token, DocId>

Defined in: packages/data/src/multimap/inverted-index.ts:30

Iterable<readonly [Token, DocId], any, any>

InvertedIndex<Token, DocId>

[iterator](): Iterator<readonly [Token, DocId]>

Defined in: packages/data/src/multimap/inverted-index.ts:38

Iterator<readonly [Token, DocId]>

IMultimap.[iterator]


add(token, docId): void

Defined in: packages/data/src/multimap/inverted-index.ts:73

O(1). Idempotent: indexing the same pair twice changes nothing.

Token

DocId

void

IMultimap.add


addAll(token, docIds): void

Defined in: packages/data/src/multimap/inverted-index.ts:99

O(m) in the number of documents.

Token

Iterable<DocId>

void

IMultimap.addAll


clear(): void

Defined in: packages/data/src/multimap/inverted-index.ts:47

Removes all elements from the collection.

void

IMultimap.clear


count(): number

Defined in: packages/data/src/multimap/inverted-index.ts:43

The total number of (token, document) postings. O(1).

number

IMultimap.count


countFor(token): number

Defined in: packages/data/src/multimap/inverted-index.ts:200

O(1).

Token

number

IMultimap.countFor


delete(token): number

Defined in: packages/data/src/multimap/inverted-index.ts:132

Removes token and every posting under it. O(p) in the posting list.

Token

number

How many postings were removed.

IMultimap.delete


deleteDocument(docId): boolean

Defined in: packages/data/src/multimap/inverted-index.ts:175

Removes a document from every posting list it appears in.

O(t) in the number of tokens that document was indexed under — the reverse map is carried precisely so this is not a scan of the whole index.

DocId

boolean

true if the document was indexed.


deleteEntry(token, docId): boolean

Defined in: packages/data/src/multimap/inverted-index.ts:150

O(1).

Token

DocId

boolean

IMultimap.deleteEntry


documentCount(): number

Defined in: packages/data/src/multimap/inverted-index.ts:59

The number of distinct documents indexed. O(1).

number


documentIds(): IterableIterator<DocId>

Defined in: packages/data/src/multimap/inverted-index.ts:210

Every indexed document identifier, in first-indexed order.

IterableIterator<DocId>


entries(): IterableIterator<readonly [Token, DocId]>

Defined in: packages/data/src/multimap/inverted-index.ts:215

Every (token, document) posting, grouped by token.

IterableIterator<readonly [Token, DocId]>

IMultimap.entries


get(token): IterableIterator<DocId>

Defined in: packages/data/src/multimap/inverted-index.ts:64

The documents containing token, in first-indexed order.

Token

IterableIterator<DocId>

IMultimap.get


has(token): boolean

Defined in: packages/data/src/multimap/inverted-index.ts:118

O(1). Whether any document holds token.

Token

boolean

IMultimap.has


hasEntry(token, docId): boolean

Defined in: packages/data/src/multimap/inverted-index.ts:123

O(1).

Token

DocId

boolean

IMultimap.hasEntry


index(docId, tokens): void

Defined in: packages/data/src/multimap/inverted-index.ts:111

Indexes one document under every one of its tokens — the natural way in.

O(t) in the number of tokens. Adds to whatever is already indexed for docId; call deleteDocument first to re-index from scratch.

DocId

Iterable<Token>

void


keyCount(): number

Defined in: packages/data/src/multimap/inverted-index.ts:54

The number of distinct tokens indexed. O(1).

number

IMultimap.keyCount


keys(): IterableIterator<Token>

Defined in: packages/data/src/multimap/inverted-index.ts:205

Every indexed token, in first-indexed order.

IterableIterator<Token>

IMultimap.keys


search(tokens): IterableIterator<DocId>

Defined in: packages/data/src/multimap/inverted-index.ts:230

Documents containing every token — a conjunctive query.

Intersects from the rarest token outwards, so the work is bounded by the shortest posting list rather than the longest. An empty tokens matches nothing, not everything: “all of no conditions” is not a useful search.

Iterable<Token>

IterableIterator<DocId>


searchAny(tokens): IterableIterator<DocId>

Defined in: packages/data/src/multimap/inverted-index.ts:260

Documents containing any of the tokens — a disjunctive query.

Iterable<Token>

IterableIterator<DocId>