Lunr.js Add Index Entry Information

In lunr.js you can add a unique link using the method .ref(), but I cannot find any method to add additional data / information about this particular record. Is this not possible, or will I miss something really obvious.

I even tried to assign a ref object, but it saves it as a string.

EDIT At the moment, I am saving all the contents as a JSON string in .ref(), which works, but is very ugly to use.

+4
source share
1 answer

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]
})
+6
source

Source: https://habr.com/ru/post/1570346/


All Articles