拓冰建站拓冰建站
首页 / 资讯中心 / 正文

第五阶段 41 · 相关性调优(boosting、function_score、filter vs query 上下文)

41 · 相关性调优boosting、function_score、filter vs query 上下文阶段第五阶段 / 进阶与实战主题_score怎么来的、怎么调以及 filter/query 上下文的性能差异1. 概念query 上下文算_score相关性打分回答「有多匹配」。filter 上下文只判断 yes/no不算分可缓存、更快回答「符不符合」。调分手段字段boost、boosting查询降权而非排除、function_score按业务规则改分。PostgreSQL 没有相关性评分概念这是 ES 相对 SQL 的核心增量能力。2. PostgreSQL 对照-- SQL 没有 _score只能靠 ORDER BY 表达优先级SELECT*,CASEWHENgeo_cdAPTHEN2ELSE1ENDASweightFROMsalesdataWHEREmaterialILIKE%thinkpad%ORDERBYweightDESC;3. ES DSLfilter 上下文不算分、可缓存GET salesdata_idx/_search { query: { bool: { must: [ { match: { material: notebook } } ], // 算分 filter: [ { term: { geo_cd: AP } } ] // 不算分、缓存 } } }字段 boost{ match: { material: { query: notebook, boost: 3 } } }function_score按业务改分GET salesdata_idx/_search { query: { function_score: { query: { match: { material: notebook } }, functions: [ { filter: { term: { geo_cd: AP } }, weight: 2 }, { field_value_factor: { field: net_amount, modifier: log1p } } ], score_mode: sum, boost_mode: multiply } } }4. Spring Boot 实现ComponentpublicclassDoc41Relevance{AutowiredprivateElasticsearchClientelasticsearchClient;/** must 算分 filter 精确过滤不算分、可缓存 */publicListMapString,ObjectsearchScored(StringindexName,Stringkw,Stringgeo)throwsIOException{SearchResponseMaprespelasticsearchClient.search(s-s.index(indexName).query(q-q.bool(b-b.must(m-m.match(mt-mt.field(material).query(kw).boost(3f))).filter(f-f.term(t-t.field(geo_cd).value(geo))))),Map.class);returnresp.hits().hits().stream().map(Hit::source).filter(Objects::nonNull).collect(Collectors.toList());}}想看每条为什么这么打分可用 explain.explain(true)或_explainAPI 调试。5. 坑与最佳实践精确条件放 filter等值/范围/枚举放filter省去打分开销并利用缓存。只想排序不想过滤用should/boostingboosting能降权而不是直接排除。function_score谨慎脚本/函数打分贵boost_mode/score_mode要选对。别对海量结果排相关性能用业务字段排序日期、金额就别都靠_score。调分先 explain打分不符预期时用 explain 看每项贡献别盲改。
分享:

看完干货,该让你的企业上线了

免费需求沟通 · 48 小时内出具建站方案 · 河南本地可上门