【Elastic Search】默认搜索评分细则

发布时间:2026/8/9 5:18:53
【Elastic Search】默认搜索评分细则 默认搜索算法Elasticsearch 默认用 BM25 算法计算 _score核心依据这几个维度词频TF目标词条在单篇文档里出现的次数次数越高基础评分越高比如你提到的daiwa词频3651这部分权重本身有优势逆文档频率IDF目标词条在整个索引的所有文档里出现的频率越罕见的词评分权重越高文档长度文档越长相同词频的词条评分会被适当压低避免长文档占优势常规使用# Create an indexPUT /my-index# Add a document to my-indexPOST /my-index/_doc{id:park_rocky-mountain-to,title:Rocky Mountain,description:Bisected north to south by the Continental Divide, this portion of the Rockies has ecosystems varying from over 150 riparian lakes to montane and subalpine forests to treeless alpine tundra.}POST /my-index/_doc{id:park_rocky-jqwtbcext-to,title:Rocky jqwtbcext,description:Bisected north to south by the Continental Divide, this portion of the Rockies has ecosystems varying from over 150 riparian lakes to montane and subalpine forests to treeless alpine tundra. jack sds wqerwe qweqw Rocky}# Perform a search in my-indexGET /my-index/_search?qRockyGET /my-index/_explain/2X2M4J8BBCnQY8dSlK3y?qRocky解释细则{_index:my-index,_id:2X2M4J8BBCnQY8dSlK3y,matched:true,explanation:{value:0.18232156,description:max of:,details:[{value:0.18232156,description:weight(title:rocky in 0) [PerFieldSimilarity], result of:,details:[{value:0.18232156,description:score(freq1.0), computed as boost * idf * tf from:,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.45454544,description:tf, computed as freq / (freq k1 * (1 - b b * dl / avgdl)) from:,details:[{value:1,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:2,description:dl, length of field,details:[]},{value:2,description:avgdl, average length of field,details:[]}]}]}]}]}}boost (权重提升)值: 2.2含义: 这是一个查询时应用的提升因子。它可能来自查询时的设置例如 {“match”: {“title”: {“query”: “Rocky”, “boost”: 2.2}}}。这个值直接乘以最终的得分起到放大分数的作用。可以认为boost 是一个权重因子用于在查询时调整特定词条的重要性。idf (逆文档频率 - Inverse Document Frequency)值: 0.18232156含义: 衡量词项 “rocky” 在整个索引中是否罕见。越罕见的词IDF 值越高意味着它越能区分文档。计算公式:log(1(N−n0.5)/(n0.5))log(1 (N - n 0.5) / (n 0.5))log(1(N−n0.5)/(n0.5))n (包含词项的文档数): 2。说明在索引中有 2 个文档 的 title 字段包含了 “rocky”。N (包含该字段的文档总数): 2。说明索引中总共有 2 个文档 包含了 title 字段。分析: 这里的 n 和 N 都是 2意味着 “rocky” 这个词出现在 所有 文档中。因此它不是一个罕见表征词几乎不能区分文档所以计算出的 IDF 值 0.182 非常低。如果所有文档都包含某词IDF 趋近于 0。tf (词频 - Term Frequency)值: 0.45454544含义: 衡量词项 “rocky” 在当前文档中出现的频率。出现次数越多TF 值越高但 BM25 算法会对其进行“饱和”处理使词频带来的收益递减。计算公式:freq/(freqk1∗(1−bb∗dl/avgdl))freq / (freq k1 * (1 - b b * dl / avgdl))freq/(freqk1∗(1−bb∗dl/avgdl))freq (当前文档中词频): 1。表示在这个文档的 title 字段中“rocky” 只出现了 1 次。k1 (词频饱和参数): 1.2。BM25 的默认值控制词频达到多少后收益开始递减。b (长度归一化参数): 0.75。BM25 的默认值控制文档长度对得分的影响程度 (0 为无影响1 为完全影响)。dl (当前文档长度): 2。表示这个文档的 title 字段长度为 2可以理解为索引了 2 个词。avgdl (平均文档长度): 2。表示所有文档的 title 字段平均长度为 2。分析: 由于 dl 等于 avgdl长度归一化部分 (1 - b b * dl / avgdl) 计算出来是 1.0。所以 TF 的计算简化为 freq / (freq k1)即 1 / (1 1.2) ≈ 0.4545。这说明由于文档长度刚好等于平均值未受长度惩罚分数完全由词频贡献。最终得分把以上三个部分相乘得到最终得分最终得分 2.2 × 0.18232156 × 0.45454544 ≈ 0.18232156多关键词搜索类似的GET /my-index2/_explain/an2v4J8BBCnQY8dSqbTM?qtitle:Rocky OR content:Rocky我们会发现description: “sum of:”,代表最后的得分是多项得分之和。{_index:my-index2,_id:an2v4J8BBCnQY8dSqbTM,matched:true,explanation:{value:0.7773608,description:sum of:,details:[{value:0.10536051,description:weight(title:rocky in 0) [PerFieldSimilarity], result of:,details:[{value:0.10536051,description:score(freq1.0), computed as boost * idf * tf from:,details:[{value:2.2,description:boost,details:[]},{value:0.105360515,description:idf, computed as log(1 (N - n 0.5) / (n 0.5)) from:,details:[{value:4,description:n, number of documents containing term,details:[]},{value:4,description:N, total number of documents with field,details:[]}]},{value:0.45454544,description:tf, computed as freq / (freq k1 * (1 - b b * dl / avgdl)) from:,details:[{value:1,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:2,description:dl, length of field,details:[]},{value:2,description:avgdl, average length of field,details:[]}]}]}]},{value:0.6720003,description:weight(content:rocky in 0) [PerFieldSimilarity], result of:,details:[{value:0.6720003,description:score(freq1.0), computed as boost * idf * tf from:,details:[{value:2.2,description:boost,details:[]},{value:0.6931472,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:4,description:N, total number of documents with field,details:[]}]},{value:0.44067794,description:tf, computed as freq / (freq k1 * (1 - b b * dl / avgdl)) from:,details:[{value:1,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:35,description:dl, length of field,details:[]},{value:32.5,description:avgdl, average length of field,details:[]}]}]}]}]}}查询子句在 Elasticsearch 的查询语句DSL中几乎每一个 JSON 对象的大括号 { … }都可能是一个查询子句。看下面这个 bool 查询的例子{query:{bool:{// -- 这是“布尔查询容器”不算子句must:[{// -- 子句 1匹配标题match:{title:Rocky}},{// -- 子句 2匹配年份范围range:{year:{gte:1970}}}],should:[{match:{content:Rocky}},// -- 子句 3{match:{content:Balboa}},// -- 子句 4{match:{genre:Drama}}// -- 子句 5],minimum_should_match:2// -- 要求should至少匹配其中 2 个子句}}}should 列表中的每一个独立查询对象都是单独的子句。证据 1每个子句独立计算自己的 BM25 分数。证据 2最终得分是这些独立分数的累加sum。证据 3minimum_should_match 参数明确要求“至少匹配 N 个子句”证明了它们的存在独立性。常见的分数聚合方式在 Elasticsearch 中分数聚合Score Aggregation 指的是将多个查询子句的得分进行组合从而得出文档最终相关性得分的方式。min (取最小值)avg (平均值)sum (求和)max (取最大值)product (乘积)boolean (布尔组合)weight (加权) 和 custom_score (自定义分数)