match 和 term

图片.png

底层机制差异

  1. term 查询
    不分析查询词:直接按输入值搜索倒排索引。
    示例: 字段映射:"status": { "type": "keyword" } 查询:{ "term": { "status": "published" } }
    仅匹配字段值完全等于 "published" 的文档。
  2. match 查询
    分析查询词:对输入值分词后搜索(如 "apple pie" → ["apple", "pie"])。
    示例: 字段映射:"content": { "type": "text", "analyzer": "standard" } 查询:{ "match": { "content": "Quick Fox" } }
    分词后搜索 ["quick", "fox"](假设分词器转小写),匹配包含任一词的文档。

查询行为对比示例
数据示例:

{ "title": "iPhone 13 Pro Max" }  // title 字段为 `text` 类型(分词存储为 ["iphone", "13", "pro", "max"])

查询 1:term 查询(直接匹配未经分析的词项)

{ "term": { "title": "iPhone" } }  // ❌ 无结果(实际存储的是小写 "iphone")
{ "term": { "title.keyword": "iPhone 13 Pro Max" } }  // ✅ 匹配(需用 keyword 子字段)

查询 2:match 查询(自动分词后匹配)

{ "match": { "title": "Pro Max" } }  // ✅ 匹配(搜索词被拆分为 ["pro", "max"])
图片.png

常见误区
对 text 字段使用 term 查询
❌ 错误:{ "term": { "text_field": "搜索词" } }
✅ 修正:改用 match 或查询 .keyword 子字段:{ "term": { "text_field.keyword": "搜索词" } }
期望 match 完全匹配短语
默认 match 是 OR 逻辑,需通过 operator 参数调整:

    { "match": { "content": { "query": "紧急通知", "operator": "and" } } }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容