Elasticsearch的补全提示
时间: 2023-12-20 14:30:52 浏览: 105
Elasticsearch实现检索词自动补全(检索词补全,自动纠错,拼音补全,繁简转换) 包含demo
Elasticsearch提供了Completion Suggester来实现补全提示功能。具体实现方式如下:
1. 定义Mapping,使用“completion” type索引数据。
2. 运行“suggest”查询,得到搜索建议。
以下是一个例子,演示如何使用Completion Suggester实现补全提示功能:
1. 定义Mapping,使用“completion” type。
```json
PUT /suggest_test
{
"mappings": {
"properties": {
"suggest": {
"type": "completion"
}
}
}
}
```
2. 索引数据。
```json
PUT /suggest_test/_doc/1
{
"suggest": {
"input": ["apple", "pie"],
"weight": 10
}
}
```
3. 运行“suggest”查询,得到搜索建议。
```json
GET /suggest_test/_search
{
"suggest": {
"my-suggestion": {
"prefix": "app",
"completion": {
"field": "suggest"
}
}
}
}
```
返回结果如下:
```json
{
"suggest": {
"my-suggestion": [
{
"text": "app",
"offset": 0,
"length": 3,
"options": [
{
"text": "apple",
"_index": "suggest_test",
"_type": "_doc",
"_id": "1",
"_score": 10.0,
"_source": {
"suggest": {
"input": [
"apple",
"pie"
],
"weight": 10
}
}
}
]
}
]
}
}
```
阅读全文