Ch05 Aggregation
Yang Haoran 1/11/2023 ElasticSearchES
#
# Advanced
# 数据聚合

- 聚合的字段不能是text,也就是说不能分词
# Bucket聚合语法
- 和groupby



# Metrics 聚合语法

- 根据每个桶中的平均得分排序

# RestClient聚合

- 发送请求

- 解析结果

# 多条件聚合


# 拼音分词器(https://github.com/medcl/elasticsearch-analysis-pinyin)
- 可以实现搜索的时候自动提示的功能
- 把分词器的目录挂载到es的plugin之下安装
POST /_analyze
{
"text": ["如家酒店还不错"]
, "analyzer": "pinyin"
}
1
2
3
4
5
2
3
4
5

# 自定义分词器
- 因为拼音分词器不会分词(一个字一个字转成拼音,不知道哪个是词)


但是查询的时候不能用拼音分词器:


- 所以在创建索引时和搜索时要使用不同的分词器


- 所以在创建索引时和搜索时要使用不同的分词器
# 自动补全
# Completion suggester

title分成两个字段的原因是,如果是一个字符串的话只能对S进行自动补全,用户搜W是不会出现的
自动补全的查询


# 例子
- 需要分词的用text_anlyzer, 不需要分词(固定的词条,keyword )的用completion_analyzer
- 把关键字,比如说品牌,商圈放到suggestion里面去

// 酒店数据索引库
PUT /hotel
{
"settings": {
"analysis": {
"analyzer": {
"text_anlyzer": {
"tokenizer": "ik_max_word",
"filter": "py"
},
"completion_analyzer": {
"tokenizer": "keyword",
"filter": "py"
}
},
"filter": {
"py": {
"type": "pinyin",
"keep_full_pinyin": false,
"keep_joined_full_pinyin": true,
"keep_original": true,
"limit_first_letter_length": 16,
"remove_duplicated_term": true,
"none_chinese_pinyin_tokenize": false
}
}
}
},
"mappings": {
"properties": {
"id":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart",
"copy_to": "all"
},
"address":{
"type": "keyword",
"index": false
},
"price":{
"type": "integer"
},
"score":{
"type": "integer"
},
"brand":{
"type": "keyword",
"copy_to": "all"
},
"city":{
"type": "keyword"
},
"starName":{
"type": "keyword"
},
"business":{
"type": "keyword",
"copy_to": "all"
},
"location":{
"type": "geo_point"
},
"pic":{
"type": "keyword",
"index": false
},
"all":{
"type": "text",
"analyzer": "text_anlyzer",
"search_analyzer": "ik_smart"
},
"suggestion":{
"type": "completion",
"analyzer": "completion_analyzer"
}
}
}
}
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
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
- 查询

# Restful API

- 处理结果
