这三种类型都是 multi_match
查询中的策略,用于控制多个字段的匹配方式和评分计算:
类型 | 核心思想 | 适用场景 | 评分方式 |
---|---|---|---|
best_fields |
取最佳字段 | 查询词集中在单个字段 | 使用最高分字段的得分 |
most_fields |
字段民主制 | 查询词分散在多个字段 | 各字段得分的总和 |
cross_fields |
跨字段统一 | 查询词需要跨字段组合匹配 | 视为一个大字段计算 |
dis_max
查询(只取最高分字段的得分)。典型用例:
GET /products/_search
{
"query": {
"multi_match": {
"query": "quick brown",
"type": "best_fields",
"fields": ["title^3", "content"], // title权重更高
"tie_breaker": 0.3 // 其他字段得分的30%会加入总分
}
}
}
适用场景:
iPhone
”,可能在 title
字段匹配度最高。优势:
bool
查询组合多个 match
查询。典型用例:
GET /products/_search
{
"query": {
"multi_match": {
"query": "smartphone",
"type": "most_fields",
"fields": ["name", "name.pinyin", "name.standard"] // 同义词字段
}
}
}
适用场景:
主字段
+拼音字段
+同义词字段
的组合搜索。优势:
典型用例:
GET /products/_search
{
"query": {
"multi_match": {
"query": "John Smith",
"type": "cross_fields",
"fields": ["first_name", "last_name"],
"operator": "and" // 必须同时包含 John 和 Smith
}
}
}
适用场景:
优势:
term dilution problem
)。假设有以下文档:
{
"title": "Smartphone X10",
"description": "Latest smartphone with advanced camera",
"tags": ["electronics", "mobile"]
}
现在需要搜索 smartphone camera
。
{
"type": "best_fields",
"fields": ["title", "description"]
}
结果:description
字段的匹配得分更高(包含两个词),title
字段的匹配被忽略。
{
"type": "most_fields",
"fields": ["title", "description"]
}
结果:两个字段的得分相加,title
匹配 “smartphone
”,description
匹配两个词。
{
"type": "cross_fields",
"fields": ["title", "description"],
"operator": "and"
}
结果:要求两个词必须出现在字段组合中(description
满足),类似在虚拟字段 “title
+description
” 中搜索。
best_fields
:
tie_breaker
参数平衡其他字段影响。most_fields
:
cross_fields
:
best_fields
通常最快(dis_max
查询简单)。cross_fields
可能较慢(需要协调多个字段的统计信息)。正确选择匹配类型可以显著提升搜索相关性和用户体验。