Function autoSuggest

  • Provide suggestions for the given search query

    The result is a list of suggested modified search queries, derived from the given search query, each with a relevance score, sorted by descending score.

    By default, it uses the same options used for search, except that by default it performs prefix search on the last term of the query, and combine terms with 'AND' (requiring all query terms to match). Custom options can be passed as a second argument. Defaults can be changed by passing an autoSuggestOptions option when initializing the index.

    Basic usage:

    // Get suggestions for 'neuro':
    autoSuggest(searchIndex, 'neuro')
    // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ]

    Multiple words:

    // Get suggestions for 'zen ar':
    autoSuggest(searchIndex, 'zen ar')
    // => [
    // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
    // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
    // ]

    Fuzzy suggestions:

    // Correct spelling mistakes using fuzzy search:
    autoSuggest(searchIndex, 'neromancer', { fuzzy: 0.2 })
    // => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ]

    Filtering:

    // Get suggestions for 'zen ar', but only within the 'fiction' category
    // (assuming that 'category' is a stored field):
    autoSuggest(searchIndex, 'zen ar', {
    filter: (result) => result.category === 'fiction'
    })
    // => [
    // { suggestion: 'zen archery art', terms: [ 'zen', 'archery', 'art' ], score: 1.73332 },
    // { suggestion: 'zen art', terms: [ 'zen', 'art' ], score: 1.21313 }
    // ]

    Type Parameters

    • Document

    • ID

    Parameters

    • searchIndex: SearchIndex<Document, ID>

      The search Index

    • queryString: string

      Query string to be expanded into suggestions

    • options: SearchOptions<ID> = {}

      Search options. The supported options and default values are the same as for the search method, except that by default prefix search is performed on the last term in the query, and terms are combined with 'AND'.

    Returns Suggestion[]

    A sorted array of suggestions sorted by relevance score.

Generated using TypeDoc