Bootstrap

Elasticsearch 相关度评分

Elasticsearch 相关度评分,内容来自 B 站中华石杉 Elasticsearch 顶尖高手系列课程核心知识篇,本文内容似乎有些过时

TF&IDF 算法介绍

relevance score 算法,简单来说,就是计算出一个索引中的文本与搜索文本之间的关联匹配程度

Elasticsearch 使用的是 term frequency/inverse document frequency 算法,简称为 TF/IDF 算法。

Term frequency:搜索文本中的各个词条在 field 文本中出现了多少次,出现次数越多,就越相关

搜索请求:hello world

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

Inverse document frequency:搜索文本中的各个词条在整个索引的所有文档中出现了多少次,出现的次数越多,就越不相关

搜索请求:hello world

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

比如说,在 index 中有 1 万条 document,hello 这个单词在所有的 document 中,一共出现了 1000 次;world 这个单词在所有的 document 中,一共出现了 100 次

那么 doc2 更相关

Field-length norm:field 长度越长,相关度越弱

搜索请求:hello world

doc1:{ "title": "hello article", "content": "babaaba 1万个单词" }
doc2:{ "title": "my article", "content": "blablabala 1万个单词,hi world" }

hello world 在整个 index 中出现的次数是一样多的

doc1 更相关,因为 title field 更短

_score是如何被计算出来的

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

分析一个 document 是如何被匹配上的

GET /test_index/test_type/6/_explain
{
  "query": {
    "match": {
      "test_field": "test hello"
    }
  }
}