`
forfuture1978
  • 浏览: 412902 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Lucene学习总结之七:Lucene搜索过程解析(4)

阅读更多

2.4、搜索查询对象

 

2.4.1.2、创建Weight对象树

BooleanQuery.createWeight(Searcher) 最终返回return new BooleanWeight(searcher),BooleanWeight构造函数的具体实现如下:

 

public BooleanWeight(Searcher searcher) {

  this.similarity = getSimilarity(searcher);

  weights = new ArrayList<Weight>(clauses.size());

  //也是一个递归的过程,沿着新的Query对象树一直到叶子节点

  for (int i = 0 ; i < clauses.size(); i++) {

    weights.add(clauses.get(i).getQuery().createWeight(searcher));

  }

}

对于TermQuery的叶子节点,其TermQuery.createWeight(Searcher) 返回return new TermWeight(searcher)对象,TermWeight构造函数如下:

 

public TermWeight(Searcher searcher) {

  this.similarity = getSimilarity(searcher);

  //此处计算了idf

  idfExp = similarity.idfExplain(term, searcher);

  idf = idfExp.getIdf();

}

//idf的计算完全符合文档中的公式:

 

public IDFExplanation idfExplain(final Term term, final Searcher searcher) {

  final int df = searcher.docFreq(term);

  final int max = searcher.maxDoc();

  final float idf = idf(df, max);

  return new IDFExplanation() {

      public float getIdf() {

        return idf;

      }};

}

public float idf(int docFreq, int numDocs) {

  return (float)(Math.log(numDocs/(double)(docFreq+1)) + 1.0);

}

而ConstantScoreQuery.createWeight(Searcher) 除了创建ConstantScoreQuery.ConstantWeight(searcher)对象外,没有计算idf。

由此创建的Weight对象树如下:

 

weight    BooleanQuery$BooleanWeight  (id=169)   
   |   similarity    DefaultSimilarity  (id=177)   
   |   this$0    BooleanQuery  (id=89)   
   |   weights    ArrayList<E>  (id=188)   
   |      elementData    Object[3]  (id=190)   
   |------[0]    BooleanQuery$BooleanWeight  (id=171)   
   |          |   similarity    DefaultSimilarity  (id=177)   
   |          |   this$0    BooleanQuery  (id=105)   
   |          |   weights    ArrayList<E>  (id=193)   
   |          |      elementData    Object[2]  (id=199)   
   |          |------[0]    ConstantScoreQuery$ConstantWeight  (id=183)   
   |          |               queryNorm    0.0   
   |          |               queryWeight    0.0   
   |          |               similarity    DefaultSimilarity  (id=177)   

   |          |               //ConstantScore(contents:apple*)  
   |          |               this$0    ConstantScoreQuery  (id=123)   
   |          |------[1]    TermQuery$TermWeight  (id=175)   
   |                         idf    2.0986123   
   |                         idfExp    Similarity$1  (id=241)   
   |                         queryNorm    0.0   
   |                         queryWeight    0.0   
   |                         similarity    DefaultSimilarity  (id=177)   

   |                         //contents:boy
   |                        this$0    TermQuery  (id=124)   
   |                         value    0.0   
   |                 modCount    2   
   |                 size    2   
   |------[1]    BooleanQuery$BooleanWeight  (id=179)   
   |          |   similarity    DefaultSimilarity  (id=177)   
   |          |   this$0    BooleanQuery  (id=110)   
   |          |   weights    ArrayList<E>  (id=195)   
   |          |      elementData    Object[2]  (id=204)   
   |          |------[0]    ConstantScoreQuery$ConstantWeight  (id=206)   
   |          |               queryNorm    0.0   
   |          |               queryWeight    0.0   
   |          |               similarity    DefaultSimilarity  (id=177)   

   |          |               //ConstantScore(contents:cat*)
   |          |               this$0    ConstantScoreQuery  (id=135)   
   |          |------[1]    TermQuery$TermWeight  (id=207)   
   |                         idf    1.5389965   
   |                         idfExp    Similarity$1  (id=210)   
   |                         queryNorm    0.0   
   |                         queryWeight    0.0   
   |                         similarity    DefaultSimilarity  (id=177)

   |                         //contents:dog
   |                         this$0    TermQuery  (id=136)   
   |                         value    0.0   
   |                 modCount    2   
   |                 size    2   
   |------[2]    BooleanQuery$BooleanWeight  (id=182)   
              |  similarity    DefaultSimilarity  (id=177)   
              |  this$0    BooleanQuery  (id=113)   
              |  weights    ArrayList<E>  (id=197)   
              |     elementData    Object[2]  (id=216)   
              |------[0]    BooleanQuery$BooleanWeight  (id=181)   
              |          |    similarity    BooleanQuery$1  (id=220)   
              |          |    this$0    BooleanQuery  (id=145)   
              |          |    weights    ArrayList<E>  (id=221)   
              |          |      elementData    Object[2]  (id=224)   
              |          |------[0]    TermQuery$TermWeight  (id=226)   
              |          |                idf    2.0986123   
              |          |                idfExp    Similarity$1  (id=229)   
              |          |                queryNorm    0.0   
              |          |                queryWeight    0.0   
              |          |                similarity    DefaultSimilarity  (id=177)   

              |          |                //contents:eat
              |          |                this$0    TermQuery  (id=150)   
              |          |                value    0.0   
              |          |------[1]    TermQuery$TermWeight  (id=227)   
              |                          idf    1.1823215   
              |                          idfExp    Similarity$1  (id=231)   
              |                          queryNorm    0.0   
              |                          queryWeight    0.0   
              |                          similarity    DefaultSimilarity  (id=177)   

              |                          //contents:cat^0.33333325
              |                          this$0    TermQuery  (id=151)   
              |                          value    0.0   
              |                  modCount    2   
              |                  size    2   
              |------[1]    TermQuery$TermWeight  (id=218)   
                            idf    2.0986123   
                            idfExp    Similarity$1  (id=233)   
                            queryNorm    0.0   
                            queryWeight    0.0   
                            similarity    DefaultSimilarity  (id=177)   

                            //contents:foods
                            this$0    TermQuery  (id=154)   
                            value    0.0   
                    modCount    2   
                    size    2   
        modCount    3   
        size    3   

 

 

 

2.4.1.3、计算Term Weight分数

(1) 首先计算sumOfSquaredWeights

按照公式:

 

代码如下:

float sum = weight.sumOfSquaredWeights();

 

 

//可以看出,也是一个递归的过程

public float sumOfSquaredWeights() throws IOException {

  float sum = 0.0f;

  for (int i = 0 ; i < weights.size(); i++) {

    float s = weights.get(i).sumOfSquaredWeights();

    if (!clauses.get(i).isProhibited())

      sum += s;

  }

  sum *= getBoost() * getBoost();  //乘以query boost

  return sum ;

}

对于叶子节点TermWeight来讲,其TermQuery$TermWeight.sumOfSquaredWeights()实现如下:

 

public float sumOfSquaredWeights() {

  //计算一部分打分,idf*t.getBoost(),将来还会用到。

  queryWeight = idf * getBoost();

  //计算(idf*t.getBoost())^2

  return queryWeight * queryWeight;

}

对于叶子节点ConstantWeight来讲,其ConstantScoreQuery$ConstantWeight.sumOfSquaredWeights() 如下:

 

public float sumOfSquaredWeights() {

  //除了用户指定的boost以外,其他都不计算在打分内

  queryWeight = getBoost();

  return queryWeight * queryWeight;

}

(2) 计算queryNorm

其公式如下:

 

其代码如下:

 

public float queryNorm(float sumOfSquaredWeights) {

  return (float)(1.0 / Math.sqrt(sumOfSquaredWeights));

}

(3) 将queryNorm算入打分

代码为:

weight.normalize(norm);

 

//又是一个递归的过程

public void normalize(float norm) {

  norm *= getBoost();

  for (Weight w : weights) {

    w.normalize(norm);

  }

}

其叶子节点TermWeight来讲,其TermQuery$TermWeight.normalize(float) 代码如下:

 

public void normalize(float queryNorm) {

  this.queryNorm = queryNorm;

  //原来queryWeight为idf*t.getBoost(),现在为queryNorm*idf*t.getBoost()。

  queryWeight *= queryNorm;

  //打分到此计算了queryNorm*idf*t.getBoost()*idf = queryNorm*idf^2*t.getBoost()部分。

  value = queryWeight * idf;

}

我们知道,Lucene的打分公式整体如下,到此计算了图中,红色的部分:

  • 大小: 8.3 KB
  • 大小: 18.4 KB
  • 大小: 11.9 KB
  • 大小: 17.1 KB
  • 大小: 24.7 KB
  • 大小: 37.8 KB
  • 大小: 52.1 KB
分享到:
评论

相关推荐

    Lucene 3.0 原理与代码分析完整版

    1.15 Lucene学习总结之七:Lucene搜索过程解析(4) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246 1.16 Lucene学习总结之七:Lucene搜索过程解析(5) . . . . . . . . . . . . ....

    lucene-6.5.0工具包

    官网的lucene全文检索引擎工具包,下载后直接解压缩即可使用

    IKAnalyzer中文分词支持lucene6.5.0版本

    由于林良益先生在2012之后未对IKAnalyzer进行更新,后续lucene分词接口发生变化,导致不可使用,所以此jar包支持lucene6.0以上版本

    lucene-query-parser:Lucene查询字符串解析器用作Web api查询或过滤器字符串

    Lucene查询解析器 Lucene查询字符串解析器,用作Web api查询或过滤器字符串。 基本代码来自 使用这种语言的示例查询: name: apple price: &gt; 100 price: &gt; 100 AND active: = 1 product.price: &gt; 100 AND ...

    lucene-搜索过程源码解析-Score树

    lucene-搜索过程源码解析-Score树

    lucene-sequence-diagram:lucene搜索端uml时序图,lucene源码解析

    lucene搜索端uml时序图,lucene源码解析 图比较大,看不清,可以下载【sd-search.svg】后再用浏览器打开 使用starUML画图,可以下载【lucene.mdj】后打开,编辑 前提 只考虑最简单的查询,比如只对一个字段,用一个...

    经典的lucene实例代码及详细解析以及lucene结构流程介绍

    本文并给出一个经典的lucene全文收索例子代码。该例子功能是从磁盘文档建立索引,搜索该文档中的哪个TXT文件包含所搜索内容。最后再大致介绍Lucene的结构模块,应用流程希望对网友能有帮助。

    毕设 Lucene解析索引PDF文档的内容

    ----使用iText解析PDF 文档代码 PDFBoxHello.java ----------- --PDFBox测试代码 PDFBoxLuceneIndex.java ------ --PDFBox创建PDF文件的Lucene索引 PDFBoxPathIndex.java ------- --PDFBox创建指定目录PDF文档...

    基于lucene的搜索引擎总结

    Lucene搜索过程的核心类 IndexSearcher:用于搜索IndexWriter创建的索引 Term:用于搜索的一个基本单元包括了一对字符串元素,与Field相对应 Query :抽象的查询类 TermQuery:最基本的查询类型,用来匹配特定Field...

    Lucene搜索引擎开发权威经典 光盘

    Lucene搜索引擎开发权威经典 光盘 于天恩 著 中国铁道出版社出版 2008-10 这本书基于Lucene的当前最新版本(2.1)精解了Lucene搜索引擎的相关知识,从基础知识到应用开发,精练简洁,恰到好处。 本书共包括16章,...

    lucene-搜索过程源码解析-1-Weight生成.txt

    lucene-搜索过程源码解析-1-Weight生成.txt

    lucene搜索过程代码详解

    详细分析lucene搜索的实现过程,通过代码解析,会对lucene的搜索实现过程有一个更加深刻的认识

    Heritrix lucene开发自己的搜索引擎(源码)1

    Eclipse工程/ch7:原书第七章和第九章的Eclipse工程文件 使用PDFBox解析PDF文件 使用xpdf解析中文PDF文件 使用POI解析WORD和Excel文件 使用Jacob解析WORD文件 Google的Search API的使用 安装:直接在Eclipse中...

    Lucene中的FST算法描述

    描述了Lucene中如何使用FST算法构建term的内存索引,使用了很多图,直观的展现了FST图的构建流程,能够对想了解lucene内部实现机制原理的同学有帮助。

    开发自己的搜索引擎lucene and heritrix

    Eclipse工程/ch7:原书第七章和第九章的Eclipse工程文件 使用PDFBox解析PDF文件 使用xpdf解析中文PDF文件 使用POI解析WORD和Excel文件 使用Jacob解析WORD文件 Google的Search API的使用 安装:直接在Eclipse中...

    lucene例子

    Lucene 是一个开源、高度可扩展的搜索引擎库,可以从 Apache Software Foundation 获取。您可以将 Lucene 用于商业和开源应用程序。Lucene 强大的 API 主要关注文本索引和搜索。它可以用于为各种应用程序构建搜索...

    lucene2.9.1所有最新开发包及源码及文档

    开源全文搜索工具包Lucene2.9.1的使用。 1. 搭建Lucene的开发环境:在classpath中添加lucene-core-2.9.1.jar包 2. 全文搜索的两个工作: 建立索引文件,搜索索引. 3. Lucene的索引文件逻辑结构 1) 索引(Index)由...

    Lucene 源代码剖析.rar

    这是一篇公司的内部培训教材,其中中的内容涵盖LUCENE的方方面面,从源代码角度深入剖析LUCENE,如果要对LUCENE有更加深入的了解(专家级别),这篇技术文档必不可少。 前提:对LUCENE有一定程度的了解,否则会让你云...

    Lucene 源码解析

    FileReaderAll函数用来从文件中读取字符串,默认编码为“GBK”。在创建完最重要的IndexWriter之后,就开始遍历需要索引的文件,构造对应的Document和Filed类,最终通过IndexWriter的addDocument函数开始索引。...

    一个经典Lucene入门模块及例子解析

    Lucene的功能请打,方法众多。主要介绍了Lucene的功能模块及其调用代码,实际使用中可以具体修改。最后还有一个常见的Lucene实例与解析。

Global site tag (gtag.js) - Google Analytics