lucene 一次查询多个id

在文本搜索中,有时也需要一次搜索多个id,这里id类似数据库里面的主键。

这个id在索引里面的倒排列表长度往往等于1.

例如:根据id=[1,2,4,6,7]查询索引

最最一般的思路是构造一个booleanQuery,然后add 5个TermQuery,用should逻辑。

但是这个检索效率肯定不行。

可行的一个办法是:

TermDocs td = null;//

int[] docIds = new int[ids.length];//存放结果

int count = 0 ;

td = search.getIndexReader().termDocs();

for(id : ids){

   td.seek(new Term("id", id));//NumericUtils

   if(td.next())

       docIds[count++] = td.doc();

}//伪代码

docIds收集了ids对应索引中的docId,下一步直接从search中获取document即可。

 

//

数据库做这事一般使用in,貌似效率也很一般,有什么好办法么?

 

 

你可能感兴趣的:(Lucene)