Interface SearchIndexOptions<ID, Document, Index>

Configuration options passed to the SearchIndex constructor

interface SearchIndexOptions<
    ID = any,
    Document = any,
    Index extends Record<string, any> = Record<never, never>,
> {
    autoSuggestOptions?: SearchOptions<ID, Index>;
    autoVacuum?: boolean | AutoVacuumOptions;
    extractField?: (document: Document, fieldName: string) => string;
    fields: string[];
    idField?: string;
    logger?: (level: LogLevel, message: string, code?: string) => void;
    processTerm?: (
        term: string,
        fieldName?: string,
    ) => undefined | null | string | false | string[];
    searchOptions?: SearchOptions<ID, Index>;
    storeFields?: string[];
    tokenize?: (text: string, fieldName?: string) => string[];
}

Type Parameters

  • ID = any

    The type of id being indexed.

  • Document = any

    The type of documents being indexed.

  • Index extends Record<string, any> = Record<never, never>

    The type of the documents being indexed.

Properties

autoSuggestOptions?: SearchOptions<ID, Index>

Default auto suggest options (see the SearchOptions type and the autoSuggest method for details)

autoVacuum?: boolean | AutoVacuumOptions

If true (the default), vacuuming is performed automatically as soon as discard is called a certain number of times, cleaning up obsolete references from the index. If false, no automatic vacuuming is performed. Custom settings controlling auto vacuuming thresholds, as well as batching behavior, can be passed as an object (see the AutoVacuumOptions type).

extractField?: (document: Document, fieldName: string) => string

Function used to extract the value of each field in documents. By default, the documents are assumed to be plain objects with field names as keys, but by specifying a custom extractField function one can completely customize how the fields are extracted.

The function takes as arguments the document, and the name of the field to extract from it. It should return the field value as a string.

fields: string[]

Names of the document fields to be indexed.

idField?: string

Name of the ID field, uniquely identifying a document.

logger?: (level: LogLevel, message: string, code?: string) => void

Function called to log messages. Arguments are a log level ('debug', 'info', 'warn', or 'error'), a log message, and an optional string code that identifies the reason for the log.

The default implementation uses console, if defined.

processTerm?: (
    term: string,
    fieldName?: string,
) => undefined | null | string | false | string[]

Function used to process a term before indexing or search. This can be used for normalization (such as stemming). By default, terms are downcased, and otherwise no other normalization is performed.

The function takes as arguments a term to process, and the name of the field it comes from. It should return the processed term as a string, or a falsy value to reject the term entirely.

It can also return an array of strings, in which case each string in the returned array is indexed as a separate term.

searchOptions?: SearchOptions<ID, Index>

Default search options (see the SearchOptions type and the search method for details)

storeFields?: string[]

Names of fields to store, so that search results would include them. By default none, so results would only contain the id field.

tokenize?: (text: string, fieldName?: string) => string[]

Function used to split a field value into individual terms to be indexed. The default tokenizer separates terms by space or punctuation, but a custom tokenizer can be provided for custom logic.

The function takes as arguments string to tokenize, and the name of the field it comes from. It should return the terms as an array of strings. When used for tokenizing a search query instead of a document field, the fieldName is undefined.