LuceneInAction-explain()理解搜索结果评分

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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值