1、简介

Elastic 官方网站

Elastic 中文社区

Elasticsearch: 权威指南

2、使用

2.1、创建索引

img

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
{
"aliases": {
"customer_remind_alias_pre": {}
},
"mappings": {
"properties": {
"carExclusiveConsultantsId": {
"type": "keyword"
},
"carId": {
"type": "keyword"
},
"carIdOwnOrg": {
"type": "keyword"
},
"car_exclusive_consultants_id": {
"type": "keyword"
},
"cardEntityName": {
"type": "keyword"
},
"consumeAgainDate": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"consumeAgainState": {
"type": "keyword"
},
"couponSendState": {
"type": "keyword"
},
"creationtime": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"customerCellPhone": {
"type": "text",
"analyzer": "ngram_11_analyzer"
},
"customerId": {
"type": "keyword"
},
"customerName": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
},
"analyzer": "ngram_10_analyzer"
},
"finishTime": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"fullCarNo": {
"type": "text",
"fields": {
"raw": {
"type": "keyword"
}
},
"analyzer": "ngram_8_analyzer"
},
"groupId": {
"type": "keyword"
},
"idOwnOrg": {
"type": "keyword"
},
"isDeleted": {
"type": "keyword"
},
"lastDetectionItemCode": {
"type": "keyword"
},
"lastDetectionServiceIdList": {
"type": "keyword"
},
"lastInsuranceCompanyId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lastInsuranceCompanyId ": {
"type": "keyword"
},
"lastInsuranceOrgId": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lastInsuranceOrgId ": {
"type": "keyword"
},
"lastServiceDate": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"lastServiceOrg": {
"type": "keyword"
},
"operatorId": {
"type": "keyword"
},
"operatorProcessState": {
"type": "keyword"
},
"operatorSendState": {
"type": "keyword"
},
"phoneSendDate": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"phoneSendState": {
"type": "keyword"
},
"picSendState": {
"type": "keyword"
},
"pkId": {
"type": "keyword"
},
"remindDate": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"remindRuleId": {
"type": "keyword"
},
"remindType": {
"type": "keyword"
},
"smsSendDate": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"smsSendState": {
"type": "keyword"
},
"sourceType": {
"type": "keyword"
},
"state": {
"type": "keyword"
},
"wechatFollower": {
"type": "keyword"
},
"wechatSendDate": {
"type": "date",
"ignore_malformed": true,
"format": "yyyy-MM-dd"
},
"wechatSendState": {
"type": "keyword"
}
},
"settings": {
"index": {
"max_ngram_diff": "19",
"analysis": {
"analyzer": {
"ngram_10_analyzer": {
"tokenizer": "ngram_10_tokenizer"
},
"ngram_11_analyzer": {
"tokenizer": "ngram_11_tokenizer"
},
"ngram_8_analyzer": {
"tokenizer": "ngram_8_tokenizer"
}
},
"tokenizer": {
"ngram_10_tokenizer": {
"type": "ngram",
"min_gram": "1",
"max_gram": "20"
},
"ngram_11_tokenizer": {
"type": "ngram",
"min_gram": "1",
"max_gram": "11"
},
"ngram_8_tokenizer": {
"type": "ngram",
"min_gram": "1",
"max_gram": "8"
}
}
}
}
}
}
}

2.2、状态查询

2.2.1、集群的健康状态

1
GET _cat/health

2.2.2、节点状态

1
GET _cat/nodes

2.2.3、 查看所有索引

1
GET _cat/indices

2.3、常用查询

2.3.1、查询数量

  • track_total_hits=true不然超过1万,数量只给10000
  • customer_remind为索引名称
  • 限制size=0,不反回结果,只返回数量
  • should 相当于mysql中的 or
  • filter 过滤 可以理解成mysql中的 and
  • term 精确匹配 相当于mysql中的 =
  • terms 相当于mysql中的 in
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
POST customer_remind/_search?track_total_hits=true
{
"query": {
"bool": {
"should": [
{
"terms": {
"carIdOwnOrg": [
"15519724471253495810",
"15519724471253495811"
]
}
},
{
"terms": {
"idOwnOrg": [
"15519724471253495810"
]
}
}
],
"filter": [
{
"terms": {
"state": [
"0",
"2"
]
}
},
{
"term": {
"groupId": "15519724471253495812"
}
}
]
}
},
"size": 0
}

2.3.2、查询列表

  • customer_remind为索引名称
  • must 必须匹配
  • multi_match 多个字段模糊查询
  • query对应查询的关键词
  • from、size对应分页参数
  • sort 排序参数
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
POST customer_remind/_search
{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "16605115059",
"analyzer": "ik_smart",
"type": "best_fields",
"operator": "and",
"fields": ["customerName","customerCellPhone"]
}
}
]
}
},
"from": 0,
"size": 10,
"sort": {
"creationtime": {
"order": "desc"
}
}
}
{
"query": {
"bool": {
"must": [
{
"match": {
"customerName": {
"analyzer": "ik_smart",
"query": "AAA",
"operator": "and"
}
}
},
{
"match": {
"customerCellPhone": {
"analyzer": "ik_smart",
"query": "A",
"operator": "and"
}
}
},
{
"match": {
"fullCarNo": {
"analyzer": "ik_smart",
"query": "A",
"operator": "and"
}
}
}
]
}
},
"from": 0,
"size": 10,
"sort": {
"creationtime": {
"order": "desc"
}
}
}

2.3.3、详情查询

  • _doc/{_id} 根据id查询详情
1
GET customer_remind/_doc/15748138447761588306

2.3.4、filter与must,term与match的区别

2.3.4.1、根据字段类型不同

  • 对于 keyword类型的字段而言, 用 term 和 match 都是可以查询的;
  • 但对于 text 类型的分词字段而言,只能用match才能够查询到结果;

2.3.4.2、根据嵌套类型查询(filter 与 must 是属于同一个级别的查询方式,都可以作为 query->bool 的属性)

  • filter: 不计算评分,查询效率高、有缓存。(推荐)

​ term: 精确匹配

​ match: 模糊匹配、倒排索引

  • must: 要计算评分,查询效率低、无缓存

​ term: 精确匹配、 要评分

​ match:模糊匹配、 要评分

2.4、删除

2.4.1、删除索引下所有的数据

1
2
3
4
5
6
POST /customer_remind/_delete_by_query
{
"query": {
"match_all": {}
}
}

2.4.2、根据id删除单条数据

1
DELETE /customer_remind/_doc/15748138447761588306

2.5、更新

2.5.1、局部更新

1
2
3
4
5
6
POST /customer_remind/_doc/15748138447761588306/_update
{
"doc": {
"customerName": "名"
}
}

2.5.2、全量更新

1
2
3
4
POST /customer_remind/_doc/15748138447761588306
{
"customerName": "名ss"
}

2.6、批量插入

  • 每两行为一组,第一行指定索引id(也可为空),第二行为实际的数据体。如果id为空:{“index”:{}}
1
2
3
4
5
POST /crm_channel_customer/_bulk
{"index":{"_id":"998"}}
{"imgUrl":"","groupId":"168769212699901969","nickname":"","cellphone":"","dialogueLastTime":"2021-11-25 19:20:58","userTypes":[{"deleteFlag":"0","subscribeTime":"2019-09-16 16:28:07","id":"998","userType":"3"}],"docUpdateTime":"2022-05-16 03:00:01","id":33657,"delFlag":"0"}
{"index":{"_id":"999"}}
{"imgUrl":"","groupId":"168769212699901969","nickname":"","cellphone":"","dialogueLastTime":"2021-11-25 19:20:58","userTypes":[{"deleteFlag":"0","subscribeTime":"2019-09-16 16:28:07","id":"999","userType":"3"}],"docUpdateTime":"2022-05-16 03:00:01","id":33657,"delFlag":"0"}

3、备注

3.1、浏览器插件

Elasticvue

ElasticSearch Head

3.2、配置

img