当前位置: 首页 > news >正文

elasticsearch学习笔记(三十)——Elasticsearch 相关度评分 TFIDF算法

Elasticsearch的相关度评分(relevance score)算法采用的是term frequency/inverse document frequency算法,简称为TF/IDF算法。

算法介绍

relevance score算法,简单来说就是,就是计算出一个索引中的文本,与搜索文本,它们之间的关联匹配程度。
TF/IDF算法,分为两个部分,IF 和IDF
Term Frequency(TF): 搜索文本中的各个词条在field文本中出现了多少次,出现的次数越多,就越相关

例如:
搜索请求:hello world
doc1: hello you, and world is very good
doc2: hello, how are you
那么此时根据TF算法,doc1的相关度要比doc2的要高

Inverse Document Frequency(IDF): 搜索文本中的各个词条在整个索引的所有文档中出现的次数,出现的次数越多,就越不相关。

搜索请求: hello world
doc1: hello, today is very good.
doc2: hi world, how are you.
比如在index中有1万条document, hello这个单词在所有的document中,一共出现了1000次,world这个单词在所有的document中一共出现100次。那么根据IDF算法此时doc2的相关度要比doc1要高。

对于ES还有一个Field-length norm

field-length norm就是field长度越长,相关度就越弱
搜索请求:hello world
doc1: {"title": "hello article", "content": "1万个单词"}
doc2: {"title": "my article", "content": "1万个单词, hi world"}
此时hello world在整个index中出现的次数是一样多的。但是根据Field-length norm此时doc1比doc2相关度要高。因为title字段更短。

_score是如何被计算出来的

GET /test_index/_search?explain=true { "query": { "match": { "test_field": "hello" } } } { "took" : 9, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 2, "relation" : "eq" }, "max_score" : 0.20521778, "hits" : [ { "_shard" : "[test_index][0]", "_node" : "P-b-TEvyQOylMyEcMEhApQ", "_index" : "test_index", "_type" : "_doc", "_id" : "2", "_score" : 0.20521778, "_source" : { "test_field" : "hello, how are you" }, "_explanation" : { "value" : 0.20521778, "description" : "weight(test_field:hello in 0) [PerFieldSimilarity], result of:", "details" : [ { "value" : 0.20521778, "description" : "score(freq=1.0), product of:", "details" : [ { "value" : 2.2, "description" : "boost", "details" : [ ] }, { "value" : 0.18232156, "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:", "details" : [ { "value" : 2, "description" : "n, number of documents containing term", "details" : [ ] }, { "value" : 2, "description" : "N, total number of documents with field", "details" : [ ] } ] }, { "value" : 0.5116279, "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:", "details" : [ { "value" : 1.0, "description" : "freq, occurrences of term within document", "details" : [ ] }, { "value" : 1.2, "description" : "k1, term saturation parameter", "details" : [ ] }, { "value" : 0.75, "description" : "b, length normalization parameter", "details" : [ ] }, { "value" : 4.0, "description" : "dl, length of field", "details" : [ ] }, { "value" : 5.5, "description" : "avgdl, average length of field", "details" : [ ] } ] } ] } ] } }, { "_shard" : "[test_index][0]", "_node" : "P-b-TEvyQOylMyEcMEhApQ", "_index" : "test_index", "_type" : "_doc", "_id" : "1", "_score" : 0.16402164, "_source" : { "test_field" : "hello you, and world is very good" }, "_explanation" : { "value" : 0.16402164, "description" : "weight(test_field:hello in 0) [PerFieldSimilarity], result of:", "details" : [ { "value" : 0.16402164, "description" : "score(freq=1.0), product of:", "details" : [ { "value" : 2.2, "description" : "boost", "details" : [ ] }, { "value" : 0.18232156, "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:", "details" : [ { "value" : 2, "description" : "n, number of documents containing term", "details" : [ ] }, { "value" : 2, "description" : "N, total number of documents with field", "details" : [ ] } ] }, { "value" : 0.40892193, "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:", "details" : [ { "value" : 1.0, "description" : "freq, occurrences of term within document", "details" : [ ] }, { "value" : 1.2, "description" : "k1, term saturation parameter", "details" : [ ] }, { "value" : 0.75, "description" : "b, length normalization parameter", "details" : [ ] }, { "value" : 7.0, "description" : "dl, length of field", "details" : [ ] }, { "value" : 5.5, "description" : "avgdl, average length of field", "details" : [ ] } ] } ] } ] } } ] } }

匹配的文档有两个,下面直接用一个文档来分析出ES各个算法的公式。
从上面可以看出第一个文档的相关度分数是0.20521778

{ "_shard" : "[test_index][0]", "_node" : "P-b-TEvyQOylMyEcMEhApQ", "_index" : "test_index", "_type" : "_doc", "_id" : "2", "_score" : 0.20521778, "_source" : { "test_field" : "hello, how are you" }, "_explanation" : { "value" : 0.20521778, "description" : "weight(test_field:hello in 0) [PerFieldSimilarity], result of:", "details" : [ { "value" : 0.20521778, "description" : "score(freq=1.0), product of:", "details" : [ { "value" : 2.2, "description" : "boost", "details" : [ ] }, { "value" : 0.18232156, "description" : "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:", "details" : [ { "value" : 2, "description" : "n, number of documents containing term", "details" : [ ] }, { "value" : 2, "description" : "N, total number of documents with field", "details" : [ ] } ] }, { "value" : 0.5116279, "description" : "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:", "details" : [ { "value" : 1.0, "description" : "freq, occurrences of term within document", "details" : [ ] }, { "value" : 1.2, "description" : "k1, term saturation parameter", "details" : [ ] }, { "value" : 0.75, "description" : "b, length normalization parameter", "details" : [ ] }, { "value" : 4.0, "description" : "dl, length of field", "details" : [ ] }, { "value" : 5.5, "description" : "avgdl, average length of field", "details" : [ ] } ] } ] } ] } }
通过观察我们可以知道 _score = boost * idf * tf 此时boost = 2.2, idf = 0.18232156, tf = 0.5116279 idf = log(1 + (N - n + 0.5) / (n + 0.5)) 此时n = 2 (n, number of documents containing term), N = 2(N, total number of documents with field) tf = freq / (freq + k1 * (1 - b + b * dl / avgdl)) 此时freq = 1(freq, occurrences of term within document), k1 = 1.2(k1, term saturation parameter), b = 0.75(b, length normalization parameter), d1 = 4 (dl, length of field), avgdl = 5.5(avgdl, average length of field)
http://www.jsqmd.com/news/1163532/

相关文章:

  • 不止表层内容优化:千眼智推依托智推时代GEO引擎重构家居AI流量逻辑 - 资讯纵览
  • Python进阶:enumerate函数的索引加值遍历
  • 告别“懂行业无技术”困境:千眼智推依托智推时代打造垂直GEO服务闭环 - 资讯纵览
  • 2026北京上门收酒行业深度测评!6大头部商家真实口碑大盘点 - 深鉴新闻
  • STM32F405ZG与MCP3428高精度数据采集系统设计
  • ERP系统升级,让供应链管理焕发生机
  • 2026年保定GEO系统贴牌服务商靠谱源头厂商综合实力推荐:本地合伙人如何选对源头工厂、走通OEM贴牌与技术加盟路径? - 子柔传媒
  • QCheckbox复选框太小 设置不生效问题
  • Penpot云原生架构深度解析:构建可扩展的设计协作平台
  • 长沙哪里回收黄金,这家提供上门服务 - 小蝶回收测评
  • 主流售后服务软件盘点:企业数字化售后选型指南
  • GEO 技术入门:大模型如何检索、理解并引用你的内容
  • 安卓免费点播电影、动漫、动画片软件APP 云帧享 高清影视聚合在线播放器
  • 面试一些内容准备ai
  • 亚马逊75字符标题新规将落地,用卖家精灵关键词功能轻松应对(含卖家精灵优惠折扣码) - 跨境电商卖家出海
  • Transporter技术革命:突破传统ETL瓶颈的轻量级数据同步方案
  • elasticsearch学习笔记(二十九)——Elasticsearch 将一个field索引两次来解决字符串排序问题
  • 2026年7月庆阳​市公共卫生许可证检测集中空调通风系统检测水质检测空气质量甲醛检测怎么做?正规CMA机构办理指南 - 绿呼吸检测中心
  • 意念控制机器人:从脑机接口到远程意念指令的无限可能
  • 神器“Knockoff”扩展程序:精准识别亚马逊“字母汤”品牌,按需处理搜索结果
  • 2026年7月宁波​市公共卫生许可证检测集中空调通风系统检测水质检测空气质量甲醛检测怎么做?正规CMA机构办理指南 - 绿呼吸检测中心
  • 2026广东功效洗护生产厂家推荐:洗护赛道硬实力深度拆解
  • 【北京联合大学本科毕业论文】基于Java的车间生产任务调度管理系统设计与实现
  • 密码遗忘困扰你?3步掌握LaZagne找回丢失的凭据
  • HarmonyOS 6.1 组件生命周期全解剖 — aboutToAppear到aboutToRecycle的执行时序
  • 广州天河珠江新城高端搬家公司怎么选?正规合规搬迁服务商推荐指南 - 从来都是英雄出少年
  • 2026年7月最新北京伯爵官方售后客户服务热线与维修网点地址汇总 - 亨得利钟表维修中心
  • 鸿蒙ArkTS泛型进阶以及命名空间
  • AI:AI写的诽谤文章,我可以告谁?
  • WarcraftHelper:魔兽争霸3终极兼容性修复工具,三步解决现代系统运行难题