Class SearchIndex<Document, ID>

Type Parameters

  • Document = any

    The type of the documents being indexed.

  • ID = any

    The id type of the documents being indexed.

    Basic example:

    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 }
    // ]

Constructors

Properties

_avgFieldLength: number[]
_currentVacuum: null | Promise<void>
_dirtCount: number
_documentCount: number
_documentIds: Map<number, ID>
_enqueuedVacuum: null | Promise<void>
_enqueuedVacuumConditions: undefined | VacuumConditions
_fieldIds: {
    [key: string]: number;
}

Type declaration

  • [key: string]: number
_fieldLength: Map<number, number[]>
_idToShortId: Map<ID, number>
_index: SearchableMap<FieldTermData>
_nextId: number
_options: OptionsWithDefaults<Document, any>
_storedFields: Map<number, Record<string, unknown>>

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

  • get termCount(): number
  • Number of terms in the index

    Returns number

Methods

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

    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 the same options used to create the original instance that was serialized.

    Usage:

    // 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

    A plain-object serializable representation of the search index.

Generated using TypeDoc