ndx
Version:
Lightweight Full-Text Indexing and Searching Library.
124 lines • 3.62 kB
TypeScript
/**
* Index data structure.
*
* @typeparam T Document key.
*/
export interface DocumentIndex<T> {
/** Additional information about documents. */
readonly docs: Map<T, DocumentDetails<T>>;
/** Inverted index root node. */
readonly root: InvertedIndexNode<T>;
/** Sum of field lengths in all documents. */
readonly fSum: Float64Array;
/** Average of field lengths in all documents. */
readonly fAvg: Float64Array;
/** Number of removed documents. */
removed: number;
/**
* Tokenizer is a function that breaks text into words, phrases, symbols, or
* other meaningful elements called tokens.
*/
tokenizer(s: string): string[];
/**
* Filter is a function that processes tokens and returns terms, terms are
* used in Inverted Index to index documents.
*/
filter(s: string): string;
}
/**
* Inverted Index Node.
*
* Inverted index is implemented with a
* [trie](https://en.wikipedia.org/wiki/Trie) data structure.
*
* @typeparam T Document key.
*/
export interface InvertedIndexNode<T> {
/** Char code key. */
k: number;
/** Children nodes. */
c: InvertedIndexNode<T>[] | null;
/** Documents associated with this node. */
d: DocumentPointer<T>[] | null;
}
/**
* Document Details object stores additional information about documents.
*
* @typeparam T Document key.
*/
export interface DocumentDetails<T> {
/**
* Document key. It can be a simple unique ID or a direct reference to an
* original document.
*/
readonly key: T;
/**
* Field count is an array that contains number of terms in each indexed
* text field.
*/
readonly fCount: Int32Array;
/**
* Removed flag.
*/
removed: boolean;
}
/**
* Document pointer contains information about term frequency for a document.
*
* @typeparam T Document key.
*/
export interface DocumentPointer<T> {
/**
* Reference to a {@link DocumentDetails} object that is used for this
* document.
*/
readonly details: DocumentDetails<T>;
/**
* Term frequency in each field.
*/
readonly tf: Int32Array;
}
/**
* Creates an Index.
*
* @typeparam T Document key.
* @param fieldsNum Number of fields.
* @returns {@link Index}
*/
export declare const createIndex: <T>(fieldsNum: number, tokenizer: (s: string) => string[], filter: (s: string) => string) => DocumentIndex<T>;
/**
* Finds inverted index node that matches the `term`.
*
* @typeparam T Document key.
* @param node Root node.
* @param term Term.
* @returns Inverted index node that contains `term` or an `undefined` value.
*/
export declare const findInvertedIndexNode: <T>(node: InvertedIndexNode<T> | undefined, term: string) => InvertedIndexNode<T> | undefined;
/**
* Adds a document to the index.
*
* @typeparam T Document key.
* @typeparam D Document type.
* @param index {@link DocumentIndex}.
* @param fieldGetters Field getters.
* @param key Document key.
* @param doc Document.
*/
export declare const indexAdd: <T, D>(index: DocumentIndex<T>, fieldGetters: ((doc: D) => string)[], key: T, doc: D) => void;
/**
* Remove document from the index.
*
* @typeparam T Document key.
* @param index {@link DocumentIndex}.
* @param key Document key.
*/
export declare const indexRemove: <T>(index: DocumentIndex<T>, key: T) => void;
/**
* Cleans up removed documents from the {@link DocumentIndex}.
*
* @typeparam T Document key.
* @param index {@link DocumentIndex}.
*/
export declare function indexVacuum<T>(index: DocumentIndex<T>): void;
//# sourceMappingURL=index.d.ts.map