1. 程式人生 > >ElasticSearch教程——lucene的相關度評分TF&IDF演算法以及向量空間模型演算法

ElasticSearch教程——lucene的相關度評分TF&IDF演算法以及向量空間模型演算法

1、boolean model

類似and這種邏輯操作符,先過濾出包含指定term的doc

query "hello world" --> 過濾 --> hello / world / hello & world
bool --> must/must not/should --> 過濾 --> 包含 / 不包含 / 可能包含
doc --> 不打分數 --> 正或反 true or false --> 為了減少後續要計算的doc的數量,提升效能query: hello world
 

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

普通multivalue搜尋,轉換為bool搜尋,boolean model


2、TF/IDF演算法介紹

relevance score演算法,簡單來說,就是計算出,一個索引中的文字,與搜尋文字,他們之間的關聯匹配程度
Elasticsearch使用的是 term frequency/inverse document frequency演算法,簡稱為TF/IDF演算法
Term frequency:搜尋文字中的各個詞條在field文字中出現了多少次,出現次數越多,就越相關

搜尋請求: hello world --> doc.content

doc1: java is my favourite programming language, hello world !!!
doc2: hello java, you are very good, oh hello world!!!
Inverse document frequency:搜尋文字中的各個詞條在整個索引的所有文件中出現了多少次,出現的次數越多,就越不相關

搜尋請求:hello world

doc1:hello, today is very good
doc2:hi world, how are you

hello對doc1的評分

TF: term frequency 
找到hello在doc1中出現了幾次,1次,會根據出現的次數給個分數
一個term在一個doc中,出現的次數越多,那麼最後給的相關度評分就會越高

IDF:inversed document frequency
找到hello在所有的doc中出現的次數,3次
一個term在所有的doc中,出現的次數越多,那麼最後給的相關度評分就會越低

length norm
hello搜尋的那個field的長度,field長度越長,給的相關度評分越低; field長度越短,給的相關度評分越高

最後,會將hello這個term,對doc1的分數,綜合TF,IDF,length norm,計算出來一個綜合性的分數
hello world --> doc1 --> hello對doc1的分數,world對doc1的分數 --> 但是最後hello world query要對doc1有一個總的分數 --> vector space model

_score是如何被計算出來的
 

GET /test_index/test_type/_search?explain
{
  "query": {
    "match": {
      "test_field": "hello world"
    }
  }
}

"description": "tfNorm, computed as (freq * (k1 + 1)) / (freq + k1 * (1 - b + b * fieldLength / avgFieldLength)) from:"

3、vector space model

多個term生成的一個doc的總分數

hello world --> es會根據hello world在所有doc中的評分情況,計算出一個query vector,query向量
例如:
hello這個term,給的基於所有doc的一個評分就是2
world這個term,給的基於所有doc的一個評分就是5
query向量 就是 [2, 5]

query vector

doc vector,3個doc,一個包含1個term,一個包含另一個term,一個包含2個term

3個doc
doc1:包含hello --> 向量 [2, 0]
doc2:包含world --> 向量 [0, 5]
doc3:包含hello, world -->  向量[2, 5]

會給每一個doc,拿每個term經過lucene practical scoring function計算出一個分數來,hello有一個分數,world有一個分數,再拿所有term的分陣列成一個doc vector

畫在一個圖中,取每個doc vector對query vector的弧度,給出每個doc對多個term的總分數
-

每個doc vector計算出對query vector的弧度,最後基於這個弧度給出一個doc相對於query中多個term的總分數
弧度越大,分數月底; 弧度越小,分數越高

如果是多個term,那麼就是線性代數來計算,無法用圖表示

4、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) score(q,d) is the relevance score of document d for query q.
這個公式的最終結果,就是說是一個query(叫做q),對一個doc(叫做d)的最終的總評分

queryNorm(q) is the query normalization factor (new).
queryNorm,是用來讓一個doc的分數處於一個合理的區間內,不要太離譜,舉個例子,一個doc分數是10000,一個doc分數是0.1,你們說好不好,肯定不好

coord(q,d) is the coordination factor (new).
簡單來說,就是對更加匹配的doc,進行一些分數上的成倍的獎勵

The sum of the weights for each term t in the query q for document d.
∑:求和的符號

∑ (t in q):query中每個term,query = hello world,query中的term就包含了hello和world
query中每個term對doc的分數,進行求和,多個term對一個doc的分數,組成一個vector space,然後計算嗎,就在這一步

tf(t in d) is the term frequency for term t in document d.
計算每一個term對doc的分數的時候,就是TF/IDF演算法

idf(t) is the inverse document frequency for term t.
計算每一個term對doc的分數的時候,就是TF/IDF演算法

t.getBoost() is the boost that has been applied to the query (new).
增加權重

norm(t,d) is the field-length norm, combined with the index-time field-level boost, if any. (new).
搜尋出的field長度越長,給的相關度評分越低; field長度越短,給的相關度評分越高

query normalization factor
queryNorm = 1 / √sumOfSquaredWeights

sumOfSquaredWeights = 所有term的IDF分數之和,開一個平方根,然後做一個平方根分之1
主要是為了將分數進行規範化 --> 開平方根,首先資料就變小了 --> 然後還用1去除以這個平方根,分數就會很小 --> 1.幾 / 零點幾
分數就不會出現幾萬,幾十萬,那樣的離譜的分數

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,分數之間拉開差距


field level boost

可以將某個搜尋條件的權重加大

{  
          "match": {  
            "title": {  
              "query": "spark",  
              "boost": 5  
            }  
          }  
        }