Class SearchIndex<ID, Document, Index>

A class to represent search index

const documents = [
{
id: 1,
title: 'Moby Dick',
text: 'Call me Ishmael. Some years ago...',
category: 'fiction'
},
{
id: 2,
title: 'Zen and the Art of Motorcycle Maintenance',
text: 'I can see by my watch...',
category: 'fiction'
},
{
id: 3,
title: 'Neuromancer',
text: 'The sky above the port was...',
category: 'fiction'
},
{
id: 4,
title: 'Zen and the Art of Archery',
text: 'At first sight it must seem...',
category: 'non-fiction'
},
// ...and more
]

// Create a search engine that indexes the 'title' and 'text' fields for
// full-text search. Search results will include 'title' and 'category' (plus the
// id field, that is always stored and returned)
const searchIndex = createIndex({
fields: ['title', 'text'],
storeFields: ['title', 'category']
})

// Add documents to the index
addAll(searchIndex, documents)

// Search for documents:
const results = search(searchIndex, 'zen art motorcycle')
// => [
// { id: 2, title: 'Zen and the Art of Motorcycle Maintenance', category: 'fiction', score: 2.77258 },
// { id: 4, title: 'Zen and the Art of Archery', category: 'non-fiction', score: 1.38629 }
// ]

Type Parameters

  • ID = any

    The id type of the documents being indexed.

  • Document = any

    The type of the documents being indexed.

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

    The type of the documents being indexed.

Constructors

Accessors

  • get dirtCount(): number
  • The number of documents discarded since the most recent vacuuming

    Returns number

  • get dirtFactor(): number
  • A number between 0 and 1 giving an indication about the proportion of documents that are discarded, and can therefore be cleaned up by vacuuming. A value close to 0 means that the index is relatively clean, while a higher value means that the index is relatively dirty, and vacuuming could release memory.

    Returns number

  • get documentCount(): number
  • Total number of documents available to search

    Returns number

  • get isVacuuming(): boolean
  • Is true if a vacuuming operation is ongoing, false otherwise

    Returns boolean

Methods

  • Allows serialization of the index to JSON, to possibly store it and later deserialize it with loadJSONIndex or loadJSONIndexAsync.

    Normally one does not directly call this method, but rather call the standard JavaScript JSON.stringify() passing the SearchIndex instance, and JavaScript will internally call this method. Upon deserialization, one must pass to loadJSONIndex or loadJSONIndexAsync the same options used to create the original instance that was serialized.

    // Serialize the index:
    let searchIndex = createIndex({ fields: ['title', 'text'] })
    addAll(searchIndex, documents)
    const json = JSON.stringify(index)

    // Later, to deserialize it:
    searchIndex = loadJSONIndex(json, { fields: ['title', 'text'] })

    Returns IndexObject<Index>

    A plain-object serializable representation of the search index.