介绍
elasticsearch的suggester共有四类(term suggester, phrase suggester, completion suggester, context suggester), 其中completion suggester作为搜索框中的自动补齐功能,尤为常用。实现一个完整的completion suggester功能,需要三个步骤:创建映射,插入索引数据,搜索数据。
此外,本文此基础上用实现ES自动补全。希望给大家一点启示吧!
先创建索引、创建映射、插入数据
========================创建映射=========================
curl -XPUT localhost:9200/index
curl -XPUT localhost:9200/index/test/_mapping -d '{
"test" : {
"properties" : {
"name" : { "type" : "string" },
"name_suggest" : { "type" : "completion",
"analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
},
"tag_suggest" : { "type" : "completion",
"analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
}
}
}
}' ========================插入索引数据=========================
curl -XPUT 'localhost:9200/index/test/1?refresh=true' -d '{
"name" : "xdy",
"name_suggest" : {
"input": ["xdy", "hdu"]
}
}' curl -XPUT 'localhost:9200/index/test/2?refresh=true' -d '{
"name" : "lz",
"name_suggest" : {
"input": ["lz", "hdu"],
"output": "lz-hdu"
}
}' curl -XPUT 'localhost:9200/index/test/3?refresh=true' -d '{
"name" : "xck",
"name_suggest" : {
"input": ["xck", "gbd"]
}
}' curl -XPUT 'localhost:9200/index/test/4?refresh=true' -d '{
"name_suggest" : {
"input": [ "hz", "bdata", "cas"]
}
}' curl -XPUT 'localhost:9200/index/test/5?refresh=true' -d '{
"tag_suggest" : {
"input": ["bfd", "bdata", "hadoop"]
}
}' ========================创建查询=========================
curl -XPOST 'localhost:9200/index/_suggest?pretty' -d '{
"index-suggest" : {
"text" : "b",
"completion" : {
"field" : "name_suggest"
}
}
}'
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
Python 接口实现自动补全
from elasticsearch import Elasticsearch def suggest(tag, query, suggest_size=10): es_suggest_options = set_suggest_optional(query, tag, suggest_size) es_client = Elasticsearch(hosts=["127.0.0.1:9200"], timeout=5000)
es_result = es_client.suggest(index='index',body=es_suggest_options) final_results = get_suggest_list(es_result) return final_results def get_suggest_list(es_result): result_items = es_result['suggest'][0]["options"]
final_results = [] for item in result_items:
final_results.append(item['text']) return final_results def set_suggest_optional(query, tag, suggest_size): es_suggest_options = { "suggest": { "text": query, "completion": { "field": tag, "size": suggest_size
}
}
} return es_suggest_options if __name__ == '__main__':
final_results = suggest('name_suggest', 'b', 2) for item in final_results: print item print '=========================================' final_results = suggest('tag_suggest', 'b', 2) for item in final_results: print item
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
输出:
说明和参考
相关说明和参考只看官方文档即可:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#completion-suggester-mapping
关于创建映射时的参数说明:
查询补全时参数说明: