2017-01-03 18 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は、インデックスにドキュメントを格納していると言います。
それでは、どのように私はそのperticularインデックスから格納されたドキュメントを取得できますか?
元のドキュメントを検索結果として取得しようとしていますか? すなわち

[{ 
     "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は、配列内の位置の1つを参照します。 そのようにして、一連の結果が得られたら、IDを使用して配列内の関連するドキュメントを検索します。

+0

はい..私はこのビット以前やった...問題は、私は、同様アレイの私のインデックスとオブジェクトを更新する必要があります。管理するのは難しいでしょう。また、ストレージの観点からは 'lunr index object'と' array全体のオブジェクト 'を格納する必要があります –

1

indexdocumentStoreから入手できます。だから、あなたは結果を取得した後、ストアからドキュメントを取得するためにidを使用します。

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

'.docs [' 1 ']'の代わりに' .getDoc( '1') 'メソッドを使用するほうが良いでしょう –

+0

!私は答えを更新しました。 – pihvi