繁体   English   中英

分配后无法将文档添加到 lunr 索引(TypeError:idx.add 不是函数)

[英]Can't add documents to lunr index after assignment (TypeError: idx.add is not a function)

我正在尝试创建一个 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 is not a function。 我已经看到多个教程说这是你应该能够做到的。

如果我在分配 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 noob,所以这可能与 lunr 没有必然关系。

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

有一个升级到最新版本的指南,它有望涵盖该(和其他)教程中的旧版本与最新版本之间的差异。

我有同样的错误,你必须使用旧的 v1.0.0 才能这样做。

v1.0

                  `var idx = lunr(function () {
                     this.ref('id')
                       this.field('text')
                     })


  // somewhere else, when you received 1 json from a stream.
 oboe().node('features.*', function( ___feature___ ){
                                 idx.add( one-node-json)
                     )}

`

v2.x 不能那样做,2.x 文件是在配置功能结束前添加的

      `var idx = lunr(function () {
                    this.ref('id')
                   this.field('text')

               // at this moment you have to have whole json,  not work with oboe stream json
               // when stream json, only 1 node received at a time, you do not have whole-json yet 
                //until you reach stream end
                this.add( whole-json)
               })`

为了避免将整个 1GB json 存储在内存中,我选择 oboe 来流式传输 json,意思是一次只发出 1 个 json,我只能使用 v1.x 将这一个 json 添加到 idx。

v2.x,我必须将那 1 个 json 一一存储直到结束,在内存中加起来最多 1 GB,那时,我可以将整个 1GB json 添加到 idx,不工作,因为许多用户的浏览器会崩溃1GB 内存。

但是 v1.x 可以工作,因为它使用更少的内存,一次只添加 1 个 json。

我已经要求作者在未来解决这个问题

v1 v2 区别

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM