elasticlunr

DocumentStore

constructor
elasticlunr.DocumentStore()

elasticlunr.DocumentStore is a simple key-value document store used for storing sets of tokens for
documents stored in index.

elasticlunr.DocumentStore = function () {
  this.docs = {};
  this.length = 0;
};

load

method
elasticlunr.DocumentStore.load()

Option name Type Description
serialisedData Object

The serialised document store to load.

return elasticlunr.Store

Loads a previously serialised document store

elasticlunr.DocumentStore.load = function (serialisedData) {
  var store = new this;

  store.length = serialisedData.length;
  store.docs = serialisedData.docs;

  return store;
};

addDoc

method
elasticlunr.DocumentStore.prototype.addDoc()

Option name Type Description
docRef Object

The key used to store the JSON format doc.

doc Object

The JSON format doc.

Stores the given doc in the document store against the given id.
If docRef already exist, then update doc.

elasticlunr.DocumentStore.prototype.addDoc = function (docRef, doc) {
  if (!this.hasDoc(docRef)) this.length++;
  this.docs[docRef] = doc;
};

getDoc

method
elasticlunr.DocumentStore.prototype.getDoc()

Option name Type Description
docRef, Object

The key to lookup and retrieve from the document store.

return Object

Retrieves the JSON doc from the document store for a given key.

elasticlunr.DocumentStore.prototype.getDoc = function (docRef) {
  return this.docs[docRef];
};

hasDoc

method
elasticlunr.DocumentStore.prototype.hasDoc()

Option name Type Description
docRef Object

The id to look up in the document store.

return Boolean

Checks whether the document store contains a key (docRef).

elasticlunr.DocumentStore.prototype.hasDoc = function (docRef) {
  return docRef in this.docs;
};

removeDoc

method
elasticlunr.DocumentStore.prototype.removeDoc()

Option name Type Description
docRef Object

The id to remove from the document store.

Removes the value for a key in the document store.

elasticlunr.DocumentStore.prototype.removeDoc = function (docRef) {
  if (!this.hasDoc(docRef)) return;

  delete this.docs[docRef];
  this.length--;
};

toJSON

method
elasticlunr.DocumentStore.prototype.toJSON()

Returns a representation of the document store ready for serialisation.

elasticlunr.DocumentStore.prototype.toJSON = function () {
  return {
    docs: this.docs,
    length: this.length
  };
};