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.

// Get suggestions for 'neuro':
autoSuggest(searchIndex, 'neuro')
// => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 0.46240 } ]
// 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 }
// ]
// Correct spelling mistakes using fuzzy search:
autoSuggest(searchIndex, 'neromancer', { fuzzy: 0.2 })
// => [ { suggestion: 'neuromancer', terms: [ 'neuromancer' ], score: 1.03998 } ]
// 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

    • ID

      The id type of the documents being indexed.

    • Document

      The type of the documents being indexed.

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

      The type of the documents being indexed.

    Parameters

    • searchIndex: SearchIndex<ID, Document, Index>

      The search index

    • queryString: string

      Query string to be expanded into suggestions

    • options: SearchOptions<ID, Index> = {}

      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.