Elasticlunr搜索基于索引返回文档对象

时间:2017-01-03 15:35:47

标签: javascript

elasticlunr.js搜索配置如下,

var index = elasticlunr(function () {
  this.addField('title');
  this.addField('body');
  this.setRef('id');
});

var doc1 = {
  "id": '1',
  "title": "Oracle released its latest database Oracle 12g beta",
  "body": "Yestaday Oracle has released its new database Oracle 12g, this would make more money for this company and lead to a nice profit report of annual year."
}

var doc2 = {
  "id": '2',
  "title": "Oracle released its profit report of 2015",
  "body": "As expected, Oracle released its profit report of 2015, during the good sales of database and hardware, Oracle's profit of 2015 reached 12.5 Billion."
}

index.addDoc(doc1);
index.addDoc(doc2);

let result = index.search("beta");

它返回,

  

[{ “REF”: “1”, “得分”:0.3779644730092272}]

elasticlunr document表示它正在将文档存储在索引中。
那我怎么得到那个特定索引的存储文件呢?
我希望将原始文档作为搜索结果吗? 即。

[{
      "id": '1',
      "title": "Oracle released its latest database Oracle 12g beta",
      "body": "Yestaday Oracle has released its new database Oracle 12g, this would make more money for this company and lead to a nice profit report of annual year."
    }]

2 个答案:

答案 0 :(得分:2)

您可以从documentStore中的index获取。因此,在获得结果后,使用id从商店获取文档:

index.documentStore.getDoc('1')

答案 1 :(得分:0)

我将我的文档存储在数组中,由索引引用。 当我向elasticlunr添加文档时,我使用的id指的是数组中的一个位置。 这样,当我得到一组结果时,我使用id来查找我的数组中的相关文档。