Interface SearchOptions<ID>

Search options to customize the search behavior.

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

Type Parameters

  • ID = any

    The type of id being indexed.

Hierarchy (view full)

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?: {
    [fieldName: 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.

Type declaration

  • [fieldName: string]: number
boostDocument?: ((documentId, term, storedFields?) => 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.

Type declaration

    • (documentId, term, storedFields?): 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.

      Parameters

      • documentId: ID
      • term: string
      • Optional storedFields: Record<string, unknown>

      Returns number

combineWith?: string

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) => 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.

Type declaration

    • (result): 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.

      Parameters

      Returns boolean

fuzzy?: number | boolean | ((term, index, terms) => 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.

Type declaration

    • (term, index, terms): number | boolean
    • Parameters

      • term: string
      • index: number
      • terms: string[]

      Returns number | boolean

maxFuzzy?: number

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

prefix?: boolean | ((term, index, terms) => 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.

Type declaration

    • (term, index, terms): boolean
    • Parameters

      • term: string
      • index: number
      • terms: string[]

      Returns boolean

processTerm?: ((term) => 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.

Type declaration

    • (term): 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.

      Parameters

      • term: string

      Returns undefined | null | string | false | string[]

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

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

Type declaration

    • (text): string[]
    • Function to tokenize the search query. By default, the same tokenizer used for indexing is used also for search.

      Parameters

      • text: string

      Returns string[]

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.

Type declaration

  • fuzzy: number
  • prefix: number

Generated using TypeDoc