ES操作模版

下面是一些ES日常操作,记录下来方便查询

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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#查看集群的健康情况
GET /_cat/health?v

#查看节点的情况
GET /_cat/nodes?v

#查询各个索引状态
GET /_cat/indices?v

#创建索引
PUT /movie_index

#查看某一个索引的分片情况
GET /_cat/shards/movie_index?v

#删除索引
DELETE /movie_index

#创建文档
PUT /movie_index/movie/1
{ "id":100,
"name":"operation red sea",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"zhang yi"},
{"id":2,"name":"hai qing"},
{"id":3,"name":"zhang han yu"}
]
}

PUT /movie_index/movie/2
{
"id":200,
"name":"operation meigong river",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"zhang han yu"}
]
}

PUT /movie_index/movie/3
{
"id":300,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"zhang san feng"}
]
}

#查询某一个索引中的全部文档
GET /movie_index/_search

#根据id查询某一个文档
GET /movie_index/movie/3

#根据文档id,删除某一个文档
DELETE /movie_index/movie/3

POST /_forcemerge

#put 对已经存在的文档进行替换(幂等性)
PUT /movie_index/movie/3
{
"id":300,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"zhang cuishan"}
]
}


#post 进行新增操作,无法保证幂等性
#根据主键保证幂等性
POST /movie_index/movie/
{
"id":300,
"name":"incident red sea",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"zhang cuishan111"}
]
}


GET /movie_index/_search

GET /movie_index/movie/1

POST /movie_index/movie/1/_update
{
"doc": { "name": "新的字段值" }
}


POST /movie_index/_update_by_query
{
"query": {
"match":{
"actorList.id":1
}
},
"script": {
"lang": "painless",
"source":"for(int i=0;i<ctx._source.actorList.length;i++){if(ctx._source.actorList[i].id==3){ctx._source.actorList[i].name='tttt'}}"
}
}

GET /movie_index/_search

POST /movie_index/_delete_by_query
{
"query": {
"match_all": {}
}
}

#批量添加两个document
POST /movie_index/movie/_bulk
{"index":{"_id":66}}
{"id":300,"name":"incident red sea","doubanScore":5.0,"actorList":[{"id":4,"name":"zhang cuishan"}]}
{"index":{"_id":88}}
{"id":300,"name":"incident red sea","doubanScore":5.0,"actorList":[{"id":4,"name":"zhang cuishan"}]}




POST /movie_index/movie/_bulk
{"update":{"_id":"66"}}
{"doc": { "name": "wudangshanshang" } }
{"delete":{"_id":"88"}}


#------------------查询操作--------------------
#查询出当前索引中的全部数据
GET /movie_index/_search

GET /movie_index/_search?q=_id:66

#查询全部
GET /movie_index/_search
{
"query": {
"match_all": {}
}
}

#根据电影的名称进行查询
GET /movie_index/_search
{
"query": {
"match": {
"name": "operation red sea"
}
}
}

GET /movie_index

#按分词进行查询
GET /movie_index/_search
{
"query": {
"match": {
"actorList.name": "zhang han yu"
}
}
}

#按短语查询 相当于like
GET /movie_index/_search
{
"query": {
"match_phrase": {
"actorList.name": "zhang han yu"
}
}
}

#不分词 通过精准匹配进行查询 term精准匹配
GET /movie_index/_search
{
"query": {
"term": {
"actorList.name.keyword":"zhang han yu"
}
}
}


#容错匹配
GET /movie_index/_search
{
"query": {
"fuzzy": {
"name": "radd"
}
}
}

#先匹配 再过滤
GET /movie_index/_search
{
"query": {
"match": {
"name": "red"
}
},
"post_filter": {
"term": {
"actorList.id": "3"
}
}
}


#匹配和过滤同时
GET /movie_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "red"
}
}
],
"filter": {
"term": {
"actorList.id": "3"
}
}
}
}
}

#范围过滤 ,将豆瓣评分在6到9的文档查询出来
GET /movie_index/_search
{
"query": {
"range": {
"doubanScore": {
"gte": 6,
"lte": 9
}
}
}
}


#按照豆瓣评分降序排序
GET /movie_index/_search
{
"query": {
"match": {
"name": "red"
}
},
"sort": [
{
"doubanScore": {
"order": "asc"
}
}
]
}

#分页查询
GET /movie_index/_search
{
"from": 0,
"size": 2
}

#查询指定字段
GET /movie_index/_search
{
"_source": ["name", "doubanScore"]
}

#高亮显示
GET /movie_index/_search
{
"query": {
"match": {
"name": "red"
}
},
"highlight": {
"fields": {"name":{} },
"pre_tags": "<a>",
"post_tags": "</a>"
}
}


#需求1:取出每个演员共参演了多少部电影
#aggs 聚合
#term 精准匹配
#terms 聚合操作,相当于groupBy
GET /movie_index/_search
{
"aggs": {
"myAggs": {
"terms": {
"field": "actorList.name.keyword",
"size": 10
}
}
}
}

#需求2:每个演员参演电影的平均分是多少,并按评分排序

GET /movie_index/_search
{
"aggs": {
"groupByName": {
"terms": {
"field": "actorList.name.keyword",
"size": 10,
"order": {
"avg_score": "asc"
}
},
"aggs": {
"avg_score": {
"avg": {
"field": "doubanScore"
}
}
}
}
}
}


#分词
#英文默认分词规则
GET /_analyze
{
"text": "hello world"
}

#中文默认分词规则
GET /_analyze
{
"text": "蓝瘦香菇",
"analyzer": "ik_smart"
}

GET /_analyze
{
"text": "蓝瘦香菇",
"analyzer": "ik_max_word"
}


GET /movie_index



#自动定义mapping
PUT /movie_chn_1/movie/1
{ "id":1,
"name":"红海行动",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"张译"},
{"id":2,"name":"海清"},
{"id":3,"name":"张涵予"}
]
}
PUT /movie_chn_1/movie/2
{
"id":2,
"name":"湄公河行动",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"张涵予"}
]
}

PUT /movie_chn_1/movie/3
{
"id":3,
"name":"红海事件",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"张三丰"}
]
}

GET /movie_chn_1/_search
{
"query": {
"match": {
"name": "海行"
}
}
}

GET /movie_chn_1/_mapping


#自定义mapping
DELETE movie_chn_2

PUT movie_chn_2
{
"mappings": {
"movie":{
"properties": {
"id":{
"type": "long"
},
"name":{
"type": "keyword"
},
"doubanScore":{
"type": "double"
},
"actorList":{
"properties": {
"id":{
"type":"long"
},
"name":{
"type":"keyword"
}
}
}
}
}
}
}


PUT /movie_chn_2/movie/1
{ "id":1,
"name":"红海行动",
"doubanScore":8.5,
"actorList":[
{"id":1,"name":"张译"},
{"id":2,"name":"海清"},
{"id":3,"name":"张涵予"}
]
}
PUT /movie_chn_2/movie/2
{
"id":2,
"name":"湄公河行动",
"doubanScore":8.0,
"actorList":[
{"id":3,"name":"张涵予"}
]
}

PUT /movie_chn_2/movie/3
{
"id":3,
"name":"红海事件",
"doubanScore":5.0,
"actorList":[
{"id":4,"name":"张三丰"}
]
}

GET /movie_chn_2/_mapping

GET /movie_chn_2/_search
{
"query": {
"match": {
"name": "海行"
}
}
}

POST /_reindex
{
"source": {}
, "dest": {}
}

#创建索引 并指定别名
PUT movie_chn_3
{
"aliases": {
"movie_chn_3_aliase": {}
},
"mappings": {
"movie":{
"properties": {
"id":{
"type": "long"
},
"name":{
"type": "text",
"analyzer": "ik_smart"
},
"doubanScore":{
"type": "double"
},
"actorList":{
"properties": {
"id":{
"type":"long"
},
"name":{
"type":"keyword"
}
}
}
}
}
}
}


GET /_cat/indices



GET /_cat/aliases


POST /_aliases
{
"actions": [
{
"add": {
"index": "movie_chn_3",
"alias": "movie_chn_3_a2"
}
}
]
}


GET /movie_chn_3/_search

GET /movie_chn_3_a2/_search

GET /_cat/aliases

POST /_aliases
{
"actions": [
{
"remove": {"index": "movie_chn_3","alias": "movie_chn_3_a2"}
}
]
}


GET /movie_chn_1/_search

GET /movie_chn_2/_search



POST _aliases
{
"actions": [
{ "add": { "index": "movie_chn_1", "alias": "movie_chn_query" }},
{ "add": { "index": "movie_chn_2", "alias": "movie_chn_query" }}
]
}


GET /movie_chn_query/_search


POST _aliases
{
"actions": [
{
"add":
{
"index": "movie_chn_1",
"alias": "movie_chn_1_sub_query",
"filter": {
"term": { "actorList.id": "4"}
}
}
}
]
}

GET /movie_chn_1_sub_query/_search


POST /_aliases
{
"actions": [
{ "remove": { "index": "movie_chn_1", "alias": "movie_chn_query" }},
{ "remove": { "index": "movie_chn_2", "alias": "movie_chn_query" }},
{ "add": { "index": "movie_chn_3", "alias": "movie_chn_query" }}
]
}


#创建模板
PUT _template/template_movie0523
{
"index_patterns": ["movie_test*"],
"settings": {
"number_of_shards": 1
},
"aliases" : {
"{index}-query": {},
"movie_test-query":{}
},
"mappings": {
"_doc": {
"properties": {
"id": {
"type": "keyword"
},
"movie_name": {
"type": "text",
"analyzer": "ik_smart"
}
}
}
}
}

POST movie_test_202010/_doc
{
"id":"333",
"name":"zhang3"
}

GET /movie_test_202010-query/_mapping


GET /_cat/templates

GET /_template/template_movie*