Class: Vector

lunr.Vector

new Vector(elementsopt)

A vector is used to construct the vector space of documents and queries. These vectors support operations to determine the similarity between two documents or a document and a query.

Normally no parameters are required for initializing a vector, but in the case of loading a previously dumped vector the raw elements can be provieded to the constructor.

For performance reasons vectors are implemented with a flat array, where an elements index is immediatly followed by its value. E.g. [index, value, index, value]. This allows the underlying array to be as sparse as possible and still offer decent performance when being used for vector calculations.

Parameters:
Name Type Attributes Description
elements Array.<Number> <optional>

The flat list of element index and element value pairs.

Source:

Methods

dot(otherVector) → {Number}

Calculates the dot product of this vector and another vector.

Parameters:
Name Type Description
otherVector lunr.Vector

The vector to compute the dot product with.

Source:
Returns:
Type
Number

insert(insertIdx, val)

Inserts an element at an index within the vector.

Parameters:
Name Type Description
insertIdx Number

The index at which the element should be inserted.

val Number

The value to be inserted into the vector.

Source:

magnitude() → {Number}

Calculates the magnitude of this vector.

Source:
Returns:
Type
Number

similarity(otherVector) → {Number}

Calculates the cosine similarity between this vector and another vector.

Parameters:
Name Type Description
otherVector lunr.Vector

The other vector to calculate the similarity with.

Source:
Returns:
Type
Number

toArray() → {Array.<Number>}

Converts the vector to an array of the elements within the vector.

Source:
Returns:
Type
Array.<Number>

toJSON() → {Array.<Number>}

A JSON serializable representation of the vector.

Source:
Returns:
Type
Array.<Number>