使用 Lucene 4.3.0
Lucene 新手。我想获取更多文档,例如当前选择的文档。根据我的研究,旧版本的 Lucene 有一个 MoreLikeThis (这与我想要的行为相似)。
我整理了一些玩具代码来测试选项。我已完成索引并将 TermVector 包含在索引中。
代码摘录
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "body", this.analyzer);
Query query = null ;
try {
query = parser.parse(searchterm);
ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs;
simpleresult = simpleresult + "HITS = " + hits.length + "\n";
IndexReader ir = isearcher.getIndexReader() ; //2013-06-09 testing
simpleresult = simpleresult + "Total Indexed Num Docs = " + ir.numDocs() + "\n" ;
//Loop through results and construct simple string representation
for (int i = 0; i < hits.length; i++) {
Document hitdoc = isearcher.doc(hits[i].doc);
float docscore = hits[i].score ;
simpleresult = simpleresult + "=======" + (i+1) + "=======\n" ;
simpleresult = simpleresult + "DOCDBID: " + hitdoc.get("dbid") + "\n" ;
simpleresult = simpleresult + "Score: " + docscore + "\n" ;
simpleresult = simpleresult + "File: " + hitdoc.get("filename") + "\n" ;
simpleresult = simpleresult + hitdoc.get("body") ;
simpleresult = simpleresult + "\n--------META--------\n" ;
simpleresult = simpleresult + hitdoc.get("meta") ;
simpleresult = simpleresult + "==============\n" ;
//TESTING 2013-06-09
//Trying to mimic similar documents
//Feed the text contents of the current document back into nother query?????
query = parser.parse(hitdoc.get("body"));
ScoreDoc[] simhits = isearcher.search(query, null, 1000).scoreDocs;
TopDocs top = isearcher.search(query, 10);
simpleresult = simpleresult + "Similar Hits = " + simhits.length + "\n";
simpleresult = simpleresult + "Top Hits MaxScore= " + top.getMaxScore() + "\n"; //why does this score differ from the above scores???????
simpleresult = simpleresult + "Top Hits = " + top.totalHits + "\n";
}
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.close() ;
同样,这是一个玩具示例的摘录,因此我可以更好地学习 Lucene。它本质上只是执行一个简单的查询,显示每个结果(在 GUI 中),然后尝试使用每个文档重新查询以查看任何类似的文档以模仿 MoreLikeThis。我要做的是获取与文档类似的文档。我
ty 示例是在 Lucene 4+ 中执行此操作的正确方法吗?