NoSQL 与搜索 / 系统课时
布尔复合查询
布尔复合查询
本课用一个商品搜索示例,把全文匹配、精确筛选、偏好加权和结果排除组合到同一个 bool 查询中。完成后,你能够根据业务条件正确选择 must、filter、should 与 must_not,并验证最终命中文档。
学习目标
- 说明四类子句是否要求匹配、是否参与相关性评分
- 使用
minimum_should_match明确控制可选条件 - 组合多个条件并通过
_search响应验证结果
bool 查询解决什么问题
单个 match、term 或 range 查询只能表达一种条件。真实搜索通常同时包含关键词、分类、价格、状态和偏好标签,bool 查询负责把这些叶子查询组合成一个完整判断。
| 子句 | 逻辑作用 | 是否影响 _score |
典型用途 |
|---|---|---|---|
must |
所有条件都要满足 | 是 | 关键词、必须参与相关性计算的条件 |
filter |
所有条件都要满足 | 否 | 分类、价格、时间、权限等精确筛选 |
should |
满足时加分,或按规则要求至少命中若干项 | 是 | 标签偏好、字段加权、召回扩展 |
must_not |
命中任一条件就排除 | 否 | 下架、删除、黑名单数据 |
filter 和 must_not 在过滤上下文中执行,不计算相关性分数。对于只判断“是否符合”的条件,应优先放入 filter,避免让无意义的精确条件干扰排序。

构建一个完整查询
下面示例假设 Elasticsearch 可通过 http://localhost:9200 访问。脚本先创建索引和五条商品数据,再查找描述中包含 headphones、价格不高于 1000、未停用,并且至少具有一个偏好标签的商品。
set -euo pipefail
ES="${ES:-http://localhost:9200}"
# 重建索引,确保示例可以重复执行
curl -s -X DELETE "$ES/products" >/dev/null || true
curl -s -X PUT "$ES/products" \
-H 'Content-Type: application/json' \
-d '{
"mappings": {
"properties": {
"name": { "type": "keyword" },
"description": { "type": "text" },
"price": { "type": "integer" },
"status": { "type": "keyword" },
"tags": { "type": "keyword" }
}
}
}' >/dev/null
# 使用 NDJSON 一次写入覆盖所有分支的数据
curl -s -X POST "$ES/products/_bulk?refresh=true" \
-H 'Content-Type: application/x-ndjson' \
--data-binary $'{"index":{"_id":"1"}}\n{"name":"无线降噪耳机","description":"wireless noise cancelling headphones","price":899,"status":"active","tags":["wireless","noise-cancelling"]}\n{"index":{"_id":"2"}}\n{"name":"有线监听耳机","description":"wired studio headphones","price":499,"status":"active","tags":["wired"]}\n{"index":{"_id":"3"}}\n{"name":"无线蓝牙音箱","description":"wireless bluetooth speaker","price":399,"status":"active","tags":["wireless"]}\n{"index":{"_id":"4"}}\n{"name":"停产无线耳机","description":"wireless headphones","price":299,"status":"discontinued","tags":["wireless"]}\n{"index":{"_id":"5"}}\n{"name":"高端无线耳机","description":"premium wireless headphones","price":1299,"status":"active","tags":["wireless"]}\n' >/dev/null
# 执行 bool 查询,并只保留便于核对的响应字段
curl -s -X POST \
"$ES/products/_search?filter_path=hits.total,hits.hits._id,hits.hits._source" \
-H 'Content-Type: application/json' \
-d '{
"query": {
"bool": {
"must": [
{ "match": { "description": "headphones" } }
],
"filter": [
{ "range": { "price": { "lte": 1000 } } }
],
"should": [
{ "term": { "tags": "wireless" } },
{ "term": { "tags": "noise-cancelling" } }
],
"minimum_should_match": 1,
"must_not": [
{ "term": { "status": "discontinued" } }
]
}
}
}'预期输出
{"hits":{"total":{"value":1,"relation":"eq"},"hits":[{"_id":"1","_source":{"name":"无线降噪耳机","description":"wireless noise cancelling headphones","price":899,"status":"active","tags":["wireless","noise-cancelling"]}}]}}文档 1 同时满足关键词、价格和偏好标签条件。文档 2 因未命中任何 should 条件被过滤,文档 4 被 must_not 排除,文档 5 则超过价格上限;文档 3 不满足 must 中的关键词查询。
should 的关键边界
当同一层 bool 已有 must 或 filter 时,should 默认可以一项都不命中,此时通常只用于加分;当只有 should 而没有 must、filter 时,默认至少命中一项。业务规则要求“至少满足若干偏好”时,应显式设置 minimum_should_match,不要依赖默认值。
多个同类条件可以写成数组。数组中的 must 与 filter 按“并且”组合,must_not 中任一条件命中都会排除文档;should 的最低命中数量由 minimum_should_match 决定。
使用建议与常见错误
关键词相关性放入 must 或 should,精确状态、范围、租户和权限条件放入 filter。这样既保留合理评分,也让查询意图更容易审查。
常见错误是把 term 用在经过分词的 text 字段上。精确值应查询 keyword 字段;全文内容应使用 match 等全文查询。另一个错误是同时写了 must 和 should,却误以为 should 自动成为必选条件。
本课总结
must与filter都要求匹配,但只有must参与相关性评分should是否必选取决于查询结构,业务约束应显式设置minimum_should_matchmust_not用于排除结果,并在过滤上下文中执行
下一课预告
下一课将使用 from、size 和 sort 对搜索结果分页排序,并分析深度分页为什么会增加分片与协调节点的工作量。