boolean modelTF/IDFvector space model
深入讲解TF/IDF算法,在lucene中,底层,到底进行TF/IDF算法计算的一个完整的公式是什么?

0、boolean model

  1. query: hello world
  2. "match": {
  3. "title": "hello world"
  4. }

与下面这个查询是匹配的:

"bool": {
    "should": [
        {
            "match": {
                "title": "hello"
            }
        },
        {
            "natch": {
                "title": "world"
            }
        }
    ]
}
"bool": {
  "should": [
    {
      "term": {
        "title": {
          "value": "hello"
        }
      }
    },
    {
      "term": {
        "title": {
          "value": "world"
        }
      }
    }
  ]
}

但是与这个查询是不匹配的:

"bool": {
  "should": [
    {"terms": {
      "title": [
        "hello",
        "world"
      ]
    }}
  ]
}

普通multivalue搜索,转换为bool搜索,boolean model

1、lucene practical scoring function

practical scoring function,来计算一个query对一个doc的分数的公式,该函数会使用一个公式来计算

score(q,d)  =  
            queryNorm(q)  
          · coord(q,d)    
          · ∑ (           
                tf(t in d)   
              · idf(t)2      
              · t.getBoost() 
              · norm(t,d)    
            ) (t in q)
score(q,d) is the relevance score of document d for query q. score(q,d)是文档d对查询q的相关性得分。

这个公式的最终结果,就是说是一个query(叫做q),对一个doc(叫做d)的最终的总评分

queryNorm(q) is the query normalization factor (new).  queryNorm(q)是查询归一化因子(new)。

queryNorm,是用来让一个doc的分数处于一个合理的区间内,不要太离谱,举个例子,一个doc分数是10000,一个doc分数是0.1,你们说好不好,肯定不好

coord(q,d) is the coordination factor (new). coord(q,d)是协调系数。

简单来说,就是对更加匹配的doc,进行一些分数上的成倍的奖励

The sum of the weights for each term t in the query q for document d.  文档 d 的查询 q 中每个 term t 的权重之和。

∑:求和的符号

∑ (t in q):query中每个term,query = hello world,query中的term就包含了hello和world

query中每个termdoc的分数,进行求和,多个term对一个doc的分数,组成一个vector space,然后计算吗,就在这一步

tf(t in d) is the term frequency for term t in document d.  tf(t in d)是 term t在文档d中的 term 频率。

计算每一个termdoc的分数的时候,就是TF/IDF算法

idf(t) is the inverse document frequency for term t.  idf(t)是 term t 的倒排文档频率。

t.getBoost() is the boost that has been applied to the query (new).  t.getBoost()是已经应用于查询的boost。

norm(t,d) is the field-length norm, combined with the index-time field-level boost, if any. (new).

2、query normalization factor

queryNorm = 1 / √sumOfSquaredWeights

sumOfSquaredWeights = 所有term的IDF分数之和,开一个平方根,然后做一个平方根分之1
主要是为了将分数进行规范化 —> 开平方根,首先数据就变小了 —> 然后还用1去除以这个平方根,分数就会很小 —> 1.几 / 零点几
分数就不会出现几万,几十万,那样的离谱的分数

3、query coodination

奖励那些匹配更多字符的doc更多的分数

Document 1 with hello → score: 1.5
Document 2 with hello world → score: 3.0
Document 3 with hello world java → score: 4.5

Document 1 with hello → score: 1.5 * 1 / 3 = 0.5
Document 2 with hello world → score: 3.0 * 2 / 3 = 2.0
Document 3 with hello world java → score: 4.5 * 3 / 3 = 4.5

把计算出来的总分数 * 匹配上的term数量 / 总的term数量,让匹配不同term/query数量的doc,分数之间拉开差距

4、field level boost

提高权重
https://blog.csdn.net/starzhou/article/details/51543209