首页/数据/NoSQL 速查/constant_score 与 boosting 打分调整
Elasticsearch89 条

constant_score 与 boosting 打分调整

constant_score 把过滤条件包成固定分值查询,避免无意义的评分计算;boosting 通过 positive/negative 给匹配项降权,让不想要的结果沉底而不是被排除。

NoSQL 速查数据更新于 2026-10-12
constant_score子查询不评分、统一给 boost 值,等价高分过滤
boostingpositive 命中加分、negative 命中降权,negative_boost 控制降幅
适用排序需固定权重、或想让某类文档排后但不排除

示例

示例
GET /my-index/_search
{
  "query": {
    "constant_score": {
      "filter": { "term": { "user.id": "kimchy" } },
      "boost": 1.2
    }
  }
}

GET /my-index/_search
{
  "query": {
    "boosting": {
      "positive": { "match": { "title": "elastic" } },
      "negative": { "term": { "tier": "archived" } },
      "negative_boost": 0.2
    }
  }
}
纯过滤场景优先用 bool.filter,它不评分且匹配结果可缓存;constant_score 适合需要固定 boost 的场合。

易错点

  • boosting 只是降权不淘汰,真正要排除的条件应放到 must_not 或 filter
  • boost 写在小数位过多时对排名影响难感知,调整前先用 explain 看分差

数据来源与参考

  • Elasticsearch Guide: Constant score query
  • Elasticsearch Guide: Boosting query
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌