2017-01-03 97 views
1

elasticlunr.js搜索配置如下返回文檔對象,Elasticlunr搜索基於所述索引

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." 
    }] 

回答

0

我將我的文檔存儲在索引引用的數組中。 當我將文檔添加到elasticlunr時,我使用的id引用數組中的某個位置。 這樣,當我得到一組結果時,我使用id來查找數組中的相關文檔。

+0

是..我這樣做有點早..問題是我需要更新我的索引和數組對象,以及。這將很難管理。從存儲的角度來看,我需要存儲'lunr索引對象'和'整個對象在數組中' –

1

你可以從documentStoreindex中得到它。所以,你得到的結果後,使用id獲得從商店的DOC:

index.documentStore.getDoc('1') 
+0

它可能更好使用.getDoc('1')'方法而不是'.docs ['1']' –

+0

謝謝!我更新了答案。 – pihvi