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 provided to the constructor.
For performance reasons vectors are implemented with a flat array, where an elements index is immediately 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 |
<optional> |
The flat list of element index and element value pairs. |
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. |
Returns:
- Type
- Number
insert(insertIdx, val)
Inserts an element at an index within the vector.
Does not allow duplicates, will throw an error if there is already an entry for this index.
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. |
magnitude() → {Number}
Calculates the magnitude of this vector.
Returns:
- Type
- Number
positionForIndex(insertIdx) → {Number}
Calculates the position within the vector to insert a given index.
This is used internally by insert and upsert. If there are duplicate indexes then the position is returned as if the value for that index were to be updated, but it is the callers responsibility to check whether there is a duplicate at that index
Parameters:
Name | Type | Description |
---|---|---|
insertIdx |
Number | The index at which the element should be inserted. |
Returns:
- Type
- Number
similarity(otherVector) → {Number}
Calculates the similarity between this vector and another vector.
Parameters:
Name | Type | Description |
---|---|---|
otherVector |
lunr.Vector | The other vector to calculate the similarity with. |
Returns:
- Type
- Number
toArray() → {}
Converts the vector to an array of the elements within the vector.
Returns:
- Type
toJSON() → {}
A JSON serializable representation of the vector.
Returns:
- Type
upsert(insertIdx, val, fn)
Inserts or updates an existing 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. |
fn |
function | A function that is called for updates, the existing value and the requested value are passed as arguments |