Elasticsearch全文搜索 #

一、全文搜索概述 #

1.1 全文搜索流程 #

text
全文搜索流程
├── 1. 分词
│   ├── 查询文本分词
│   └── 文档内容分词
├── 2. 倒排索引查找
│   └── 查找词项对应的文档
├── 3. 相关性计算
│   ├── TF-IDF
│   └── BM25
└── 4. 结果排序
    └── 按相关性得分排序

1.2 相关性算法 #

算法 说明 默认
BM25 Okapi BM25 5.x以后
TF-IDF 词频-逆文档频率 5.x之前

二、match查询详解 #

2.1 基本match #

bash
GET /products/_search
{
  "query": {
    "match": {
      "description": "quick brown fox"
    }
  }
}

分词后查询:quick OR brown OR fox

2.2 operator控制 #

bash
GET /products/_search
{
  "query": {
    "match": {
      "description": {
        "query": "quick brown fox",
        "operator": "and"
      }
    }
  }
}

分词后查询:quick AND brown AND fox

2.3 minimum_should_match #

bash
GET /products/_search
{
  "query": {
    "match": {
      "description": {
        "query": "quick brown fox jumps lazy dog",
        "minimum_should_match": "75%"
      }
    }
  }
}

格式

  • 数字:至少匹配N个词
  • 百分比:至少匹配N%的词
  • 组合:如 “3<90%” 表示少于3个词全部匹配,否则匹配90%

2.4 analyzer指定 #

bash
GET /products/_search
{
  "query": {
    "match": {
      "description": {
        "query": "Quick Brown Fox",
        "analyzer": "english"
      }
    }
  }
}

2.5 fuzziness模糊匹配 #

bash
GET /products/_search
{
  "query": {
    "match": {
      "name": {
        "query": "iPhne",
        "fuzziness": "AUTO"
      }
    }
  }
}

fuzziness值

说明
0 不允许模糊
1 允许1个编辑距离
2 允许2个编辑距离
AUTO 自动选择(推荐)

AUTO规则

  • 1-2字符:不允许模糊
  • 3-5字符:允许1个编辑距离
  • 5+字符:允许2个编辑距离

2.6 模糊参数组合 #

bash
GET /products/_search
{
  "query": {
    "match": {
      "name": {
        "query": "iPhne",
        "fuzziness": "AUTO",
        "prefix_length": 2,
        "max_expansions": 50,
        "transpositions": true
      }
    }
  }
}
参数 说明
prefix_length 前缀必须精确匹配的长度
max_expansions 最大扩展词项数
transpositions 是否允许相邻字符交换

三、match_phrase短语查询 #

3.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "match_phrase": {
      "description": "quick brown fox"
    }
  }
}

特点

  • 词项必须按顺序出现
  • 词项之间不能有其他词

3.2 slop间隔 #

bash
GET /products/_search
{
  "query": {
    "match_phrase": {
      "description": {
        "query": "quick fox",
        "slop": 2
      }
    }
  }
}

slop=2:允许中间间隔最多2个位置

text
原文: "quick brown fox"
slop=0: quick fox ✗
slop=1: quick fox ✗
slop=2: quick fox ✓

3.3 应用场景 #

text
应用场景
├── 精确短语搜索
├── 人名搜索
├── 产品名称搜索
└── 专业术语搜索

四、match_phrase_prefix前缀短语 #

4.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "match_phrase_prefix": {
      "name": {
        "query": "iPhone 15 Pro M",
        "max_expansions": 10
      }
    }
  }
}

4.2 参数说明 #

参数 说明
max_expansions 最后一个词的最大扩展数

4.3 应用场景 #

text
应用场景
├── 搜索建议
├── 即时搜索
└── 自动补全

五、multi_match多字段查询 #

5.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iPhone",
      "fields": ["name", "description", "brand"]
    }
  }
}

5.2 字段权重 #

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "iPhone",
      "fields": ["name^3", "description", "brand^2"]
    }
  }
}

5.3 查询类型 #

best_fields(默认)

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "quick brown fox",
      "type": "best_fields",
      "fields": ["title", "content"],
      "tie_breaker": 0.3
    }
  }
}

得分:最高分 + tie_breaker × 其他分

most_fields

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "quick brown fox",
      "type": "most_fields",
      "fields": ["title", "title.standard", "title.english"]
    }
  }
}

得分:所有字段得分之和

cross_fields

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "John Smith",
      "type": "cross_fields",
      "fields": ["first_name", "last_name"],
      "operator": "and"
    }
  }
}

特点:将所有字段视为一个大字段

phrase

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "quick brown fox",
      "type": "phrase",
      "fields": ["title", "content"]
    }
  }
}

使用match_phrase查询每个字段

phrase_prefix

bash
GET /products/_search
{
  "query": {
    "multi_match": {
      "query": "quick brown f",
      "type": "phrase_prefix",
      "fields": ["title", "content"]
    }
  }
}

使用match_phrase_prefix查询每个字段

六、common术语查询 #

6.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "common": {
      "description": {
        "query": "the quick brown fox",
        "cutoff_frequency": 0.01
      }
    }
  }
}

6.2 工作原理 #

text
工作流程
├── 1. 计算词项频率
├── 2. 高频词(> cutoff_frequency)
│   └── 使用低优先级查询
├── 3. 低频词(<= cutoff_frequency)
│   └── 使用高优先级查询
└── 4. 组合结果

6.3 应用场景 #

text
应用场景
├── 包含常见词的查询
├── 提高查询性能
└── 避免停用词影响

七、query_string查询 #

7.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "query_string": {
      "query": "iPhone AND (Apple OR Samsung)"
    }
  }
}

7.2 指定字段 #

bash
GET /products/_search
{
  "query": {
    "query_string": {
      "query": "iPhone",
      "fields": ["name", "description"]
    }
  }
}

7.3 查询语法 #

语法 说明
title:“quick brown fox” 精确短语
title:(quick OR brown) OR操作
title:(quick AND brown) AND操作
title:(quick NOT brown) NOT操作
title:(quick -brown) 排除
title:quic* 通配符
title:quic? 单字符通配符
title:quikc~ 模糊匹配
title:quikc~2 指定编辑距离
title:“quick brown”~2 短语间隔

7.4 参数 #

bash
GET /products/_search
{
  "query": {
    "query_string": {
      "query": "iPhone",
      "default_field": "name",
      "default_operator": "AND",
      "analyze_wildcard": true,
      "lenient": true
    }
  }
}

八、simple_query_string查询 #

8.1 基本用法 #

bash
GET /products/_search
{
  "query": {
    "simple_query_string": {
      "query": "iPhone +Apple -Samsung",
      "fields": ["name", "description"]
    }
  }
}

8.2 操作符 #

符号 说明
+ 必须(AND)
| 或者(OR)
- 排除(NOT)
" 短语
* 前缀
( ) 分组
~N 模糊

8.3 与query_string的区别 #

特性 query_string simple_query_string
错误处理 语法错误返回错误 忽略错误部分
安全性 较低 较高
灵活性

九、相关度调优 #

9.1 使用boost #

bash
GET /products/_search
{
  "query": {
    "bool": {
      "should": [
        { "match": { "name": { "query": "iPhone", "boost": 3 } } },
        { "match": { "description": { "query": "iPhone", "boost": 1 } } }
      ]
    }
  }
}

9.2 使用function_score #

bash
GET /products/_search
{
  "query": {
    "function_score": {
      "query": { "match": { "name": "iPhone" } },
      "functions": [
        {
          "filter": { "term": { "featured": true } },
          "weight": 2
        },
        {
          "field_value_factor": {
            "field": "rating",
            "modifier": "sqrt"
          }
        }
      ]
    }
  }
}

9.3 使用衰减函数 #

bash
GET /products/_search
{
  "query": {
    "function_score": {
      "query": { "match": { "name": "iPhone" } },
      "functions": [
        {
          "gauss": {
            "price": {
              "origin": 500,
              "scale": 200
            }
          }
        }
      ]
    }
  }
}

十、搜索建议 #

10.1 使用completion suggester #

bash
PUT /products
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "suggest": {
        "type": "completion",
        "analyzer": "standard"
      }
    }
  }
}

索引数据:

bash
POST /products/_doc
{
  "name": "iPhone 15",
  "suggest": {
    "input": ["iPhone", "iPhone 15", "iPhone 15 Pro"],
    "weight": 10
  }
}

查询建议:

bash
POST /products/_search
{
  "suggest": {
    "product_suggest": {
      "prefix": "iPh",
      "completion": {
        "field": "suggest",
        "size": 5
      }
    }
  }
}

十一、最佳实践 #

11.1 查询选择 #

text
查询选择建议
├── 简单全文搜索
│   └── match查询
├── 精确短语搜索
│   └── match_phrase查询
├── 多字段搜索
│   └── multi_match查询
├── 用户输入搜索
│   └── simple_query_string查询
└── 高级搜索
    └── query_string查询

11.2 性能优化 #

text
性能优化建议
├── 避免通配符开头
│   └── 如 *phone
├── 合理使用模糊匹配
│   └── 设置prefix_length
├── 控制max_expansions
│   └── 避免过多扩展
└── 使用filter缓存
    └── 精确过滤条件

十二、总结 #

本章介绍了Elasticsearch全文搜索:

  1. match查询是最常用的全文搜索
  2. match_phrase用于精确短语匹配
  3. multi_match支持多字段搜索
  4. fuzziness实现模糊匹配
  5. function_score调优相关性
  6. completion suggester实现搜索建议

下一步,我们将学习精确搜索。

最后更新:2026-03-27