Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/document_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ lunr.Store.prototype.remove = function (id) {
* @memberOf Store
*/
lunr.Store.prototype.toJSON = function () {
var store = {};
for(var key in this.store){
if(!this.store.hasOwnProperty(key))continue;
if(typeof this.store[key].toJSON == 'function'){
store[key] = this.store[key].toJSON();
}
else {
store[key] = this.store[key];
}
}
return {
store: this.store,
store: store,
length: this.length
}
}

14 changes: 14 additions & 0 deletions test/serialisation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ test('dumping and loading an index', function () {
deepEqual(idx.search('green plant'), clonedIdx.search('green plant'))
})

test('dumping and loading an index without stringify', function () {
var idx = new lunr.Index

idx.field('title', { boost: 10 })
idx.field('body')

this.corpus.forEach(function (doc) { idx.add(doc) })

var dumpedIdx = idx.toJSON(),
clonedIdx = lunr.Index.load(dumpedIdx)

deepEqual(idx.search('green plant'), clonedIdx.search('green plant'))
})

test('dumping and loading an index with a populated pipeline', function () {
var idx = lunr(function () {
this.field('title', { boost: 10 })
Expand Down