es: minimum_should_match的作用
一,minimum_should_match
什么是 minimum_should_match?
minimum_should_match 用于指定在 should 子句中至少需要匹配多少个条件才能返回文档。它可以用以下方式表示:
数字:2 表示至少匹配 2 个条件
百分比:"60%" 表示至少匹配 60% 的条件
组合:"2<75%" 表示少于 2 个时全部匹配,2 个或以上时匹配 75%
二,场景 1:只有 should 子句
当 bool 查询中只有 should 子句时,minimum_should_match 会生效:
{
"query": {
"bool": {
"should": [
{ "match": { "title": "疫苗" } },
{ "match": { "content": "疫苗" } },
{ "match": { "abstract": "疫苗" } }
],
"minimum_should_match": 2
}
}
}
效果:文档必须至少匹配 3 个 should 条件中的 2 个才会被返回
三,场景 2:should + must 组合
当 bool 查询中同时有 must 和 should 子句时:
{
"query": {
"bool": {
"must": [
{ "match": { "status": "published" } }
],
"should": [
{ "match": { "title": "疫苗" } },
{ "match": { "content": "疫苗" } }
],
"minimum_should_match": 1
}
}
}
效果:
must条件必须全部匹配(无条件)should条件中至少匹配 1 个(由minimum_should_match控制)
四,match 查询中的 minimum_should_match 直接控制词项匹配:
{
"query": {
"match": {
"title": {
"query": "疫苗 开发 研究",
"minimum_should_match": "60%"
}
}
}
}
效果:标题字段中至少需要匹配 3 个词项中的 2 个(60%)。
match的常用场景:
{
"query": {
"multi_match": {
"query": "疫苗 开发 研究",
"fields": ["title^2", "content"],
"type": "best_fields",
"minimum_should_match": "75%"
}
}
}
效果:在多个字段中搜索,至少匹配 75% 的词项。
浙公网安备 33010602011771号