Explainer.java
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.Version;
import java.io.File;
// From chapter 3
public class Explainer {
public static void main(String[] args) throws Exception {
String indexDir = "indexes/MeetLucene";
String queryExpression = "apache";
Directory directory = FSDirectory.open(new File(indexDir));
QueryParser parser = new QueryParser(Version.LUCENE_30,
"contents", new SimpleAnalyzer());
Query query = parser.parse(queryExpression);
System.out.println("Query: " + queryExpression);
IndexSearcher searcher = new IndexSearcher(directory);
TopDocs topDocs = searcher.search(query, 10);
for (ScoreDoc match : topDocs.scoreDocs) {
Explanation explanation
= searcher.explain(query, match.doc); //#A
System.out.println("----------");
Document doc = searcher.doc(match.doc);
System.out.println(doc.get("filename"));
System.out.println(explanation.toString()); //#B
}
searcher.close();
directory.close();
}
}
/*
#A Generate Explanation
#B Output Explanation
*/
结果:
Query: apache
----------
apache1.0.txt
0.5776299 = (MATCH) fieldWeight(contents:apache in 0), product of:
3.8729835 = tf(termFreq(contents:apache)=15)
2.3862944 = idf(docFreq=3, maxDocs=16)
0.0625 = fieldNorm(field=contents, doc=0)
----------
apache1.1.txt
0.49465272 = (MATCH) fieldWeight(contents:apache in 1), product of:
3.3166249 = tf(termFreq(contents:apache)=11)
2.3862944 = idf(docFreq=3, maxDocs=16)
0.0625 = fieldNorm(field=contents, doc=1)
----------
apache2.0.txt
0.13050047 = (MATCH) fieldWeight(contents:apache in 2), product of:
2.0 = tf(termFreq(contents:apache)=4)
2.3862944 = idf(docFreq=3, maxDocs=16)
0.02734375 = fieldNorm(field=contents, doc=2)
本文介绍了一个使用Java实现的Lucene查询解释器示例,该示例展示了如何通过解释器获取文档的相关性评分及其详细计算过程。通过具体代码及运行结果,读者可以了解Lucene内部评分机制。
1754

被折叠的 条评论
为什么被折叠?



