elasticlunr

DocumentStore

constructor
elasticlunr.DocumentStore()

Option name Type Description
save Boolean

If the original JSON document should be stored.

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

elasticlunr.DocumentStore store original JSON format documents that you could build search snippet by this original JSON document.

user could choose whether original JSON format document should be store, if no configuration then document will be stored defaultly.
If user care more about the index size, user could select not store JSON documents, then this will has some defects, such as user
could not use JSON document to generate snippets of search results.

elasticlunr.DocumentStore = function (save) {
  if (save === null || save === undefined) {
    this._save = true;
  } else {
    this._save = save;
  }

  this.docs = {};
  this.docInfo = {};
  this.length = 0;
};

load

method
elasticlunr.DocumentStore.load()

Option name Type Description
serialisedData Object

The serialised document store to load.

return elasticlunr.DocumentStore

Loads a previously serialised document store

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

  store.length = serialisedData.length;
  store.docs = serialisedData.docs;
  store.docInfo = serialisedData.docInfo;
  store._save = serialisedData.save;

  return store;
};

isDocStored

method
elasticlunr.DocumentStore.prototype.isDocStored()

check if current instance store the original doc

elasticlunr.DocumentStore.prototype.isDocStored = function () {
  return this._save;
};

addDoc

method
elasticlunr.DocumentStore.prototype.addDoc()

Option name Type Description
docRef Integer,String

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.

Document is store by original JSON format, then you could use original document to generate search snippets.

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

  if (this._save === true) {
    this.docs[docRef] = clone(doc);
  } else {
    this.docs[docRef] = null;
  }
};

getDoc

method
elasticlunr.DocumentStore.prototype.getDoc()

Option name Type Description
docRef Integer,String

The key to lookup and retrieve from the document store.

return Object

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

If docRef not found, return null.
If user set not storing the documents, return null.

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

hasDoc

method
elasticlunr.DocumentStore.prototype.hasDoc()

Option name Type Description
docRef Integer,String

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 Integer,String

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];
  delete this.docInfo[docRef];
  this.length--;
};

addFieldLength

method
elasticlunr.DocumentStore.prototype.addFieldLength()

Option name Type Description
docRef Integer,String

document's id or reference

fieldName String

field name

length Integer

field length

Add field length of a document's field tokens from pipeline results.
The field length of a document is used to do field length normalization even without the original JSON document stored.

elasticlunr.DocumentStore.prototype.addFieldLength = function (docRef, fieldName, length) {
  if (docRef === null || docRef === undefined) return;
  if (this.hasDoc(docRef) == false) return;

  if (!this.docInfo[docRef]) this.docInfo[docRef] = {};
  this.docInfo[docRef][fieldName] = length;
};

updateFieldLength

method
elasticlunr.DocumentStore.prototype.updateFieldLength()

Option name Type Description
docRef Integer,String

document's id or reference

fieldName String

field name

length Integer

field length

Update field length of a document's field tokens from pipeline results.
The field length of a document is used to do field length normalization even without the original JSON document stored.

elasticlunr.DocumentStore.prototype.updateFieldLength = function (docRef, fieldName, length) {
  if (docRef === null || docRef === undefined) return;
  if (this.hasDoc(docRef) == false) return;

  this.addFieldLength(docRef, fieldName, length);
};

getFieldLength

method
elasticlunr.DocumentStore.prototype.getFieldLength()

Option name Type Description
docRef Integer,String

document id or reference

fieldName String

field name

return Integer

field length

get field length of a document by docRef

elasticlunr.DocumentStore.prototype.getFieldLength = function (docRef, fieldName) {
  if (docRef === null || docRef === undefined) return 0;

  if (!(docRef in this.docs)) return 0;
  if (!(fieldName in this.docInfo[docRef])) return 0;
  return this.docInfo[docRef][fieldName];
};

toJSON

method
elasticlunr.DocumentStore.prototype.toJSON()

Returns a JSON representation of the document store used for serialisation.

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

clone

function
clone()

Option name Type Description
object Object

in JSON format

return Object

copied object

Cloning object

function clone(obj) {
  if (null === obj || "object" !== typeof obj) return obj;

  var copy = obj.constructor();

  for (var attr in obj) {
    if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
  }

  return copy;
}