lunr does not store documents that you pass to them for indexing at all, the way it indexes means that the original document is not accessible to lunr at all, so there is no way to transfer and store metadata associated with the indexed object.
The best solution is to save your entries outside of lunr and use the link you give lunr to pull out the entry when you get the search results. This way you can store any arbitrary metadata you want.
A simple implementation may look like this, its too simplistic, but you get the idea ...
var documents = [{
id: 1,
title: "Third rock from the sun",
album: "Are you expirienced",
rating: 8
},{
id: 2,
title: "If 6 Was 9",
album: "Axis bold as love",
rating: 7
},{
id: 3,
title: "1983...(A Merman I Should Turn to Be)",
album: "Electric Ladyland",
rating: 10
}]
var db = documents.reduce(function (acc, document) {
acc[document.id] = document
return acc
}, {})
var idx = lunr(function () {
this.ref('id')
this.field('title', { boost: 10 })
this.field('album')
})
documents.forEach(function (document) {
idx.add(document)
})
var results = idx.search("love").forEach(function (result) {
return db[result.ref]
})
source
share