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)