注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

Koala++'s blog

计算广告学 RTB

 
 
 

日志

 
 

Lucene源代码分析[-6]  

2009-07-08 22:33:52|  分类: Lucene |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

         接上一篇,我们看BooleanScore2中的makeCountingSumScorer

/** Returns the scorer to be used for match counting and score summing.

 * Uses requiredScorers, optionalScorers and prohibitedScorers.

 */

private Scorer makeCountingSumScorer() {

// each scorer counted as a single matcher

    return (requiredScorers.size() == 0) ? makeCountingSumScorerNoReq()

           : makeCountingSumScorerSomeReq();

}

         看一下注释,返回的scorer是用于匹配计数和得分累记,这里我们用了两个MUST,所以requiredScorers size等于2。执行makeCountingSumScoreSomeReq

private Scorer makeCountingSumScorerSomeReq() {

    if (optionalScorers.size() < minNrShouldMatch) {

       return new NonMatchingScorer();

    } else if (optionalScorers.size() == minNrShouldMatch) {

       ArrayList allReq = new ArrayList(requiredScorers);

       allReq.addAll(optionalScorers);

       return addProhibitedScorers(

countingConjunctionSumScorer(allReq));

    } else {

       Scorer requiredCountingSumScorer = (requiredScorers.size() == 1)

? new SingleMatchScorer((Scorer) requiredScorers.get(0))

              : countingConjunctionSumScorer(requiredScorers);

       if (minNrShouldMatch > 0) {

           return addProhibitedScorers(dualConjunctionSumScorer(

                 

                  requiredCountingSumScorer,

                  countingDisjunctionSumScorer(optionalScorers,

                         minNrShouldMatch)));

       } else {

           return new ReqOptSumScorer(

              addProhibitedScorers(requiredCountingSumScorer),

              ((optionalScorers.size() == 1)

? new SingleMatchScorer((Scorer) optionalScorers.get(0))

              : countingDisjunctionSumScorer(optionalScorers,1)));

       }

    }

}

         刚才讲了requiredScorers size2,那么optionalScorers size就等于0,执行的是else if语句,看一下是countingConjunctionSumScorer

private static Similarity defaultSimilarity = new DefaultSimilarity();

 

private Scorer countingConjunctionSumScorer(List requiredScorers) {

    // each scorer from the list counted as a single matcher

    final int requiredNrMatchers = requiredScorers.size();

    ConjunctionScorer cs = new ConjunctionScorer(defaultSimilarity) {

       private int lastScoredDoc = -1;

 

       public float score() throws IOException {

           if (doc() > lastScoredDoc) {

              lastScoredDoc = doc();

              coordinator.nrMatchers += requiredNrMatchers;

           }

           // All scorers match, so defaultSimilarity

//super.score() always has 1 as the coordination factor.

           // Therefore the sum of the scores of the requiredScorers

           // is used as score.

           return super.score();

       }

    };

    Iterator rsi = requiredScorers.iterator();

    while (rsi.hasNext()) {

       cs.add((Scorer) rsi.next());

    }

    return cs;

}

         我们看到这里只是用ConjunctionScorer来保存,而且score被重写了,我们再看addProhibitedScorers

private Scorer addProhibitedScorers(Scorer requiredCountingSumScorer) {

    return (prohibitedScorers.size() == 0) ? requiredCountingSumScorer // no prohibited

           : new ReqExclScorer(

                  requiredCountingSumScorer,

                  ((prohibitedScorers.size() == 1)

? (Scorer) prohibitedScorers.get(0)

                   : new DisjunctionSumScorer(prohibitedScorers)));

}

         我们没有用MUST_NOT所以prohibitedScorers size0,那么直接返回requriedCountingSumScorer。回到score函数:

public void score(HitCollector hc) throws IOException {

    if (countingSumScorer == null) {

       initCountingSumScorer();

    }

    while (countingSumScorer.next()) {

       hc.collect(countingSumScorer.doc(), score());

    }

}

         看一下doc

public int doc() {

       return first().doc();

}

         再看一下score()

public float score() throws IOException {

    coordinator.initDoc();

    float sum = countingSumScorer.score();

    return sum * coordinator.coordFactor();

}

         Coordinator就是简单地把匹配上的词初始化为0,而这里的countingSumScorer是刚才在countingConjunctionSumScorer中重写的score,里面有一个super.score

public float score() throws IOException {

    float score = 0.0f; // sum scores

    Iterator i = scorers.iterator();

    while (i.hasNext())

       score += ((Scorer) i.next()).score();

    score *= coord;

    return score;

}

         sum还要乘以相应的coordFactor,差不多结束了。

         再提一下,比如我们搜索hellokoala都用MUST什么都搜索不到,为什么呢?因为合取的时候算分是用乘法算的,当然如果有一个找不到,乘出来就是0,所以什么也没有了。

 

 

  评论这张
 
阅读(1045)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017