25_ElasticSearch 揭秘lucene的相关度分数算法

25_ElasticSearch 揭秘lucene的相关度分数算法

更多干货

  • 分布式实战(干货)
  • spring cloud 实战(干货)
  • mybatis 实战(干货)
  • spring boot 实战(干货)
  • React 入门实战(干货)
  • 构建中小型互联网企业架构(干货)
  • python 学习持续更新
  • ElasticSearch 笔记

一、概述

  • 一个搜索引擎使用的时候必定需要排序这个模块,一般情况下在不选择按照某一字段排序的情况下,都是按照打分的高低进行一个默认排序的,所以如果正式使用的话,必须对默认排序的打分策略有一个详细的了解
  • 对TF/IDF算法,在lucene中,底层,到底进行TF/IDF算法计算的一个完整的公式进行说明

ES官网给出的打分公式:

score(q,d)  =  
            queryNorm(q)  
          · coord(q,d)    
          · ∑ (           
                tf(t in d)   
              · idf(t)2      
              · t.getBoost() 
              · norm(t,d)    
            ) (t in q) 
  • t 表示term
  • q 表示 query 表示一个queue. 一个query 里有多个term
  • d 表示 doc 文档
  • score(q,d)计算分数的算法. 表示计算一个query对一个doc的分数的公式
  • queryNorm将分数进行规范化,使分数不会太高。不影响排序
  • coord() 就是对更加匹配的doc,进行一些分数上的成倍的奖励(奖励那些匹配更多字符的doc更多的分数)
  • norm() 根据 field-length 进行分数上的奖励。field越短,如果召回的话权重越大
  • t.getBoost 获取每一个term的权值

二、例子

  • 使用hello world 关键字进行查询
  • 普通multivalue搜索,转换为bool搜索,boolean model
"bool": {
	"should": [
		{
			"match": {
				"title": "hello"
			}
		},
		{
			"natch": {
				"title": "world"
			}
		}
	]
}

二、算法解释

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) 

2、公式解释

  • 1、score(q,d) score(q,d) is the relevance score of document d for query q.

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

  • 2、queryNorm(q) is the query normalization factor (new).

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

  • 3、coord(q,d) is the coordination factor (new).

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

  • 4、The sum of the weights for each term t in the query q for document d.

  • 求和

3、对求进行解释

	∑ (           
		tf(t in d)   
	  · idf(t)2      
	  · t.getBoost() 
	  · norm(t,d)    
	) (t in q) 
  • 1、∑:求和的符号

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

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

  • 4、tf(t in d) is the term frequency for term t in document d.

  • 计算每一个term对doc的分数的时候,就是TF/IDF算法

  • 5、idf(t) is the inverse document frequency for term t.

  • 计算IDF算法

  • 6、t.getBoost() is the boost that has been applied to the query (new).

  • getBoost表明该field的权值越大,越重要

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

  • 字段长度归约是为了让内容较短的字段发挥更大的作用,而内容较长的字段权重相对降低

4、queryNorm(query normalization factor)

是用来让一个doc的分数处于一个合理的区间内,不要太离谱。不影响排序

公式:

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

5、coord (query coodination)

  • 奖励那些匹配更多字符的doc更多的分数
  • 把计算出来的总分数 * 匹配上的term数量 / 总的term数量,让匹配不同term/query数量的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

6、field level boost

相关文章

  • ElasticSearch 笔记

  • 1_ElasticSearch使用term filter来搜索数据

  • 2_ElasticSearch filter执行原理 bitset机制与caching机制

  • 3_ElasticSearch 基于bool组合多个filter条件来搜索数据

  • 4_ElasticSearch 使用terms搜索多个值

  • 5_ElasticSearch 基于range filter来进行范围过滤

  • 6_ElasticSearch 控制全文检索结果的精准度

  • 7_ElasticSearch term+bool实现的multiword搜索原理

  • 8_基于boost的搜索条件权重控制

  • 9_ElasticSearch 多shard场景下relevance score不准确

  • 10_ElasticSearch dis_max实现best fields策略进行多字段搜索

  • 11_ElasticSearch 基于tie_breaker参数优化dis_max搜索效果

  • 12_ElasticSearch multi_match语法实现dis_max+tie_breaker

  • 13_ElasticSearch multi_match+most fiels策略进行multi-field搜索

  • 14_ElasticSearch 使用most_fields策略进行cross-fields search

  • 15_ElasticSearch copy_to定制组合field进行cross-fields搜索

  • 16_ElasticSearch 使用原生cross-fiels 查询

  • 17_ElasticSearch phrase matching搜索

  • 18_ElasticSearch 基于slop参数实现近似匹配

  • 19_ElasticSearch 使用match和近似匹配实现召回率与精准度的平衡

  • 20_ElasticSearch rescoring机制优化近似匹配搜索的性能

  • 21_ElasticSearch 前缀搜索、通配符搜索、正则搜索

  • 22_ElasticSearch 搜索推荐match_phrase_prefix实现search-time

  • 23_ElsaticSearch 搜索推荐ngram分词机制实现index-time更多干货

  • 24_ElasticSearch TF&IDF算法以及向量空间模型

  • 25_ElasticSearch 揭秘lucene的相关度分数算法

  • 日志管理ELK



你可能感兴趣的:(【构建高可用架构】,【ElatisSearch】,【大数据】)