-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathch06_indexing_operations.txt
557 lines (457 loc) · 10.8 KB
/
ch06_indexing_operations.txt
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
##
## A handy scripts file for hands-on exercises on Kibana
## Copy the whole contents of this file to Kibana's DevTools
## If you wish to follow the running commentary for these scripts,
## follow the Ch6 Text Analysis wiki page here: https://github.com/madhusudhankonda/elasticsearch-in-action/wiki/Ch-6.-Indexing Operations
##
## Please let me know if you find any issues with this script
##
## Indexing Documents
### Indexing a movie document with an identifier:
# First document containing car data
PUT cars/_doc/1
{
"make":"Maserati",
"model":"GranTurismo Sport",
"speed_mph":186
}
# Disabling automatic creation of indices
# Don't execute it for now as it will hamper your progress
# executing the rest of the script
# PUT _cluster/settings
# {
# "persistent": {
# "action.auto_create_index":false
# }
# }
# Index with sample mapping
PUT cars_index_with_sample_mapping
{
"mappings": {
"properties": {
"make":{
"type": "text"
}
}
}
}
#Index with custom settings
PUT cars_index_with_sample_settings
{
"settings": {
"number_of_replicas": 3,
"codec": "best_compression"
}
}
# Creating an index with an alias
PUT cars_index_with_sample_alias
{
"aliases": {
"alias_for_cars_index_with_sample_alias": {}
}
}
# Creating an index with custom settings
PUT cars_with_custom_settings
{
"settings":{
"number_of_shards":3,
"number_of_replicas":5,
"codec": "best_compression",
"max_script_fields":128,
"refresh_interval": "60s"
}
}
# Updating dynamic property on an index
PUT cars_with_custom_settings/_settings
{
"settings": {
"number_of_replicas": 2
}
}
# Fetching the settings of an index
GET cars_with_custom_settings/_settings
# Pre-requisite to create three car indices for the next query to pass
PUT cars1
PUT cars2
PUT cars3
# Fetching the settings of multiple indices in one go
GET cars1,cars2,cars3/_settings
# Fetches the settings for the index identified with a wildcard (*)
GET cars*/_settings
# Fetching a single attribute
GET cars_with_custom_settings/_settings/index.number_of_shards
#Creating an index with field mappings for a car document
PUT cars_with_mappings
{
"mappings": {
"properties": {
"make":{
"type": "text"
},
"model":{
"type": "text"
},
"registration_year":{
"type": "date",
"format": "dd-MM-yyyy"
}
}
}
}
#Creating an index with both settings and mappings
PUT cars_with_settings_and_mappings
{
"settings": {
"number_of_replicas": 3
},
"mappings": {
"properties": {
"make":{
"type": "text"
}
}
}
}
# Creating an alias using an aliases object
PUT cars_for_aliases
{
"aliases": {
"my_new_cars_alias": {}
}
}
# Creating an alias using an _alias endpoint
PUT cars_for_aliases/_alias/my_cars_alias
# Creating an alias using an _alias endpoint
PUT cars_for_aliases/_alias/all_cars_alias
# Creating a single alias pointing to multiple indices
PUT cars1,cars2,cars3/_alias/multi_cars_alias
# Creating an alias with a wildcard
PUT cars*/_alias/wildcard_cars_alias
#Fetching aliases, settings, and mappings for an index
GET cars
# Getting an alias on a single index
GET my_cars_alias/_alias
# Fetching aliases associated with multiple indices
GET all_cars_alias,my_cars_alias/_alias
#Prerequisite in getting the next query executed successfully
PUT vintage_cars
PUT vintage_cars_new
# Performing multiple aliasing operations
POST _aliases
{
"actions": [
{
"remove": {
"index": "vintage_cars",
"alias": "vintage_cars_alias"
}
},
{
"add": {
"index": "vintage_cars_new",
"alias": "vintage_cars_alias"
}
}
]
}
# Prerequiste for the next query
PUT power_cars
PUT rare_cars
PUT luxury_cars
# Creating an alias pointing to multiple indice
POST _aliases
{
"actions": [
{
"add": {
"indices": ["vintage_cars","power_cars","rare_cars","luxury_cars"],
"alias": "high_end_cars_alais"
}
}
]
}
# Getting index configurations for multiple indices
GET cars1,cars2,cars3
# Getting multiple index configurations with a wildcard
GET ca*
# Fetching configurations for specific indices
GET mov*,stu*
# Getting individual configurations for an index
GET cars/_settings
GET cars/_mapping
GET cars/_alias
#Prerequisite for the next query
PUT cars/_alias/cars_alias
#Deleting an alias explicitly
DELETE cars/_alias/cars_alias
# Closing the cars index indefinitely
POST cars/_close?wait_for_active_shards=index-setting
# Closing multiple indices
POST cars1,*mov*,students*/_close?wait_for_active_shards=index-setting
#Halt all the live operations using _all or *:
# I am commenting this out - uncomment only when you know what you are doing.
# Refer to Book for more informtion
# POST */_close?wait_for_active_shards=index-setting
# Disabling the action to close all indices
# Caution: Make sure you understand the implication of this command. Refer to book notes
PUT _cluster/settings
{
"persistent": {
"action.destructive_requires_name":true
}
}
# Disabling the closing indices feature
# Caution: Make sure you understand the implication of this command. Refer to book notes
PUT _cluster/settings
{
"persistent": {
"cluster.indices.close.enable":false
}
}
# Putting the index back into operation
POST cars/_open
# Creating an index template
POST _index_template/cars_template
{
"index_patterns": ["*cars*"],
"priority": 1,
"template": {
"mappings": {
"properties":{
"created_at":{
"type":"date"
},
"created_by":{
"type":"text"
}
}
}
}
}
# the cars_template_mar21 overrides the cars_template_feb21 in the following code snippet (though the names suggest the other way!):
#POST _index_template/cars_template_mar21
#{
# "index_patterns": ["*cars*"],
# "priority": 20,
# "template": { ... }
#}
#POST _index_template/cars_template_feb21
#{
# "index_patterns": ["*cars*"],
# "priority": 30,
# "template": { ... }
#}
#A The lower priority index template
#B Same matching template but with a higher priority
# Developing a component template
POST _component_template/dev_settings_component_template
{
"template":{
"settings":{
"number_of_shards":3,
"number_of_replicas":3
}
}
}
# A component template with a mapping schema
POST _component_template/dev_mapping_component_template
{
"template": {
"mappings": {
"properties": {
"created_by": {
"type": "text"
},
"version": {
"type": "float"
}
}
}
}
}
# Creating an index template composed of a component template
POST _index_template/composed_cars_template
{
"index_patterns": ["*cars*"],
"priority": 200,
"composed_of": ["dev_settings_component_template",
"dev_mapping_component_template"]
}
# Fetching the statistics of an index
GET cars/_stats
# Fetching the statistics for multiple indices
GET cars*/_stats
# Fetching the statistics for all indices in the cluster
GET */_stats
#The following snippet shows how to return the statistics in segments:
GET cars/_stats/segments
# Prerequisite for the next query
PUT all_cars
# Making sure the index is read-only
PUT all_cars/_settings
{
"settings":{
"index.blocks.write":"true"
}
}
#Splitting the all_cars index
# Note the target index’s primary shard number must be a multiple of the number of primary shards of the source index. Refer to book for more details.
POST all_cars/_split/all_cars_new
{
"settings": {
"index.number_of_shards": 12
}
}
# This query will throw an exception. That’s because we violated the second rule (see book notes):
# "reason" : "the number of source shards [3] must be a factor of [14]"
POST all_cars/_split/all_cars_new
{
"settings": {
"index.number_of_shards": 14
}
}
#Carrying out the prerequisites before shrinking the index
PUT all_cars/_settings
{
"settings": {
"index.blocks.write": true,
"index.routing.allocation.require._name": "node1"
}
}
# Shrinking the index
PUT all_cars/_shrink/all_cars_new2
{
"settings":{
"index.blocks.write":null,
"index.routing.allocation.require._name":null,
"index.number_of_shards":1,
"index.number_of_replicas":5
}
}
#Prerequisite for the next query
PUT cars_2021-000001
# Creating an alias for an existing index
POST _aliases
{
"actions": [
{
"add": {
"index": "cars_2021-000001",
"alias": "latest_cars_a",
"is_write_index": true
}
}
]
}
# Rolling over the index
POST latest_cars_a/_rollover
# The response from the above rollover call:
#{
# "acknowledged" : true,
# "shards_acknowledged" : true,
# "old_index" : "latest_cars-000001", #A Old index name
# "new_index" : "latest_cars-000002", #B New index name
# "rolled_over" : true,
# "dry_run" : false,
# "conditions" : { }
#}
# Creating a policy with hot and delete policies
PUT _ilm/policy/hot_delete_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "1d",
"actions": {
"set_priority": {
"priority": 250
}
}
},
"delete": {
"actions": {
"delete" : { }
}
}
}
}
}
# Creating an index with an index life cycle
PUT hot_delete_policy_index
{
"settings": {
"index.lifecycle.name":"hot_delete_policy"
}
}
#Simple policy definition for hot phase
PUT _ilm/policy/hot_simple_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {
"rollover": {
"max_age": "1d",
"max_docs": 10000,
"max_size": "10gb"
}
}
}
}
}
}
# Attaching a life-cycle policy to a template
PUT _index_template/mysql_logs_template
{
"index_patterns": ["mysql-*"],
"template":{
"settings":{
"index.lifecycle.name":"hot_simple_policy",
"index.lifecycle.rollover_alias":"mysql-logs-alias"
}
}
}
# Setting the index as writable for the alias
PUT mysql-index-000001
{
"aliases": {
"mysql-logs-alias": {
"is_write_index":true
}
}
}
# Creating an advanced life-cycle policy
PUT _ilm/policy/hot_warm_delete_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "1d",
"actions": {
"rollover": {
"max_size": "40gb",
"max_age": "6d"
},
"set_priority": {
"priority": 50
}
}
},
"warm": {
"min_age": "7d",
"actions": {
"shrink": {
"number_of_shards": 1
}
}
},
"delete": {
"min_age": "30d",
"actions": {
"delete": {}
}
}
}
}
}