javascript - 分配后无法将文档添加到 lunr 索引(TypeError : idx. add is not a function)

标签 javascript lunrjs

我正在尝试创建一个 lunr 索引并能够在分配后向其中添加文档。这是我正在尝试做的稍微简化的版本:

var documents = [{
  'id': '1',
  'content': 'hello'
}, {
  'id': '2',
  'content': 'world'
}, {
  'id': '3',
  'content': '!'
}];

var idx = lunr(function() {
  this.ref('id');
  this.field('content');
});

for (var i = 0; i < documents.length; ++i) {
  idx.add(documents[i]);
}

这给我以下错误:TypeError: idx.add 不是一个函数。 我见过多个 tutorials说这是你应该能够做到的。

如果我在分配 idx 时添加文档,它只对我有用;

var documents = [{
  'id': '1',
  'content': 'hello'
}, {
  'id': '2',
  'content': 'world'
}, {
  'id': '3',
  'content': '!'
}];

var idx = lunr(function() {
  this.ref('id');
  this.field('content');

  for (var i = 0; i < documents.length; ++i) {
    this.add(documents[i]);
  }
});

我仍然是一个 javascript 菜鸟,所以这可能与 lunr 不一定相关。

最佳答案

您链接到的教程适用于旧版本的 Lunr。最新版本要求您在传递给 lunr 函数的函数中将所有文档添加到索引中。换句话说,您的第二个示例对于最新版本的 Lunr 是正确的。

有一个guide关于升级到最新版本,这应该有望涵盖该(和其他)教程的旧版本与最新版本之间的差异。

关于javascript - 分配后无法将文档添加到 lunr 索引(TypeError : idx. add is not a function),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44050415/

相关文章:

javascript - 使用函数来划分范围

javascript - 更新页面上的整数

javascript - 如何将 props 发送到点击处理程序 - React JS

javascript - Observable 的设置值未在 Knockout 中更新

javascript - lunr : Return the stem of the searched terms so I can highlight it within results

javascript - 使用 Js Search 在 Node 应用程序中构建索引

javascript - lunr.js 添加关于索引记录的数据

javascript - Node.js 忽略除第一个参数之外的所有查询字符串参数