4.3-搜索的相关性算分

相关性和相关性算分

  • 相关性 – Relevance

    • 搜索的相关性算分,描述了⼀个⽂档和查询语句匹配的程度。ES 会对每个匹配查询条件的结搜索的相关性算分_score

    • 打分的本质是排序,需要把最符合⽤户需求的⽂档排在前⾯。ES 5 之前,默认的相关性算分 采⽤ TF-IDF,现在采⽤ BM 25

词(Term) 文档(Doc Id)
区块链 1,2,3
2,3,4,5,6,7,8,10,12,13,15,18,19,20
应用 2,3,8,9,10,13,15

词频 TF

  • Term Frequency:检索词在⼀篇⽂档中出现的频率

    • 检索词出现的次数除以⽂档的总字数
  • 度量⼀条查询和结果⽂档相关性的简单⽅法:简单将搜索中每⼀个 词的 TF 进⾏相加

    • TF(区块链) + TF(的) + TF(应⽤)
  • Stop Word

    • “的” 在⽂档中出现了很多次,但是对贡献相关度⼏乎没有⽤处,不应该考虑他们的 TF

逆⽂档频率 IDF

  • DF:检索词在所有⽂档中出现的频率

    • “区块链”在相对⽐较少的⽂档中出现

    • “应⽤”在相对⽐较多的⽂档中出现

    • “Stop Word” 在⼤量的⽂档中出现

  • Inverse Document Frequency :简单说 = log(全部⽂档数/检索词出现过的⽂档总数)

  • TF-IDF 本质上就是将 TF 求和变成了加权求和

    • TF(区块链)*IDF(区块链) + TF(的)*IDF(的)+ TF(应⽤)*IDF(应⽤)
出现的文档数 总文档数 IDF
区块链 200万 10亿 log(500) = 8.96
10亿 10亿 log(500) = 0
应用 5亿 10亿 log(500) = 1

TF-IDF 的概念

  • TF-IDF 被公认为是信息检索领域最重要的发明

  • 除了在信息检索,在⽂献分类和其他相关领域有着⾮常⼴泛的应⽤

  • IDF 的概念,最早是剑桥⼤学的“斯巴克.琼斯”提出

    • 1972年 – “关键词特殊性的统计解释和它在⽂献检索中的应⽤”

    • 但是没有从理论上解释 IDF 应该是⽤ log(全部⽂档数/检索词出现过的⽂档总数),⽽不是其他函数。也没有做进⼀步的研究

  • 1970,1980年代萨尔顿和罗宾逊,进⾏了进⼀步的证明和研究,并⽤⾹农信息论做了证明

    • http://www.staff.city.ac.uk/~sb317/papers/foundations_bm25_review.pdf
  • 现代搜索引擎,对 TF-IDF 进⾏了⼤量细微的优化

Lucene 中的 TF-IDF 评分公式

TF-IDF 评分公式

BM 25

BM 25
  • 从 ES 5 开始,默认算法改为 BM 25

  • 和经典的TF-IDF相⽐,当 TF ⽆限增加时,BM 25算分会趋于⼀个数值

定制 Similarity

定制Similarity
  • K 默认值是 1.2,数值越⼩,饱和度越⾼,b 默认值是 0.75(取值范围 0-1),0 代表禁⽌ Normalization

通过 Explain API 查看 TF-IDF

PUT testscore/_bulk
{ "index": { "_id": 1 }}
{ "content":"we use Elasticsearch to power the search" }
{ "index": { "_id": 2 }}
{ "content":"we like elasticsearch" }
{ "index": { "_id": 3 }}
{ "content":"The scoring of documents is caculated by the scoring formula" }
{ "index": { "_id": 4 }}
{ "content":"you know, for search" }



POST /testscore/_search
{
  //"explain": true,
  "query": {
    "match": {
      "content":"you"
      //"content": "elasticsearch"
      //"content":"the"
      //"content": "the elasticsearch"
    }
  }
}
  • 4 篇⽂档 + 4 个 Term 查询

  • 思考⼀下

  • 查询中的 TF 和 IDF ?

  • 结果如何排序?

  • ⽂档⻓短 /TF / IDF 对相关度算分的影响

Boosting Relevance

POST testscore/_search
{
    "query": {
        "boosting" : {
            "positive" : {
                "term" : {
                    "content" : "elasticsearch"
                }
            },
            "negative" : {
                 "term" : {
                     "content" : "like"
                }
            },
            "negative_boost" : 0.2
        }
    }
}
  • Boosting 是控制相关度的⼀种⼿段

    • 索引,字段 或查询⼦条件
  • 参数 boost的含义

    • 当 boost > 1 时,打分的相关度相对性提升

    • 当 0 < boost < 1 时,打分的权重相对性降低

    • 当 boost < 0 时,贡献负分

本节知识点回顾

  • 什么是相关性 & 相关性算分介绍

  • TF-IDF / BM25

  • 在 Elasticsearch 中定制相关度算法的参数

  • ES 中可以对索引,字段分别设置 Boosting 参数

课程demo

PUT testscore
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "content": {
        "type": "text"
      }
    }
  }
}


PUT testscore/_bulk
{ "index": { "_id": 1 }}
{ "content":"we use Elasticsearch to power the search" }
{ "index": { "_id": 2 }}
{ "content":"we like elasticsearch" }
{ "index": { "_id": 3 }}
{ "content":"The scoring of documents is caculated by the scoring formula" }
{ "index": { "_id": 4 }}
{ "content":"you know, for search" }



POST /testscore/_search
{
  "explain": true,
  "query": {
    "match": {
      "content":"you"
      //"content": "elasticsearch"
      //"content":"the"
      //"content": "the elasticsearch"
    }
  }
}

POST testscore/_search
{
    "query": {
        "boosting" : {
            "positive" : {
                "term" : {
                    "content" : "elasticsearch"
                }
            },
            "negative" : {
                 "term" : {
                     "content" : "like"
                }
            },
            "negative_boost" : 0.2
        }
    }
}

你可能感兴趣的:(4.3-搜索的相关性算分)