elasticlunr.InvertedIndex is used for efficient storing and lookup of the inverted index of token to document ref.
elasticlunr.InvertedIndex = function () {
this.root = { docs: {}, df: 0 };
this.length = 0;
};
Option name | Type | Description |
---|---|---|
serialisedData | Object | The serialised inverted index to load. |
return | elasticlunr.InvertedIndex |
Loads a previously serialised inverted index.
elasticlunr.InvertedIndex.load = function (serialisedData) {
var idx = new this;
idx.root = serialisedData.root;
idx.length = serialisedData.length;
return idx;
};
Option name | Type | Description |
---|---|---|
token | String | |
tokenInfo | Object | format: { ref: 1, tf: 2 } |
root | Object | An optional node at which to start looking for the correct place to enter the doc, by default the root of this elasticlunr.InvertedIndex |
Adds a {token: tokenInfo} pair to the inverted index.
If the token already exist, then update the tokenInfo.
By default this function starts at the root of the current inverted index, however
it can start at any node of the inverted index if required.
elasticlunr.InvertedIndex.prototype.addToken = function (token, tokenInfo, root) {
var root = root || this.root,
idx = 0;
while (idx <= token.length - 1) {
var key = token[idx];
if (!(key in root)) root[key] = {docs: {}, df: 0};
idx += 1;
root = root[key];
}
var docRef = tokenInfo.ref;
if (!root.docs[docRef]) {
// if this doc not exist, then add this doc
root.docs[docRef] = {tf: tokenInfo.tf};
root.df += 1;
this.length += 1;
} else {
// if this doc already exist, then update tokenInfo
root.docs[docRef] = {tf: tokenInfo.tf};
}
};
Option name | Type | Description |
---|---|---|
token | String | The token to check |
return | Boolean |
Checks whether this key is in this elasticlunr.InvertedIndex.
elasticlunr.InvertedIndex.prototype.hasToken = function (token) {
if (!token) return false;
var node = this.root;
for (var i = 0; i < token.length; i++) {
if (!node[token[i]]) return false;
node = node[token[i]];
}
return true;
};
Option name | Type | Description |
---|---|---|
token | String | The token to get the node for. |
return | Object |
Retrieve a node from the inverted index for a given token.
If token not found in this InvertedIndex, return null.
elasticlunr.InvertedIndex.prototype.getNode = function (token) {
if (!token) return null;
var node = this.root;
for (var i = 0; i < token.length; i++) {
if (!node[token[i]]) return null;
node = node[token[i]];
}
return node;
};
Option name | Type | Description |
---|---|---|
token | String | The token to get the documents for. |
return | Object |
Retrieve the documents for a node for the given token.
If token not found, return {}.
elasticlunr.InvertedIndex.prototype.getDocs = function (token) {
var node = this.getNode(token);
if (node == null) {
return {};
}
return node.docs;
};
Option name | Type | Description |
---|---|---|
token | String | The token to get the documents for. |
docRef | String,Integer | |
return | Object |
Retrieve the term frequency of given token in given docRef.
If token or docRef not found, return 0.
elasticlunr.InvertedIndex.prototype.getTF = function (token, docRef) {
var node = this.getNode(token);
if (node == null) {
return 0;
}
if (!(docRef in node.docs)) {
return 0;
}
return node.docs[docRef].tf;
};
Option name | Type | Description |
---|---|---|
token | String | The token to get the documents for. |
return | Object |
Retrieve the document frequency of given token.
If token not found, return 0.
elasticlunr.InvertedIndex.prototype.getDocFreq = function (token) {
var node = this.getNode(token);
if (node == null) {
return 0;
}
return node.df;
};
Option name | Type | Description |
---|---|---|
token | String | The token to get the documents for. |
ref | String | The ref of the document to remove from this token. |
Remove the document identified by ref from the token in the inverted index.
elasticlunr.InvertedIndex.prototype.removeToken = function (token, ref) {
if (!token) return;
var node = this.root;
for (var i = 0; i < token.length; i++) {
if (!(token[i] in node)) return;
node = node[token[i]];
}
if (ref in node.docs) {
delete node.docs[ref];
node.df -= 1;
}
};
Option name | Type | Description |
---|---|---|
token | String | The token to expand. |
return | Array |
Find all the possible suffixes of the passed token using tokens currently in the inverted index.
If token not found, return empty Array.
elasticlunr.InvertedIndex.prototype.expandToken = function (token, memo, root) {
if (token == null || token == '') return [];
if (root == void 0) {
root = this.getNode(token);
if (root == null) return [];
}
var memo = memo || [];
if (root.df > 0) memo.push(token);
for (var key in root) {
if (key === 'docs') continue;
if (key === 'df') continue;
memo.concat(this.expandToken(token + key, memo, root[key]));
}
return memo;
};
Returns a representation of the inverted index ready for serialisation.
elasticlunr.InvertedIndex.prototype.toJSON = function () {
return {
root: this.root,
length: this.length
};
};