设为首页 收藏本站
查看: 1808|回复: 0

[经验分享] Apache Lucene 评分原理及代码分析

[复制链接]

尚未签到

发表于 2015-8-1 14:16:34 | 显示全部楼层 |阅读模式
  在IndexSearcher类中有一个管理Lucene得分情况的方法,如下所示:



1 public Explanation explain(Weight weight, int doc) throws IOException {
2     return weight.explain(reader, doc);
3 }
  返回的这个Explanation的实例解释了Lucene中Document的得分情况。我们可以测试一下,直观地感觉一下到底这个Explanation的实例都记录了一个Document的哪些信息。
  写一个测试类,如下所示:



  1 package org.shirdrn.lucene.learn;
  2
  3 import java.io.IOException;
  4 import java.util.Date;
  5
  6 import net.teamhot.lucene.ThesaurusAnalyzer;
  7
  8 import org.apache.lucene.document.Document;
  9 import org.apache.lucene.document.Field;
10 import org.apache.lucene.index.CorruptIndexException;
11 import org.apache.lucene.index.IndexWriter;
12 import org.apache.lucene.index.Term;
13 import org.apache.lucene.index.TermDocs;
14 import org.apache.lucene.search.Explanation;
15 import org.apache.lucene.search.Hits;
16 import org.apache.lucene.search.IndexSearcher;
17 import org.apache.lucene.search.Query;
18 import org.apache.lucene.search.TermQuery;
19 import org.apache.lucene.store.LockObtainFailedException;
20
21 public class AboutLuceneScore {
22
23 private String path = "E:\\Lucene\\index";
24
25 public void createIndex(){
26    IndexWriter writer;
27    try {
28     writer = new IndexWriter(path,new ThesaurusAnalyzer(),true);
29   
30     Field fieldA = new Field("contents","一人",Field.Store.YES,Field.Index.TOKENIZED);
31     Document docA = new Document();
32     docA.add(fieldA);
33   
34     Field fieldB = new Field("contents","一人 之交 一人之交",Field.Store.YES,Field.Index.TOKENIZED);
35     Document docB = new Document();
36     docB.add(fieldB);
37   
38     Field fieldC = new Field("contents","一人 之下 一人之下",Field.Store.YES,Field.Index.TOKENIZED);
39     Document docC = new Document();
40     docC.add(fieldC);
41   
42     Field fieldD = new Field("contents","一人 做事 一人当 一人做事一人当",Field.Store.YES,Field.Index.TOKENIZED);
43     Document docD = new Document();
44     docD.add(fieldD);
45   
46     Field fieldE = new Field("contents","一人 做事 一人當 一人做事一人當",Field.Store.YES,Field.Index.TOKENIZED);
47     Document docE = new Document();
48     docE.add(fieldE);
49
50     writer.addDocument(docA);
51     writer.addDocument(docB);
52     writer.addDocument(docC);
53     writer.addDocument(docD);
54     writer.addDocument(docE);
55   
56     writer.close();
57    } catch (CorruptIndexException e) {
58     e.printStackTrace();
59    } catch (LockObtainFailedException e) {
60     e.printStackTrace();
61    } catch (IOException e) {
62     e.printStackTrace();
63    }
64 }
65
66 public static void main(String[] args) {
67    AboutLuceneScore aus = new AboutLuceneScore();
68    aus.createIndex();    // 建立索引
69    try {
70     String keyword = "一人";
71     Term term = new Term("contents",keyword);
72     Query query = new TermQuery(term);
73     IndexSearcher searcher = new IndexSearcher(aus.path);
74     Date startTime = new Date();
75     Hits hits = searcher.search(query);
76     TermDocs termDocs = searcher.getIndexReader().termDocs(term);
77     while(termDocs.next()){
78      System.out.print("搜索关键字在编号为 "+termDocs.doc());
79      System.out.println(" 的Document中出现过 "+termDocs.freq()+" 次");
80     }
81     System.out.println("********************************************************************");
82     for(int i=0;i

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-93076-1-1.html 上篇帖子: Apache + PHP + PEAR 安装配置 + Memcache 简单学习心得 下篇帖子: Apache Flume 简介
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表