Function createIndex

Create search index with given options

  • 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

    Returns SearchIndex<ID, Document, Index>

    A instance of SearchIndex with given options

    // Create a search engine that indexes the 'title' and 'text' fields of your
    // documents:
    const searchIndex = createIndex({ fields: ['title', 'text'] })
    // Your documents are assumed to include a unique 'id' field, but if you want
    // to use a different field for document identification, you can set the
    // 'idField' option:
    const searchIndex = createIndex({ idField: 'key', fields: ['title', 'text'] })
    // The full set of options (here with their default value) is:
    const searchIndex = createIndex({
    // idField: field that uniquely identifies a document
    idField: 'id',

    // extractField: function used to get the value of a field in a document.
    // By default, it assumes the document is a flat object with field names as
    // property keys and field values as string property values, but custom logic
    // can be implemented by setting this option to a custom extractor function.
    extractField: (document, fieldName) => document[fieldName],

    // tokenize: function used to split fields into individual terms. By
    // default, it is also used to tokenize search queries, unless a specific
    // `tokenize` search option is supplied. When tokenizing an indexed field,
    // the field name is passed as the second argument.
    tokenize: (string, _fieldName) => string.split(SPACE_OR_PUNCTUATION),

    // processTerm: function used to process each tokenized term before
    // indexing. It can be used for stemming and normalization. Return a falsy
    // value in order to discard a term. By default, it is also used to process
    // search queries, unless a specific `processTerm` option is supplied as a
    // search option. When processing a term from a indexed field, the field
    // name is passed as the second argument.
    processTerm: (term, _fieldName) => term.toLowerCase(),

    // searchOptions: default search options, see the `search` method for
    // details
    searchOptions: undefined,

    // fields: document fields to be indexed. Mandatory, but not set by default
    fields: undefined

    // storeFields: document fields to be stored and returned as part of the
    // search results.
    storeFields: []
    })