在lunr.js中,您可以使用该.ref()方法添加唯一引用,但我找不到任何方法来添加有关该特定记录的额外数据/信息.这是不可能的,还是我错过了一些非常明显的东西.
我甚至尝试将对象分配给ref,但它将其保存为字符串.
编辑
现在我将所有内容保存为JSON字符串.ref(),这可以使用,但是真的很难使用.
lunr根本不存储您将其传递给索引的文档,它的索引方式意味着原始文档根本不可用于lunr,因此无法传递和存储与索引对象关联的元数据.
更好的解决方案是将您的记录保留在lunr之外,并使用您提供给lunr的引用来获取搜索结果时的记录.这样,您可以存储任何您想要的任意元数据.
一个简单的实现可能看起来像这样,它过于简单但你明白了......
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]
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1702 次 |
| 最近记录: |