Elastic实战自动补全

自动补全(中文)

定义索引

1
2
3
4
5
6
7
[root@icloud-store ~]# curl -XPUT "http://192.168.0.103:9200/index_test?pretty=true" -H 'Content-Type: application/json' -d'
{
"index": {
"number_of_shards": "1",
"number_of_replicas": "1"
}
}'

定义类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/product/_mapping?pretty=true" -H 'Content-Type: application/json' -d'
{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"tag": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"tag_suggest": {
"type": "completion",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
}
}
}'

查看索引

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
63
64
65
66
67
68
[root@icloud-store ~]# curl -XGET "http://192.168.0.103:9200/index_test?pretty=true"
{
"index_test" : {
"aliases" : { },
"mappings" : {
"product" : {
"properties" : {
"name" : {
"type" : "text",
"analyzer" : "ik_smart"
},
"tag" : {
"type" : "text",
"analyzer" : "ik_smart"
},
"tag_suggest" : {
"type" : "completion",
"analyzer" : "ik_smart",
"preserve_separators" : true,
"preserve_position_increments" : true,
"max_input_length" : 50
},
"tagsuggest" : {
"properties" : {
"completion" : {
"properties" : {
"field" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"size" : {
"type" : "long"
}
}
},
"text" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1513327157579",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "QRHKXGpRSPen2l9u1zWJyw",
"version" : {
"created" : "6000099"
},
"provided_name" : "index_test"
}
}
}
}

索引数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/product/1?pretty=true" -H 'Content-Type: application/json' -d'{
"name" : "花花公子男士修身冬季青年潮流羽绒服",
"tag": "羽绒服, 青年, 潮流",
"tag_suggest": "羽绒服, 青年, 潮流"
}'

[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/product/2?pretty=true" -H 'Content-Type: application/json' -d'{
"name" : "波司登中老年爸爸加厚保暖男士羽绒服",
"tag": "羽绒服, 保暖, 加厚, 中老年",
"tag_suggest": "羽绒服, 保暖, 加厚, 中老年"
}'

[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/product/3?pretty=true" -H 'Content-Type: application/json' -d'{
"name" : "真皮进口头层牛皮立领修身中年皮夹克",
"tag": "羽毛, 中年, 绒, 修身, 羽绒",
"tag_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
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"suggest": {
"product-suggest" : {
"prefix" : "羽绒",
"completion" : {
"field" : "tag_suggest"
}
}
}
}'
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "羽绒",
"offset" : 0,
"length" : 2,
"options" : [
{
"text" : "羽绒服, 保暖, 加厚, 中老年",
"_index" : "index_test",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "波司登中老年爸爸加厚保暖男士羽绒服",
"tag" : "羽绒服, 保暖, 加厚, 中老年",
"tag_suggest" : "羽绒服, 保暖, 加厚, 中老年"
}
},
{
"text" : "羽绒服, 青年, 潮流",
"_index" : "index_test",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "花花公子男士修身冬季青年潮流羽绒服",
"tag" : "羽绒服, 青年, 潮流",
"tag_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
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": "tag_suggest",
"suggest": {
"product-suggest" : {
"prefix" : "羽绒",
"completion" : {
"field" : "tag_suggest"
}
}
}
}'
{
"took" : 6,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "羽绒",
"offset" : 0,
"length" : 2,
"options" : [
{
"text" : "羽绒服, 保暖, 加厚, 中老年",
"_index" : "index_test",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"tag_suggest" : "羽绒服, 保暖, 加厚, 中老年"
}
},
{
"text" : "羽绒服, 青年, 潮流",
"_index" : "index_test",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"tag_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
63
64
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"suggest": {
"product-suggest" : {
"text" : "羽类",
"completion" : {
"field" : "tag_suggest",
"fuzzy" : {
"fuzziness" : 2
}
}
}
}
}'
{
"took" : 9,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "羽类",
"offset" : 0,
"length" : 2,
"options" : [
{
"text" : "羽绒服, 保暖, 加厚, 中老年",
"_index" : "index_test",
"_type" : "product",
"_id" : "2",
"_score" : 3.0,
"_source" : {
"name" : "波司登中老年爸爸加厚保暖男士羽绒服",
"tag" : "羽绒服, 保暖, 加厚, 中老年",
"tag_suggest" : "羽绒服, 保暖, 加厚, 中老年"
}
},
{
"text" : "羽绒服, 青年, 潮流",
"_index" : "index_test",
"_type" : "product",
"_id" : "1",
"_score" : 3.0,
"_source" : {
"name" : "花花公子男士修身冬季青年潮流羽绒服",
"tag" : "羽绒服, 青年, 潮流",
"tag_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": "tag_suggest",
"suggest": {
"product-suggest" : {
"text" : "羽",
"completion" : {
"field" : "tag_suggest"
}
}
}
}'
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "羽",
"offset" : 0,
"length" : 1,
"options" : [
{
"text" : "羽毛, 中年, 绒, 修身, 羽绒",
"_index" : "index_test",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"tag_suggest" : "羽毛, 中年, 绒, 修身, 羽绒"
}
},
{
"text" : "羽绒服, 保暖, 加厚, 中老年",
"_index" : "index_test",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"tag_suggest" : "羽绒服, 保暖, 加厚, 中老年"
}
},
{
"text" : "羽绒服, 青年, 潮流",
"_index" : "index_test",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"tag_suggest" : "羽绒服, 青年, 潮流"
}
}
]
}
]
}
}


[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": "tag_suggest",
"suggest": {
"product-suggest" : {
"text" : "羽",
"completion" : {
"field" : "tag_suggest",
"size": 2
}
}
}
}'
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "羽",
"offset" : 0,
"length" : 1,
"options" : [
{
"text" : "羽毛, 中年, 绒, 修身, 羽绒",
"_index" : "index_test",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"tag_suggest" : "羽毛, 中年, 绒, 修身, 羽绒"
}
},
{
"text" : "羽绒服, 保暖, 加厚, 中老年",
"_index" : "index_test",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"tag_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
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": "tag_suggest",
"suggest": {
"product-suggest" : {
"text" : "yu",
"completion" : {
"field" : "tag_suggest",
"size": 2
}
}
}
}'
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "yu",
"offset" : 0,
"length" : 2,
"options" : [ ]
}
]
}
}

可以看到拼音搜索并不能得到友好的推荐词,所以需要结合拼音分词进行再一次优化.

自动补全(中文+拼音)

删除索引

1
[root@icloud-store ~]# curl -XDELETE "http://192.168.0.103:9200/index_test*?pretty=true"

定义索引

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
[elon@icloud-store elasticsearch-6.0.0]$ curl -XPUT "http://192.168.0.103:9200/index_test?pretty=true" -H 'Content-Type: application/json' -d'
{
"index" : {
"analysis": {
"analyzer": {
"default" : {
"tokenizer" : "ik_smart"
},
"ik_pinyin_analyzer": {
"tokenizer": "ik_smart",
"filter": [
"mix_pinyin",
"word_delimiter"
]
}
},
"filter": {
"mix_pinyin": {
"type": "pinyin",
"keep_full_pinyin" : true,
"lowercase" : true,
"first_letter": "prefix",
"padding_char": " "
}
}
}
},
"number_of_shards": "3",
"number_of_replicas": "1"
}'

定义类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/product/_mapping?pretty=true" -H 'Content-Type: application/json' -d'
{
"properties": {
"name": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"tag": {
"type": "text",
"analyzer": "ik_smart",
"search_analyzer": "ik_smart"
},
"tag_suggest": {
"type": "completion",
"analyzer": "ik_pinyin_analyzer",
"search_analyzer": "ik_pinyin_analyzer",
"preserve_separators": false
}
}
}'

创建索引

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/product/1?pretty=true" -H 'Content-Type: application/json' -d'{
"name" : "花花公子男士修身冬季青年潮流羽绒服",
"tag": "羽绒服, 青年, 潮流",
"tag_suggest": "羽绒服, 青年, 潮流"
}'

[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/product/2?pretty=true" -H 'Content-Type: application/json' -d'{
"name" : "波司登中老年爸爸加厚保暖男士羽绒服",
"tag": "羽绒服, 保暖, 加厚, 中老年",
"tag_suggest": "羽绒服, 保暖, 加厚, 中老年"
}'

[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/product/3?pretty=true" -H 'Content-Type: application/json' -d'{
"name" : "真皮进口头层牛皮立领修身中年皮夹克",
"tag": "羽毛, 中年, 绒, 修身, 羽绒",
"tag_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
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": "tag_suggest",
"suggest": {
"product-suggest" : {
"text" : "yumao",
"completion" : {
"field" : "tag_suggest",
"size": 5
}
}
}
}'
{
"took" : 17,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "yumao",
"offset" : 0,
"length" : 5,
"options" : [
{
"text" : "羽毛, 中年, 绒, 修身, 羽绒",
"_index" : "index_test",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"tag_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": "tag_suggest",
"suggest": {
"product-suggest" : {
"text" : "yumao",
"completion" : {
"field" : "tag_suggest",
"fuzzy" : {
"fuzziness" : 2
},
"size": 5
}
}
}
}'
{
"took" : 18,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "yumao",
"offset" : 0,
"length" : 5,
"options" : [
{
"text" : "羽毛, 中年, 绒, 修身, 羽绒",
"_index" : "index_test",
"_type" : "product",
"_id" : "3",
"_score" : 3.0,
"_source" : {
"tag_suggest" : "羽毛, 中年, 绒, 修身, 羽绒"
}
},
{
"text" : "羽绒服, 保暖, 加厚, 中老年",
"_index" : "index_test",
"_type" : "product",
"_id" : "2",
"_score" : 2.0,
"_source" : {
"tag_suggest" : "羽绒服, 保暖, 加厚, 中老年"
}
},
{
"text" : "羽绒服, 青年, 潮流",
"_index" : "index_test",
"_type" : "product",
"_id" : "1",
"_score" : 2.0,
"_source" : {
"tag_suggest" : "羽绒服, 青年, 潮流"
}
}
]
}
]
}
}

[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": "tag_suggest",
"suggest": {
"product-suggest" : {
"text" : "yu毛",
"completion" : {
"field" : "tag_suggest",
"fuzzy" : {
"fuzziness" : 2
},
"size": 5
}
}
}
}'
{
"took" : 11,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "yu毛",
"offset" : 0,
"length" : 3,
"options" : [
{
"text" : "羽毛, 中年, 绒, 修身, 羽绒",
"_index" : "index_test",
"_type" : "product",
"_id" : "3",
"_score" : 1.0,
"_source" : {
"tag_suggest" : "羽毛, 中年, 绒, 修身, 羽绒"
}
},
{
"text" : "羽绒服, 保暖, 加厚, 中老年",
"_index" : "index_test",
"_type" : "product",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"tag_suggest" : "羽绒服, 保暖, 加厚, 中老年"
}
},
{
"text" : "羽绒服, 青年, 潮流",
"_index" : "index_test",
"_type" : "product",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"tag_suggest" : "羽绒服, 青年, 潮流"
}
}
]
}
]
}
}

[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"_source": "tag_suggest",
"suggest": {
"product-suggest" : {
"text" : "羽毛",
"completion" : {
"field" : "tag_suggest",
"fuzzy" : {
"fuzziness" : 2
},
"size": 5
}
}
}
}'
{
"took" : 13,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 0,
"max_score" : 0.0,
"hits" : [ ]
},
"suggest" : {
"product-suggest" : [
{
"text" : "羽毛",
"offset" : 0,
"length" : 2,
"options" : [
{
"text" : "羽毛, 中年, 绒, 修身, 羽绒",
"_index" : "index_test",
"_type" : "product",
"_id" : "3",
"_score" : 3.0,
"_source" : {
"tag_suggest" : "羽毛, 中年, 绒, 修身, 羽绒"
}
},
{
"text" : "羽绒服, 保暖, 加厚, 中老年",
"_index" : "index_test",
"_type" : "product",
"_id" : "2",
"_score" : 2.0,
"_source" : {
"tag_suggest" : "羽绒服, 保暖, 加厚, 中老年"
}
},
{
"text" : "羽绒服, 青年, 潮流",
"_index" : "index_test",
"_type" : "product",
"_id" : "1",
"_score" : 2.0,
"_source" : {
"tag_suggest" : "羽绒服, 青年, 潮流"
}
}
]
}
]
}
}

[root@icloud-store ~]# curl -XPOST "http://192.168.0.103:9200/index_test/_search?pretty" -H 'Content-Type: application/json' -d'
{
"suggest": {
"product-suggest" : {
"text" : "yu毛",
"completion": {
"field": "tag_suggest",
"highlight": {
"pre_tag": "<em>",
"post_tag": "</em>"
},
"size": 5
}
}
}
}'