Elasticsearch搜索建议 #
一、建议类型 #
text
建议类型
├── Term Suggester
│ └── 词项纠错建议
├── Phrase Suggester
│ └── 短语纠错建议
└── Completion Suggester
└── 自动补全建议
二、Completion Suggester #
2.1 映射配置 #
bash
PUT /products
{
"mappings": {
"properties": {
"name": {
"type": "text"
},
"suggest": {
"type": "completion",
"analyzer": "standard",
"search_analyzer": "standard",
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50
}
}
}
}
参数说明:
| 参数 | 说明 | 默认值 |
|---|---|---|
| analyzer | 索引分析器 | simple |
| search_analyzer | 搜索分析器 | 同analyzer |
| preserve_separators | 保留分隔符 | true |
| preserve_position_increments | 保留位置增量 | true |
| max_input_length | 最大输入长度 | 50 |
2.2 索引数据 #
bash
POST /products/_doc
{
"name": "iPhone 15 Pro Max",
"suggest": {
"input": [
"iPhone",
"iPhone 15",
"iPhone 15 Pro",
"iPhone 15 Pro Max"
],
"weight": 10
}
}
权重说明:
- weight值越大,排名越靠前
- 用于控制建议顺序
2.3 查询建议 #
bash
POST /products/_search
{
"suggest": {
"product_suggest": {
"prefix": "iPh",
"completion": {
"field": "suggest",
"size": 5,
"skip_duplicates": true,
"fuzzy": {
"fuzziness": 1,
"transpositions": true,
"min_length": 3,
"prefix_length": 2
}
}
}
}
}
响应:
json
{
"suggest": {
"product_suggest": [
{
"text": "iPh",
"offset": 0,
"length": 3,
"options": [
{
"text": "iPhone",
"_index": "products",
"_id": "1",
"_score": 10.0,
"_source": {
"name": "iPhone 15 Pro Max"
}
}
]
}
]
}
}
2.4 参数说明 #
| 参数 | 说明 |
|---|---|
| prefix | 前缀查询 |
| field | 建议字段 |
| size | 返回数量 |
| skip_duplicates | 跳过重复 |
| fuzzy | 模糊配置 |
2.5 上下文建议 #
映射配置:
bash
PUT /products
{
"mappings": {
"properties": {
"suggest": {
"type": "completion",
"contexts": [
{
"name": "brand",
"type": "category"
},
{
"name": "location",
"type": "geo",
"precision": 4
}
]
}
}
}
}
索引数据:
bash
POST /products/_doc
{
"suggest": {
"input": ["iPhone", "iPhone 15"],
"weight": 10,
"contexts": {
"brand": "Apple",
"location": {
"lat": 40.7128,
"lon": -74.0060
}
}
}
}
查询建议:
bash
POST /products/_search
{
"suggest": {
"product_suggest": {
"prefix": "iPh",
"completion": {
"field": "suggest",
"contexts": {
"brand": "Apple"
}
}
}
}
}
三、Term Suggester #
3.1 基本用法 #
bash
POST /products/_search
{
"suggest": {
"text": "iPhne",
"my_suggestion": {
"term": {
"field": "name"
}
}
}
}
响应:
json
{
"suggest": {
"my_suggestion": [
{
"text": "iPhne",
"offset": 0,
"length": 5,
"options": [
{
"text": "iPhone",
"score": 0.8,
"freq": 100
}
]
}
]
}
}
3.2 参数配置 #
bash
POST /products/_search
{
"suggest": {
"text": "iPhne",
"my_suggestion": {
"term": {
"field": "name",
"analyzer": "standard",
"size": 5,
"sort": "score",
"suggest_mode": "missing",
"max_edits": 2,
"prefix_length": 1,
"min_word_length": 4,
"string_distance": "internal"
}
}
}
}
| 参数 | 说明 | 默认值 |
|---|---|---|
| analyzer | 分析器 | 字段分析器 |
| size | 建议数量 | 5 |
| sort | 排序方式 | score |
| suggest_mode | 建议模式 | missing |
| max_edits | 最大编辑距离 | 2 |
| prefix_length | 前缀长度 | 1 |
| min_word_length | 最小词长度 | 4 |
| string_distance | 距离算法 | internal |
3.3 suggest_mode #
| 模式 | 说明 |
|---|---|
| missing | 只对不存在词提供建议 |
| popular | 只对热门词提供建议 |
| always | 总是提供建议 |
3.4 string_distance #
| 算法 | 说明 |
|---|---|
| internal | ES内部算法 |
| damerau_levenshtein | Damerau-Levenshtein |
| levenshtein | Levenshtein |
| jaro_winkler | Jaro-Winkler |
| ngram | N-gram |
四、Phrase Suggester #
4.1 基本用法 #
bash
POST /products/_search
{
"suggest": {
"text": "iPhne fiften",
"my_suggestion": {
"phrase": {
"field": "name",
"gram_size": 2,
"real_word_error_likelihood": 0.95,
"max_errors": 2,
"collate": {
"query": {
"match": {
"name": "{{suggestion}}"
}
}
}
}
}
}
}
4.2 参数配置 #
| 参数 | 说明 | 默认值 |
|---|---|---|
| gram_size | n-gram大小 | 1 |
| real_word_error_likelihood | 真词错误概率 | 0.95 |
| max_errors | 最大错误数 | 1 |
| collate | 验证查询 | - |
| size | 建议数量 | 5 |
4.3 高亮建议 #
bash
POST /products/_search
{
"suggest": {
"text": "iPhne fiften",
"my_suggestion": {
"phrase": {
"field": "name",
"highlight": {
"pre_tag": "<em>",
"post_tag": "</em>"
}
}
}
}
}
五、组合使用 #
5.1 搜索与建议组合 #
bash
POST /products/_search
{
"query": {
"match": {
"name": "iPhne"
}
},
"suggest": {
"text": "iPhne",
"my_suggestion": {
"term": {
"field": "name"
}
}
}
}
5.2 多建议器 #
bash
POST /products/_search
{
"suggest": {
"text": "iPhne",
"term_suggestion": {
"term": {
"field": "name"
}
},
"completion_suggestion": {
"prefix": "iPh",
"completion": {
"field": "suggest"
}
}
}
}
六、实际应用 #
6.1 搜索框自动补全 #
bash
PUT /search_suggestions
{
"mappings": {
"properties": {
"suggest": {
"type": "completion"
},
"category": {
"type": "keyword"
}
}
}
}
POST /search_suggestions/_doc
{
"suggest": {
"input": [
"iPhone",
"iPhone 15",
"iPhone 15 Pro"
],
"weight": 10
},
"category": "products"
}
查询:
bash
POST /search_suggestions/_search
{
"suggest": {
"search_suggest": {
"prefix": "iPh",
"completion": {
"field": "suggest",
"size": 10,
"skip_duplicates": true
}
}
}
}
6.2 拼写纠错 #
bash
POST /products/_search
{
"query": {
"match": {
"name": "iPhne"
}
},
"suggest": {
"text": "iPhne",
"did_you_mean": {
"phrase": {
"field": "name",
"max_errors": 2,
"collate": {
"query": {
"match": {
"name": "{{suggestion}}"
}
}
}
}
}
}
}
七、最佳实践 #
7.1 性能优化 #
text
性能优化建议
├── Completion Suggester
│ ├── 使用合适的分析器
│ └── 控制input数量
├── Term Suggester
│ ├── 设置合理的max_edits
│ └── 使用prefix_length
└── 通用
├── 控制size大小
└── 使用skip_duplicates
7.2 使用场景 #
text
建议器选择
├── 自动补全
│ └── Completion Suggester
├── 单词纠错
│ └── Term Suggester
├── 短语纠错
│ └── Phrase Suggester
└── 组合使用
└── 多建议器组合
八、总结 #
本章介绍了Elasticsearch搜索建议:
- Completion Suggester用于自动补全
- Term Suggester用于单词纠错
- Phrase Suggester用于短语纠错
- 可组合使用多种建议器
- 上下文建议支持过滤
- 合理配置优化性能
下一步,我们将学习高级特性。
最后更新:2026-03-27