forked from jolicode/elasticsearch-cheatsheet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1240 lines (1054 loc) · 38.9 KB
/
index.html
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>Elasticsearch Cheat Sheet for developers</title>
<meta name="description" content="Elasticsearch API cheatsheet for developers with copy and paste example for the most useful APIs">
<link rel="shortcut icon" href="./favicon.ico"/>
<link rel="canonical" href="https://elasticsearch-cheatsheet.jolicode.com/" />
<link rel="stylesheet" type="text/css" href="./semantic-UI/semantic.min.css">
<script src="./javascripts/jquery-2.2.4.min.js"></script>
<script src="./javascripts/clipboard.min.js"></script>
<script src="./semantic-UI/semantic.min.js"></script>
<link href="./stylesheets/prism.css" rel="stylesheet" />
<style type="text/css">
.toolbar-item button {
color: #EEE !important;
background-color: #333 !important;
padding: 5px !important;;
}
pre[class*="language-"] {
padding-bottom: 0.5em;
padding-top: 0.5em;
}
</style>
<script>
$(document)
.ready(function() {
// fix main menu to page on passing
$('.main.menu').visibility({
type: 'fixed'
});
// show dropdown on hover
$('.main.menu .ui.dropdown').dropdown({
on: 'hover'
});
function updateElasticsearchVersion() {
if (location.hash.match(/#es[0-9]+/) !== null) {
var className = location.hash.replace('#', '');
document.body.className = className;
//window.scrollTo(0, 0);
}
}
window.onhashchange = updateElasticsearchVersion;
updateElasticsearchVersion();
})
;
</script>
<style type="text/css">
body {
background-color: #FFFFFF;
}
.main.container {
margin-top: 2em;
}
/*See https://github.com/Semantic-Org/Semantic-UI/issues/4342*/
.divider-column {
position: relative;
padding: 0 !important;
}
pre code { display: block; }
pre + pre { padding-top: 0 !important; }
pre[class*="language-"] { padding-bottom: 0.5em; }
code.language-json:not(.json-only):first-line { font-weight: bold; color: black; }
body.es1 .es2:not(.es1), body.es1 .es5:not(.es1), body.es1 .es6:not(.es1), body.es1 .es7:not(.es1), body.es1 .es8:not(.es1) {
display: none!important;
}
body.es2 .es1:not(.es2), body.es2 .es5:not(.es2), body.es2 .es6:not(.es2), body.es2 .es7:not(.es2), body.es2 .es8:not(.es2) {
display: none!important;
}
body.es5 .es1:not(.es5), body.es5 .es2:not(.es5), body.es5 .es6:not(.es5), body.es5 .es7:not(.es5), body.es5 .es8:not(.es5) {
display: none!important;
}
body.es6 .es1:not(.es6), body.es6 .es2:not(.es6), body.es6 .es5:not(.es6), body.es6 .es7:not(.es6), body.es6 .es8:not(.es6) {
display: none!important;
}
body.es7 .es1:not(.es7), body.es7 .es2:not(.es7), body.es7 .es5:not(.es7), body.es7 .es6:not(.es7), body.es7 .es8:not(.es7) {
display: none!important;
}
body.es8 .es1:not(.es8), body.es8 .es2:not(.es8), body.es8 .es5:not(.es8), body.es8 .es6:not(.es8), body.es8 .es7:not(.es8) {
display: none!important;
}
</style>
</head>
<body class="es8">
<div class="ui main text container">
<img class="es2 es5 es6 es7 es8 ui tiny right floated image" src="./images/Elasticsearch-Logo-Color-V-sm.png">
<img class="es1 ui tiny right floated image" src="./images/Elasticsearch1-Logo-Color-V-sm.png">
<h1 class="ui header">🔎 Elasticsearch
<span class="es1">1.x</span>
<span class="es2">2.x</span>
<span class="es5">5.x</span>
<span class="es6">6.x</span>
<span class="es7">7.x</span>
<span class="es8">8.x</span>
Cheatsheet 🔍</h1>
<p>
All the API endpoints and pro-tips you always forgot about in one place!<br />
Built by developers for developers. Hosted on <a href="https://github.com/jolicode/elasticsearch-sheatsheet">GitHub</a>, contributions welcome.
</p>
</div>
<div class="ui borderless main menu">
<div class="ui text container">
<a href="#links" class="item">Links</a>
<a href="#queries" class="item">Queries</a>
<a href="#indexes" class="item">Indexes</a>
<a href="#debug" class="item">Debug</a>
<a href="#cluster" class="item">Cluster & Plugins</a>
<div class="ui right floated dropdown item js-version-selector">
<span class="es1">Elasticsearch 1.X</span>
<span class="es2">Elasticsearch 2.X</span>
<span class="es5">Elasticsearch 5.X</span>
<span class="es6">Elasticsearch 6.X</span>
<span class="es7">Elasticsearch 7.X</span>
<span class="es8">Elasticsearch 8.X</span>
<i class="dropdown icon"></i>
<div class="menu">
<div class="item es2 es5 es6 es7 es8"><a href="#es1">Elasticsearch 1.X</a></div>
<div class="item es1 es5 es6 es7 es8"><a href="#es2">Elasticsearch 2.X</a></div>
<div class="item es1 es2 es6 es7 es8"><a href="#es5">Elasticsearch 5.X</a></div>
<div class="item es1 es2 es5 es7 es8"><a href="#es6">Elasticsearch 6.X</a></div>
<div class="item es1 es2 es5 es6 es8"><a href="#es7">Elasticsearch 7.X</a></div>
<div class="item es1 es2 es5 es6 es7"><a href="#es8">Elasticsearch 8.X</a></div>
</div>
</div>
</div>
</div>
<div class="ui text container language-json">
<div class="es1 ui secondary orange segment">
<p>Elasticsearch 1.7 is End Of Life. Consider upgrading.
<a href="https://www.elastic.co/support/eol">More information about supported versions</a>.</p>
</div>
<div class="es2 ui secondary orange segment">
<p>Elasticsearch 2.4 is End Of Life. Consider upgrading.
<a href="https://www.elastic.co/support/eol">More information about supported versions</a>.</p>
</div>
<div class="es5 ui secondary orange segment">
<p>Elasticsearch 5.6 is End Of Life. Consider upgrading.
<a href="https://www.elastic.co/support/eol">More information about supported versions</a>.</p>
</div>
<div class="es6 ui secondary orange segment">
<p>Elasticsearch 6.8 is End Of Life. Consider upgrading.
<a href="https://www.elastic.co/support/eol">More information about supported versions</a>.</p>
</div>
<a name="links"></a>
<h2 class="ui dividing header">Links</h2>
<p class="es2">
First thing, forget about your <code>curl</code> calls and <strong>install <a href="https://www.elastic.co/guide/en/sense/current/installing.html">Sense</a> please!</strong>
</p>
<p class="es5 es6 es7 es8">
First thing, forget about your <code>curl</code> calls and <strong>install <a href="https://www.elastic.co/products/kibana">Kibana</a> please!</strong>
</p>
<ul>
<li class="es1"><a href="https://www.elastic.co/guide/en/elasticsearch/guide/1.x/index.html">Elasticsearch: The Definitive Guide</a>, official book;</li>
<li class="es2"><a href="https://www.elastic.co/guide/en/elasticsearch/guide/2.x/index.html">Elasticsearch: The Definitive Guide</a>, official book;</li>
<li class="es5 es6"><a href="https://www.elastic.co/guide/en/elasticsearch/guide/master/index.html">Elasticsearch: The Definitive Guide</a>, official book (not up to date);</li>
<li class="es1 es2 es5 es6"><a href="https://www.elastic.co/downloads/past-releases/#elasticsearch">Download Elasticsearch</a>, official download page;</li>
<li class="es7 es8"><a href="https://www.elastic.co/downloads/elasticsearch">Download Elasticsearch</a>, official download page;</li>
<li class="es6 es7"><a href="https://www.elastic.co/downloads/past-releases#elasticsearch-oss">Download pure OSS Elasticsearch</a> (<= 7.10.2 only), Apache licensed download page;</li>
<li class="es1"><a href="https://www.elastic.co/guide/en/elasticsearch/reference/1.7/index.html">Elasticsearch Reference</a>, official documentation;</li>
<li class="es2"><a href="https://www.elastic.co/guide/en/elasticsearch/reference/2.3/index.html">Elasticsearch Reference</a>, official documentation;</li>
<li class="es5"><a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/index.html">Elasticsearch Reference</a>, official documentation;</li>
<li class="es6"><a href="https://www.elastic.co/guide/en/elasticsearch/reference/6.x/index.html">Elasticsearch Reference</a>, official documentation;</li>
<li class="es7"><a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.x/index.html">Elasticsearch Reference</a>, official documentation;</li>
<li class="es8"><a href="https://www.elastic.co/guide/en/elasticsearch/reference/8.1/index.html">Elasticsearch Reference</a>, official documentation;</li>
<li class="es5 es6 es7 es8"><a href="https://www.docker.elastic.co/">Docker repository</a>, quick start any Elastic software;</li>
<li><a href="https://dev.to/t/elasticsearch">Dev.to Elasticsearch</a>, great source of content about Elastic;</li>
<li><a href="https://discuss.elastic.co/">Official forum</a> and <a href="http://stackoverflow.com/questions/tagged/elasticsearch">StackOverflow</a> for support;</li>
<li class="es5"><a href="https://www.elastic.co/products/upgrade_guide">Official online migration tool</a> to help upgrading the stack to 6.x;</li>
<li class="es5 es6 es7"><a href="https://github.com/moshe/elasticsearch-comrade">Comrade</a>: a Python based Elasticsearch web admin and monitoring panel;</li>
<li class="es2 es5 es6 es7"><a href="https://github.com/lmenezes/cerebro">Cerebro</a>: a Java based Elasticsearch web admin;</li>
</ul>
<a name="queries"></a>
<h2 class="ui dividing header">Queries</h2>
<p>
There are two syntaxes for the basic queries: a simple one on the left, where you can't use any option,
and an extended one on the right. Most of the beginner headache with the DSL come from this:
</p>
<div class="ui two column very relaxed grid" style="position:relative;">
<div class="column">
<pre><code>GET _search
{
"query": {
"match": {
"FIELD": "TEXT"
}
}
}</code></pre>
</div>
<div class="divider-column">
<div class="ui vertical divider">
to
</div>
</div>
<div class="column">
<pre><code>GET _search
{
"query": {
"match": {
"FIELD": {
"query": "TEXT",
"OPTION": "VALUE"
}
}
}
}</code></pre>
</div>
</div>
<h3 class="ui header">Full search example with aggregation, highlight, filter...</h3>
<pre class="es1"><code>GET /_search
{
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"match": {
"title": "smith"
}
}
],
"must_not": [
{
"match_phrase": {
"title": "granny smith"
}
}
]
}
},
"filter": {
"bool": {
"must_not": [
{
"missing": {
"field": "title"
}
}
]
}
}
}
},
"aggs": {
"my_agg": {
"terms": {
"field": "user",
"size": 10
}
}
},
"highlight": {
"pre_tags" : ["<em>"],
"post_tags" : ["</em>"],
"fields": {
"body" : {
"number_of_fragments": 1,
"fragment_size": 20
},
"title": {}
}
},
"size": 20,
"from": 100,
"_source": ["title", "id"],
"sort": [
{ "_id" : {"order" : "desc"}}
]
}</code></pre>
<pre class="es2 es5 es6 es7 es8"><code>GET /_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "smith"
}
}
],
"must_not": [
{
"match_phrase": {
"title": "granny smith"
}
}
],
"filter": [
{
"exists": {
"field": "title"
}
}
]
}
},
"aggs": {
"my_agg": {
"terms": {
"field": "user",
"size": 10
}
}
},
"highlight": {
"pre_tags": [
"<em>"
],
"post_tags": [
"</em>"
],
"fields": {
"body": {
"number_of_fragments": 1,
"fragment_size": 20
},
"title": {}
}
},
"size": 20,
"from": 100,
"_source": [
"title",
"id"
],
"sort": [
{
"_id": {
"order": "desc"
}
}
]
}</code></pre>
<h3 class="ui header es7 es8">Control total hit count</h3>
<p class="es7 es8">Accept true, false or a fixed number, default to 10000.</p>
<pre class="es7 es8"><code>GET /_search
{
"track_total_hits": true,
"query": {}
}</code></pre>
<h3 class="ui header">Common queries</h3>
<div class="ui grid">
<div class="eight wide column"><pre><code class="json-only">"multi_match": {
"query": "Elastic",
"fields": ["user.*", "title^3"],
"type": "best_fields"
}</code></pre></div>
<div class="eight wide column es2 es5 es6 es7 es8"><pre><code class="json-only">"bool": {
"must": [],
"must_not": [],
"filter": [],
"should": [],
"minimum_should_match" : 1
}</code></pre></div>
<div class="eight wide column es1"><pre><code class="json-only">"bool": {
"must": [],
"must_not": [],
"should": [],
"minimum_should_match" : 1
}</code></pre></div>
<div class="eight wide column es1"><pre><code class="json-only">"filtered": {
"query": {
"match_all": {}
},
"filter": {
"range": {...}
}
}
</code></pre></div>
<div class="eight wide column"><pre><code class="json-only">"range": {
"age": {
"gte": 10,
"lte": 20,
"boost": 2
}
}</code></pre></div>
</div>
<h3 class="ui header">QueryString syntax</h3>
<p>
Search in the default <code>_all</code> field:
</p>
<pre><code>GET /_search?q=pony</code></pre>
<p>
Complex search with operator and exact phrase search with boost:
</p>
<pre><code>GET /_search?q=title:(joli OR code) AND author:"Damien Alexandre"^2</code></pre>
<p>
Search with wildcard and special queries:
</p>
<pre><code>GET /_search?q=_exists_:title OR title:singl? noneOrAnyChar*cter</code></pre>
<p>
Search with fuzzyness and range:
</p>
<pre><code>GET /_search?q=title:elastichurch~3 AND date:[2016-01-01 TO 2018-12-31]</code></pre>
<p>
Use in Query DSL (not recommended for user search):
</p>
<pre><code>GET /_search
{
"query": {
"query_string": {
"default_field": "content",
"query": "elastic AND (title:lucene OR title:solr)"
}
}
}</code></pre>
<h3 class="ui header">Search After - Pagination cursor</h3>
<p>
Search with a custom sort:
</p>
<pre><code>GET products/_search
{
"size": 10,
"sort": [
{"date": "asc"},
{"_id": "desc"}
]
}</code></pre>
<p>
On the next "page", pass the sort values from the last result:
</p>
<pre><code>GET product/_search
{
"size": 10,
"search_after": [1463538857, "654323"],
"sort": [
{"date": "asc"},
{"_id": "desc"}
]
}</code></pre>
<a name="indexes"></a>
<h2 class="ui dividing header">Indexes and mapping</h2>
<h3 class="ui header">Create an index with settings and mapping</h3>
<pre class="es1 es2"><code>PUT /my_index_name
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 3,
"analysis": {},
"refresh_interval": "1s"
},
"mappings": {
"my_type_name": {
"dynamic": false,
"properties": {
"title": {
"type": "string",
"analyzer": "english"
}
}
}
}
}</code></pre>
<pre class="es5 es6"><code>PUT /my_index_name
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 3,
"analysis": {},
"refresh_interval": "1s"
},
"mappings": {
"my_type_name": {
"dynamic": false,
"properties": {
"title": {
"type": "text",
"analyzer": "english"
}
}
}
}
}</code></pre>
<pre class="es7 es8"><code>PUT /my_index_name
{
"settings": {
"number_of_replicas": 1,
"number_of_shards": 3,
"analysis": {},
"refresh_interval": "1s"
},
"mappings": {
"dynamic": false,
"properties": {
"title": {
"type": "text",
"analyzer": "english"
}
}
}
}</code></pre>
<div class="es6 es7 ui secondary orange segment">
<p><a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/removal-of-types.html">Types are deprecated</a>, you can only use one in Elasticsearch 6.</p>
</div>
<h3 class="ui header">Update index settings dynamically</h3>
<pre><code>PUT /my_index_name/_settings
{
"index": {
"refresh_interval": "-1",
"number_of_replicas": 0
}
}</code></pre>
<h3 class="ui header">Update an index by adding a field to a type</h3>
<pre class="es1 es2"><code>PUT /my_index_name/_mapping/my_type_name
{
"my_type_name": {
"properties": {
"tag": {
"type": "string",
"index": "not_analyzed"
}
}
}
}</code></pre>
<pre class="es5 es6"><code>PUT /my_index_name/_mapping/my_type_name
{
"my_type_name": {
"properties": {
"tag": {
"type": "keyword"
}
}
}
}</code></pre>
<pre class="es7 es8"><code>PUT /my_index_name/_mapping
{
"properties": {
"tag": {
"type": "keyword"
}
}
}</code></pre>
<h3 class="ui header">Get the mapping and the settings</h3>
<pre><code>GET /my_index_name</code></pre>
<pre><code>GET /my_index_name/_mapping</code></pre>
<pre><code>GET /my_index_name/_settings</code></pre>
<h3 class="ui header">Create a document (auto-generated ID)</h3>
<pre class="es1 es2 es5 es6"><code>POST /my_index_name/my_type_name
{
"title": "Elastic is funny",
"tag": [
"lucene"
]
}</code></pre>
<pre class="es7 es8"><code>POST /my_index_name/_doc
{
"title": "Elastic is funny",
"tag": [
"lucene"
]
}</code></pre>
<h3 class="ui header">Get a document</h3>
<pre class="es1 es2 es5 es6"><code>GET /my_index_name/my_type_name/12abc</code></pre>
<pre class="es7 es8"><code>GET /my_index_name/_doc/12abc</code></pre>
<h3 class="ui header">Create or update a document</h3>
<pre class="es1 es2 es5 es6"><code>PUT /my_index_name/my_type_name/12abc
{
"title": "Elastic is funny",
"tag": [
"lucene"
]
}</code></pre>
<pre class="es7 es8"><code>PUT /my_index_name/_doc/12abc
{
"title": "Elastic is funny",
"tag": [
"lucene"
]
}</code></pre>
<h3 class="ui header">Delete a document</h3>
<pre class="es1 es2 es5 es6"><code>DELETE /my_index_name/my_type_name/12abc</code></pre>
<pre class="es7 es8"><code>DELETE /my_index_name/_doc/12abc</code></pre>
<h3 class="ui header">Open and close indexes to save memory and CPU</h3>
<pre><code>POST /my_index_name/_close</code></pre>
<pre><code>POST /my_index_name/_open</code></pre>
<h3 class="ui header">Remove and create aliases</h3>
<pre><code>POST /_aliases
{
"actions": [
{
"remove": {
"index": "my_index_name",
"alias": "foo"
}
},
{
"add": {
"index": "my_index_name",
"alias": "bar",
"filter" : { "term" : { "user" : "damien" } }
}
}
]
}</code></pre>
<h3 class="ui header">List aliases</h3>
<pre><code>GET /_aliases</code></pre>
<pre><code>GET /my_index_name/_alias/*</code></pre>
<pre><code>GET /*/_alias/*</code></pre>
<pre><code>GET /*/_alias/foo</code></pre>
<h3 class="ui header">Full custom analyzer declaration</h3>
<pre><code>PUT /english_example
{
"settings": {
"analysis": {
"filter": {
"english_stop": {
"type": "stop",
"stopwords": "_english_"
},
"english_stemmer": {
"type": "stemmer",
"language": "english"
}
},
"analyzer": {
"my_english": {
"char_filter": ["html_strip"],
"tokenizer": "standard",
"filter": [
"lowercase",
"english_stop",
"english_stemmer"
]
}
}
}
}
}</code></pre>
<h3 class="ui header es1 es2">Create and list index warmer</h3>
<p class="es1 es2">
Create a query that will be played on every new document:
</p>
<pre class="es1 es2"><code>PUT /my_index_name/_warmer/my_warmer
{
"query": {
"match_all": {}
},
"aggs": {
"costly_aggs": {
"terms": {
"field": "title"
}
}
}
}</code></pre>
<pre class="es1 es2"><code>GET /my_index_name/_warmer</code></pre>
<pre class="es1 es2"><code>GET /my_index_name/_warmer/*</code></pre>
<h3 class="ui header">Indices monitoring and information</h3>
<pre><code>GET /my_index_name/_stats</code></pre>
<pre><code>GET /my_index_name/_segments</code></pre>
<pre><code>GET /my_index_name/_recovery?pretty&human</code></pre>
<pre class="es1 es2"><code>GET /my_index_name/_warmer/*</code></pre>
<h3 class="ui header">Indices status and management</h3>
<pre><code>POST /my_index_name/_cache/clear</code></pre>
<pre><code>POST /my_index_name/_refresh</code></pre>
<pre><code>POST /my_index_name/_flush</code></pre>
<pre class="es1"><code>POST /my_index_name/_optimize</code></pre>
<pre class="es2 es5 es6 es7 es8"><code>POST /my_index_name/_forcemerge</code></pre>
<div class="es2 es5 es6 es7 es8">
<h2 class="ui dividing header">Reindex API</h2>
<h3 class="ui header">Simple Reindex Operation</h3>
<pre class=""><code>POST /_reindex
{
"source": {
"index": "test-index"
},
"dest": {
"index": "test-index-new"
}
}</code></pre>
<h3 class="ui header">Selective Reindex Operation</h3>
<pre><code>POST /_reindex
{
"source": {
"index": "test-index",
"query": {
"match": {
"gender": "female"
}
}
},
"dest": {
"index": "test-index-new",
"type": "female"
}
}</code></pre>
</div>
<a name="debug"></a>
<h2 class="ui dividing header">Debug and development</h2>
<h3 class="ui header">Queries</h3>
<p>Get a detailed view of what a query do:</p>
<pre><code>GET /blog/_validate/query?explain=true
{
"query": {
"match": {
"title": "Smith"
}
}
}</code></pre>
<p>Get an explanation about a document matching or not:</p>
<pre class="es1 es2 es5 es6"><code>GET /blog/post/1/_explain
{
"query": {
"match": {
"title": "Smith"
}
}
}</code></pre>
<pre class="es7 es8"><code>GET /blog/_explain/1
{
"query": {
"match": {
"title": "Smith"
}
}
}</code></pre>
<h3 class="ui header">Analysis</h3>
<p>Test how a content is tokenized in a field:</p>
<pre><code>GET /blog/_analyze
{
"field": "title",
"text": "powerful"
}</code></pre>
<p>Test analyzer token output by analyzer:</p>
<pre><code>GET /blog/_analyze
{
"analyzer": "english",
"text": "powerful"
}</code></pre>
<h3 class="ui header">Slowlog</h3>
<p>Lower the slowlog threshold to see all the search queries in the logs:</p>
<pre><code>PUT /blog/_settings
{
"index.search.slowlog.threshold.query.trace": "0s",
"index.search.slowlog.level": "trace"
}</code></pre>
<p>Go back to the default configuration:</p>
<pre><code>PUT /blog/_settings
{
"index.search.slowlog.threshold.query.trace": "500ms",
"index.search.slowlog.level": "info"
}</code></pre>
<a name="cluster"></a>
<h2 class="ui dividing header">Cluster management and plugins</h2>
<div class="es5 es6 es7 es8">
<h3 class="ui header">Running with Docker</h3>
<p>Elasticsearch:</p>
<pre class="es8"><code>docker network create elastic
docker run --rm -it --name "elastically_es" -e "discovery.type=single-node" --net elastic docker.elastic.co/elasticsearch/elasticsearch:8.1.2</code></pre>
<p class="es8">Have a look at the output for password and enrollment token!</p>
<pre class="es7"><code>docker run --rm -it --name "elastically_es" -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.17.2</code></pre>
<pre class="es6"><code>docker run -d --name "elastically_es" -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:6.8.6</code></pre>
<pre class="es5"><code>docker run -d --name "elastically_es" -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:5.6.16</code></pre>
<p>Kibana:</p>
<pre class="es8"><code>docker run --rm -it --name "elastically_kibana" --net elastic -p 5601:5601 docker.elastic.co/kibana/kibana:8.1.2</code></pre>
<pre class="es7"><code>docker run -e "ELASTICSEARCH_HOSTS=http://127.0.0.1:9200/" --network host docker.elastic.co/kibana/kibana:7.17.2</code></pre>
<pre class="es6"><code>docker run -e "ELASTICSEARCH_HOSTS=http://127.0.0.1:9200/" --network host docker.elastic.co/kibana/kibana:6.8.6</code></pre>
<pre class="es5"><code>docker run -e "ELASTICSEARCH_HOSTS=http://127.0.0.1:9200/" --network host docker.elastic.co/kibana/kibana:5.6.16</code></pre>
</div>
<h3 class="ui header">Cluster and node information</h3>
<pre><code>GET /_cluster/health?pretty</code></pre>
<pre><code>GET /_cluster/health?wait_for_status=yellow&timeout=50s</code></pre>
<pre><code>GET /_cluster/state</code></pre>
<pre><code>GET /_cluster/stats?human&pretty</code></pre>
<pre><code>GET /_cluster/pending_tasks</code></pre>
<pre><code>GET /_nodes</code></pre>
<pre><code>GET /_nodes/stats</code></pre>
<pre><code>GET /_nodes/nodeId1,nodeId2/stats</code></pre>
<pre class="es1"><code>GET /_shutdown 😱</code></pre>
<p>Get the full reference of <strong>all</strong> the settings:</p>
<pre><code>GET /_cluster/settings?include_defaults=true&flat_settings=true</code></pre>
<h3 class="ui header">Moving shards manually</h3>
<p>
Ask the index my_index_name shard 0 of node1 to go to node2:
</p>
<pre><code>POST /_cluster/reroute
{
"commands": [
{
"move": {
"index": "my_index_name",
"shard": 0,
"from_node": "node1",
"to_node": "node2"
}
},
{
"allocate": {
"index": "my_index_name",
"shard": 1,
"node": "node3"
}
}
]
}</code></pre>
<h3 class="ui header">Updating settings</h3>
<div class="es1 es2 es5 es6">
<p>
Change dynamically the minimum number of nodes to allow a master election, both persistent or not:
</p>
<pre><code>PUT /_cluster/settings
{
"persistent": {
"discovery.zen.minimum_master_nodes": 3
}
}</code></pre>
<pre><code>PUT /_cluster/settings
{
"transient": {
"discovery.zen.minimum_master_nodes": 2
}
}</code></pre>
</div>
<p>
Disable shard allocation, useful before a rolling restart:
</p>
<pre><code>PUT /_cluster/settings
{
"transient" : {
"cluster.routing.allocation.enable" : "none"
}
}</code></pre>
<pre><code>PUT /_cluster/settings
{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}</code></pre>
<h3 class="ui header">Snapshots and Restore</h3>
<pre><code>PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "my_backup_location"
}
}</code></pre>
<pre><code>PUT /_snapshot/my_backup/snapshot_a
{
"indices": "index_1,index_2",
"ignore_unavailable": "true",
"include_global_state": false
}</code></pre>
<pre><code>POST /_snapshot/my_backup/snapshot_a/_restore
{
"indices": "index_1,index_2",
"ignore_unavailable": "true",
"include_global_state": false,
"rename_pattern": "index_(.+)",