Interface SearchOptions<ID, Index>

Search options to customize the search behavior.

interface SearchOptions<
    ID = any,
    Index extends Record<string, any> = Record<never, never>,
> {
    bm25?: BM25Params;
    boost?: Record<string, number>;
    boostDocument?: (
        documentId: ID,
        term: string,
        storedFields?: Index,
    ) => number;
    boostTerm?: (term: string, i: number, terms: string[]) => number;
    combineWith?: CombinationOperator;
    fields?: string[];
    filter?: (result: SearchResult<ID, Index>) => boolean;
    fuzzy?:
        | number
        | boolean
        | (term: string, index: number, terms: string[]) => number | boolean;
    maxFuzzy?: number;
    prefix?:
        | boolean
        | (term: string, index: number, terms: string[]) => boolean;
    processTerm?: (
        term: string,
    ) => undefined | null | string | false | string[];
    tokenize?: (text: string) => string[];
    weights?: { fuzzy?: number; prefix?: number };
}

Type Parameters

  • ID = any

    The type of id being indexed.

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

    The type of the documents being indexed.

Hierarchy (View Summary)

Properties

bm25?: BM25Params

BM25+ algorithm parameters. Customizing these is almost never necessary, and fine-tuning them requires an understanding of the BM25 scoring model. In most cases, it is best to omit this option to use defaults, and instead use boosting to tweak scoring for specific use cases.

boost?: Record<string, number>

Key-value object of field names to boosting values. By default, fields are assigned a boosting factor of 1. If one assigns to a field a boosting value of 2, a result that matches the query in that field is assigned a score twice as high as a result matching the query in another field, all else being equal.

boostDocument?: (documentId: ID, term: string, storedFields?: Index) => number

Function to calculate a boost factor for documents. It takes as arguments the document ID, and a term that matches the search in that document, and the value of the stored fields for the document (if any). It should return a boosting factor: a number higher than 1 increases the computed score, a number lower than 1 decreases the score, and a falsy value skips the search result completely.

boostTerm?: (term: string, i: number, terms: string[]) => number

Function to calculate a boost factor for each term.

This function, if provided, is called for each query term (as split by tokenize and processed by processTerm). The arguments passed to the function are the query term, the positional index of the term in the query, and the array of all query terms. It is expected to return a numeric boost factor for the term. A factor lower than 1 reduces the importance of the term, a factor greater than 1 increases it. A factor of exactly 1 is neutral, and does not affect the term's importance.

combineWith?: CombinationOperator

The operand to combine partial results for each term. By default it is "OR", so results matching any of the search terms are returned by a search. If "AND" is given, only results matching all the search terms are returned by a search.

fields?: string[]

Names of the fields to search in. If omitted, all fields are searched.

filter?: (result: SearchResult<ID, Index>) => boolean

Function used to filter search results, for example on the basis of stored fields. It takes as argument each search result and should return a boolean to indicate if the result should be kept or not.

fuzzy?:
    | number
    | boolean
    | (term: string, index: number, terms: string[]) => number | boolean

Controls whether to perform fuzzy search. It can be a simple boolean, or a number, or a function.

If a boolean is given, fuzzy search with a default fuzziness parameter is performed if true.

If a number higher or equal to 1 is given, fuzzy search is performed, with a maximum edit distance (Levenshtein) equal to the number.

If a number between 0 and 1 is given, fuzzy search is performed within a maximum edit distance corresponding to that fraction of the term length, approximated to the nearest integer. For example, 0.2 would mean an edit distance of 20% of the term length, so 1 character in a 5-characters term. The calculated fuzziness value is limited by the maxFuzzy option, to prevent slowdown for very long queries.

If a function is passed, the function is called upon search with a search term, a positional index of that term in the tokenized search query, and the tokenized search query. It should return a boolean or a number, with the meaning documented above.

maxFuzzy?: number

Controls the maximum fuzziness when using a fractional fuzzy value. Very high edit distances usually don't produce meaningful results, but can excessively impact search performance.

6
prefix?: boolean | (term: string, index: number, terms: string[]) => boolean

Controls whether to perform prefix search. It can be a simple boolean, or a function.

If a boolean is passed, prefix search is performed if true.

If a function is passed, it is called upon search with a search term, the positional index of that search term in the tokenized search query, and the tokenized search query. The function should return a boolean to indicate whether to perform prefix search for that search term.

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

Function to process or normalize terms in the search query. By default, the same term processor used for indexing is used also for search.

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

Function to tokenize the search query. By default, the same tokenizer used for indexing is used also for search.

weights?: { fuzzy?: number; prefix?: number }

Relative weights to assign to prefix search results and fuzzy search results. Exact matches are assigned a weight of 1.