Lucene:基于Java的全文检索引擎简介

本文深入分析了Lucene的查询过程,包括如何通过QueryParser解析查询字符串、构建查询对象,以及IndexSearcher如何利用这些信息搜索索引并返回结果。特别关注了PhraseQuery的工作原理和索引读取流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!--[if !supportLists]-->5.   <!--[endif]-->Lucene代码分析
应用情景分析
 
Query query = parser.parse(queries[j]);
 获得布尔查询
 
hits = searcher.search(query);
 
 
 return new Hits(this, query, filter);
 
  getMoreDocs(50)
 
    TopDocs topDocs = searcher.search(query, filter, n)
 
      IndexSearcher:public TopDocs search(Query query, Filter filter, final int nDocs)

<!--[if !supportLists]-->²         <!--[endif]-->IndexSearcher 开始时已经打开了该目录

<!--[if !supportLists]-->²         <!--[endif]-->IndexSearcher 中初始化了IndexReader

<!--[if !supportLists]-->²         <!--[endif]-->IndexReader中读取了SegmentInfos

<!--[if !supportLists]-->²         <!--[endif]-->IndexReader = SegmentReader

<!--[if !supportLists]-->²         <!--[endif]-->SegmentReader ::initialize(SegmentInfo si)

<!--[if !supportLists]-->n          <!--[endif]-->1。读入域信息,只有域的名字

<!--[if !supportLists]-->n          <!--[endif]-->2. 打开保存域、保存域索引的文件
 
       Scorer scorer = query.weight(this).scorer(reader)

<!--[if !supportLists]-->u        <!--[endif]-->这里query = PhraseQuery

<!--[if !supportLists]-->u        <!--[endif]-->query.weight(this) 获得PhraseWeight(IndexSearcher)

<!--[if !supportLists]-->u        <!--[endif]-->PhraseWeight::scorer(IndexReader reader)

<!--[if !supportLists]-->u        <!--[endif]-->PhraseQuery::TermPositions p = reader.termPositions((Term)terms.elementAt(i));

<!--[if !supportLists]-->u        <!--[endif]-->public TermPositions termPositions(Term term) throws IOException {

IndexReader::TermPositions termPositions = termPositions();

SegmentTermDocs::SegmentTermDocs(SegmentReader parent)

          throws IOException {

    this.parent = parent;

    this.freqStream = (InputStream) parent.freqStream.clone();//频率文件
    this.deletedDocs = parent.deletedDocs;

    this.skipInterval = parent.tis.getSkipInterval();

  }

SegmentTermPositions::SegmentTermPositions(SegmentReader p) throws IOException {

    super(p);

    this.proxStream = (InputStream)parent.proxStream.clone();//位置文件

  }
 

IndexReader  = SegmentReader, IndexSearcher
termPositions.seek(term);

 
SegmentTermDocs::public void seek(Term term) throws IOException {

TermInfo ti = parent.tis.get(term);// parent =SegmentReader

// tis =TermInfosReader

// 在初始化SegmentTermDocs的时候读取文件并创建了

//  tis = new TermInfosReader(cfsDir, segment, fieldInfos);
/**

   * 1。从.tis文件中读取相关的信息 到 项的迭代对象

   * 2。得到项的迭代对象

   * 3。该项读取器 的 size = 该项迭代对象的 size

   * 4。读取索引,初始化了 索引指针,索引

   * */
    seek(ti);

  }
 

 
 
return termPositions;

<!--[if !supportLists]-->²         <!--[endif]-->SegmentReader. termPositions()::return SegmentTermPositions(this)`
 
 
 
 
<p>一个权重由query创建,并给查询器({@link Query#createWeight(Searcher)})使用,方法 {@link #sumOfSquaredWeights()},然后被最高级的查询api调用

用来计算查询规范化因子 (@link Similarity#queryNorm(float)}),然后该因子传给{@link #normalize(float)} 然后被{@link #scorer(IndexReader)}调用
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值