0
# Index Management
1
2
Comprehensive index lifecycle operations including creation, deletion, mapping updates, settings configuration, and maintenance operations. These operations manage the structure and configuration of Elasticsearch indices.
3
4
## Capabilities
5
6
### Index Creation
7
8
Create new indices with mappings, settings, and aliases.
9
10
```python { .api }
11
def create(
12
self,
13
index: str,
14
mappings: Optional[Dict[str, Any]] = None,
15
settings: Optional[Dict[str, Any]] = None,
16
aliases: Optional[Dict[str, Any]] = None,
17
timeout: Optional[str] = None,
18
master_timeout: Optional[str] = None,
19
wait_for_active_shards: Optional[str] = None,
20
**kwargs
21
) -> ObjectApiResponse:
22
"""
23
Create an index.
24
25
Parameters:
26
- index: Index name to create
27
- mappings: Field mappings for the index
28
- settings: Index settings configuration
29
- aliases: Index aliases to create
30
- timeout: Request timeout
31
- master_timeout: Timeout for master node response
32
- wait_for_active_shards: Wait for active shard count
33
34
Returns:
35
ObjectApiResponse with creation result
36
"""
37
```
38
39
### Index Deletion
40
41
Delete indices with support for multiple indices and wildcard patterns.
42
43
```python { .api }
44
def delete(
45
self,
46
index: Union[str, List[str]],
47
timeout: Optional[str] = None,
48
master_timeout: Optional[str] = None,
49
ignore_unavailable: Optional[bool] = None,
50
allow_no_indices: Optional[bool] = None,
51
expand_wildcards: Optional[str] = None,
52
**kwargs
53
) -> ObjectApiResponse:
54
"""
55
Delete one or more indices.
56
57
Parameters:
58
- index: Index name(s) to delete
59
- timeout: Request timeout
60
- master_timeout: Timeout for master node response
61
- ignore_unavailable: Whether to ignore unavailable indices
62
- allow_no_indices: Whether empty index list is acceptable
63
- expand_wildcards: How to expand wildcard patterns
64
65
Returns:
66
ObjectApiResponse with deletion result
67
"""
68
```
69
70
### Index Information
71
72
Retrieve comprehensive information about indices including mappings, settings, and aliases.
73
74
```python { .api }
75
def get(
76
self,
77
index: Union[str, List[str]],
78
allow_no_indices: Optional[bool] = None,
79
expand_wildcards: Optional[str] = None,
80
flat_settings: Optional[bool] = None,
81
ignore_unavailable: Optional[bool] = None,
82
include_defaults: Optional[bool] = None,
83
local: Optional[bool] = None,
84
master_timeout: Optional[str] = None,
85
**kwargs
86
) -> ObjectApiResponse:
87
"""
88
Get index information.
89
90
Parameters:
91
- index: Index name(s) to get information for
92
- allow_no_indices: Whether empty index list is acceptable
93
- expand_wildcards: How to expand wildcard patterns
94
- flat_settings: Whether to return settings in flat format
95
- ignore_unavailable: Whether to ignore unavailable indices
96
- include_defaults: Whether to include default settings
97
- local: Whether to return local information only
98
- master_timeout: Timeout for master node response
99
100
Returns:
101
ObjectApiResponse with index information
102
"""
103
```
104
105
### Mapping Management
106
107
Update and retrieve field mappings for indices.
108
109
```python { .api }
110
def put_mapping(
111
self,
112
index: Union[str, List[str]],
113
properties: Optional[Dict[str, Any]] = None,
114
_meta: Optional[Dict[str, Any]] = None,
115
_source: Optional[Dict[str, Any]] = None,
116
dynamic: Optional[Union[bool, str]] = None,
117
dynamic_templates: Optional[List[Dict[str, Any]]] = None,
118
runtime: Optional[Dict[str, Any]] = None,
119
timeout: Optional[str] = None,
120
master_timeout: Optional[str] = None,
121
ignore_unavailable: Optional[bool] = None,
122
allow_no_indices: Optional[bool] = None,
123
expand_wildcards: Optional[str] = None,
124
write_index_only: Optional[bool] = None,
125
**kwargs
126
) -> ObjectApiResponse:
127
"""
128
Update index mappings.
129
130
Parameters:
131
- index: Index name(s) to update mappings for
132
- properties: Field mapping definitions
133
- _meta: Metadata for the mapping
134
- _source: Source field configuration
135
- dynamic: Dynamic mapping behavior
136
- dynamic_templates: Dynamic mapping templates
137
- runtime: Runtime field definitions
138
- timeout: Request timeout
139
- master_timeout: Timeout for master node response
140
- ignore_unavailable: Whether to ignore unavailable indices
141
- allow_no_indices: Whether empty index list is acceptable
142
- expand_wildcards: How to expand wildcard patterns
143
- write_index_only: Whether to apply only to write index
144
145
Returns:
146
ObjectApiResponse with mapping update result
147
"""
148
149
def get_mapping(
150
self,
151
index: Optional[Union[str, List[str]]] = None,
152
ignore_unavailable: Optional[bool] = None,
153
allow_no_indices: Optional[bool] = None,
154
expand_wildcards: Optional[str] = None,
155
master_timeout: Optional[str] = None,
156
local: Optional[bool] = None,
157
**kwargs
158
) -> ObjectApiResponse:
159
"""
160
Get index mappings.
161
162
Parameters:
163
- index: Index name(s) to get mappings for
164
- ignore_unavailable: Whether to ignore unavailable indices
165
- allow_no_indices: Whether empty index list is acceptable
166
- expand_wildcards: How to expand wildcard patterns
167
- master_timeout: Timeout for master node response
168
- local: Whether to return local information only
169
170
Returns:
171
ObjectApiResponse with index mappings
172
"""
173
```
174
175
### Settings Management
176
177
Configure and retrieve index settings.
178
179
```python { .api }
180
def put_settings(
181
self,
182
index: Union[str, List[str]],
183
settings: Dict[str, Any],
184
timeout: Optional[str] = None,
185
master_timeout: Optional[str] = None,
186
preserve_existing: Optional[bool] = None,
187
ignore_unavailable: Optional[bool] = None,
188
allow_no_indices: Optional[bool] = None,
189
expand_wildcards: Optional[str] = None,
190
flat_settings: Optional[bool] = None,
191
**kwargs
192
) -> ObjectApiResponse:
193
"""
194
Update index settings.
195
196
Parameters:
197
- index: Index name(s) to update settings for
198
- settings: Settings to update
199
- timeout: Request timeout
200
- master_timeout: Timeout for master node response
201
- preserve_existing: Whether to preserve existing settings
202
- ignore_unavailable: Whether to ignore unavailable indices
203
- allow_no_indices: Whether empty index list is acceptable
204
- expand_wildcards: How to expand wildcard patterns
205
- flat_settings: Whether settings are in flat format
206
207
Returns:
208
ObjectApiResponse with settings update result
209
"""
210
211
def get_settings(
212
self,
213
index: Optional[Union[str, List[str]]] = None,
214
name: Optional[Union[str, List[str]]] = None,
215
ignore_unavailable: Optional[bool] = None,
216
allow_no_indices: Optional[bool] = None,
217
expand_wildcards: Optional[str] = None,
218
flat_settings: Optional[bool] = None,
219
local: Optional[bool] = None,
220
master_timeout: Optional[str] = None,
221
include_defaults: Optional[bool] = None,
222
**kwargs
223
) -> ObjectApiResponse:
224
"""
225
Get index settings.
226
227
Parameters:
228
- index: Index name(s) to get settings for
229
- name: Setting name(s) to retrieve
230
- ignore_unavailable: Whether to ignore unavailable indices
231
- allow_no_indices: Whether empty index list is acceptable
232
- expand_wildcards: How to expand wildcard patterns
233
- flat_settings: Whether to return settings in flat format
234
- local: Whether to return local information only
235
- master_timeout: Timeout for master node response
236
- include_defaults: Whether to include default settings
237
238
Returns:
239
ObjectApiResponse with index settings
240
"""
241
```
242
243
### Index Status Operations
244
245
Control index status and perform maintenance operations.
246
247
```python { .api }
248
def open(
249
self,
250
index: Union[str, List[str]],
251
timeout: Optional[str] = None,
252
master_timeout: Optional[str] = None,
253
ignore_unavailable: Optional[bool] = None,
254
allow_no_indices: Optional[bool] = None,
255
expand_wildcards: Optional[str] = None,
256
wait_for_active_shards: Optional[str] = None,
257
**kwargs
258
) -> ObjectApiResponse:
259
"""
260
Open closed indices.
261
262
Parameters:
263
- index: Index name(s) to open
264
- timeout: Request timeout
265
- master_timeout: Timeout for master node response
266
- ignore_unavailable: Whether to ignore unavailable indices
267
- allow_no_indices: Whether empty index list is acceptable
268
- expand_wildcards: How to expand wildcard patterns
269
- wait_for_active_shards: Wait for active shard count
270
271
Returns:
272
ObjectApiResponse with open result
273
"""
274
275
def close(
276
self,
277
index: Union[str, List[str]],
278
timeout: Optional[str] = None,
279
master_timeout: Optional[str] = None,
280
ignore_unavailable: Optional[bool] = None,
281
allow_no_indices: Optional[bool] = None,
282
expand_wildcards: Optional[str] = None,
283
wait_for_active_shards: Optional[str] = None,
284
**kwargs
285
) -> ObjectApiResponse:
286
"""
287
Close indices.
288
289
Parameters:
290
- index: Index name(s) to close
291
- timeout: Request timeout
292
- master_timeout: Timeout for master node response
293
- ignore_unavailable: Whether to ignore unavailable indices
294
- allow_no_indices: Whether empty index list is acceptable
295
- expand_wildcards: How to expand wildcard patterns
296
- wait_for_active_shards: Wait for active shard count
297
298
Returns:
299
ObjectApiResponse with close result
300
"""
301
```
302
303
### Index Maintenance
304
305
Perform maintenance operations on indices.
306
307
```python { .api }
308
def refresh(
309
self,
310
index: Optional[Union[str, List[str]]] = None,
311
ignore_unavailable: Optional[bool] = None,
312
allow_no_indices: Optional[bool] = None,
313
expand_wildcards: Optional[str] = None,
314
**kwargs
315
) -> ObjectApiResponse:
316
"""
317
Refresh indices to make recent changes searchable.
318
319
Parameters:
320
- index: Index name(s) to refresh
321
- ignore_unavailable: Whether to ignore unavailable indices
322
- allow_no_indices: Whether empty index list is acceptable
323
- expand_wildcards: How to expand wildcard patterns
324
325
Returns:
326
ObjectApiResponse with refresh result
327
"""
328
329
def flush(
330
self,
331
index: Optional[Union[str, List[str]]] = None,
332
force: Optional[bool] = None,
333
wait_if_ongoing: Optional[bool] = None,
334
ignore_unavailable: Optional[bool] = None,
335
allow_no_indices: Optional[bool] = None,
336
expand_wildcards: Optional[str] = None,
337
**kwargs
338
) -> ObjectApiResponse:
339
"""
340
Flush indices to ensure data is written to disk.
341
342
Parameters:
343
- index: Index name(s) to flush
344
- force: Whether to force flush even if not necessary
345
- wait_if_ongoing: Whether to wait if flush is ongoing
346
- ignore_unavailable: Whether to ignore unavailable indices
347
- allow_no_indices: Whether empty index list is acceptable
348
- expand_wildcards: How to expand wildcard patterns
349
350
Returns:
351
ObjectApiResponse with flush result
352
"""
353
354
def force_merge(
355
self,
356
index: Optional[Union[str, List[str]]] = None,
357
max_num_segments: Optional[int] = None,
358
only_expunge_deletes: Optional[bool] = None,
359
flush: Optional[bool] = None,
360
ignore_unavailable: Optional[bool] = None,
361
allow_no_indices: Optional[bool] = None,
362
expand_wildcards: Optional[str] = None,
363
wait_for_completion: Optional[bool] = None,
364
**kwargs
365
) -> ObjectApiResponse:
366
"""
367
Force merge index segments.
368
369
Parameters:
370
- index: Index name(s) to force merge
371
- max_num_segments: Maximum number of segments to merge to
372
- only_expunge_deletes: Whether to only expunge deleted documents
373
- flush: Whether to flush after merge
374
- ignore_unavailable: Whether to ignore unavailable indices
375
- allow_no_indices: Whether empty index list is acceptable
376
- expand_wildcards: How to expand wildcard patterns
377
- wait_for_completion: Whether to wait for completion
378
379
Returns:
380
ObjectApiResponse with force merge result
381
"""
382
```
383
384
### Index Statistics
385
386
Retrieve detailed statistics about indices.
387
388
```python { .api }
389
def stats(
390
self,
391
index: Optional[Union[str, List[str]]] = None,
392
metric: Optional[Union[str, List[str]]] = None,
393
completion_fields: Optional[Union[str, List[str]]] = None,
394
fielddata_fields: Optional[Union[str, List[str]]] = None,
395
fields: Optional[Union[str, List[str]]] = None,
396
groups: Optional[Union[str, List[str]]] = None,
397
level: Optional[str] = None,
398
types: Optional[Union[str, List[str]]] = None,
399
include_segment_file_sizes: Optional[bool] = None,
400
include_unloaded_segments: Optional[bool] = None,
401
expand_wildcards: Optional[str] = None,
402
**kwargs
403
) -> ObjectApiResponse:
404
"""
405
Get index statistics.
406
407
Parameters:
408
- index: Index name(s) to get statistics for
409
- metric: Specific metrics to return
410
- completion_fields: Completion fields to include
411
- fielddata_fields: Fielddata fields to include
412
- fields: Fields to include in statistics
413
- groups: Statistics groups to include
414
- level: Level of detail (cluster, indices, shards)
415
- types: Document types to include
416
- include_segment_file_sizes: Whether to include segment file sizes
417
- include_unloaded_segments: Whether to include unloaded segments
418
- expand_wildcards: How to expand wildcard patterns
419
420
Returns:
421
ObjectApiResponse with index statistics
422
"""
423
```
424
425
### Alias Management
426
427
Manage index aliases for flexible index organization.
428
429
```python { .api }
430
def put_alias(
431
self,
432
index: Union[str, List[str]],
433
name: str,
434
filter: Optional[Dict[str, Any]] = None,
435
routing: Optional[str] = None,
436
index_routing: Optional[str] = None,
437
search_routing: Optional[str] = None,
438
is_write_index: Optional[bool] = None,
439
timeout: Optional[str] = None,
440
master_timeout: Optional[str] = None,
441
**kwargs
442
) -> ObjectApiResponse:
443
"""
444
Create or update an index alias.
445
446
Parameters:
447
- index: Index name(s) for the alias
448
- name: Alias name
449
- filter: Filter query for the alias
450
- routing: Routing value for the alias
451
- index_routing: Routing for index operations
452
- search_routing: Routing for search operations
453
- is_write_index: Whether this is the write index for the alias
454
- timeout: Request timeout
455
- master_timeout: Timeout for master node response
456
457
Returns:
458
ObjectApiResponse with alias creation result
459
"""
460
461
def get_alias(
462
self,
463
index: Optional[Union[str, List[str]]] = None,
464
name: Optional[Union[str, List[str]]] = None,
465
ignore_unavailable: Optional[bool] = None,
466
allow_no_indices: Optional[bool] = None,
467
expand_wildcards: Optional[str] = None,
468
local: Optional[bool] = None,
469
**kwargs
470
) -> ObjectApiResponse:
471
"""
472
Get index aliases.
473
474
Parameters:
475
- index: Index name(s) to get aliases for
476
- name: Alias name(s) to retrieve
477
- ignore_unavailable: Whether to ignore unavailable indices
478
- allow_no_indices: Whether empty index list is acceptable
479
- expand_wildcards: How to expand wildcard patterns
480
- local: Whether to return local information only
481
482
Returns:
483
ObjectApiResponse with alias information
484
"""
485
486
def delete_alias(
487
self,
488
index: Union[str, List[str]],
489
name: Union[str, List[str]],
490
timeout: Optional[str] = None,
491
master_timeout: Optional[str] = None,
492
**kwargs
493
) -> ObjectApiResponse:
494
"""
495
Delete index aliases.
496
497
Parameters:
498
- index: Index name(s) to delete aliases from
499
- name: Alias name(s) to delete
500
- timeout: Request timeout
501
- master_timeout: Timeout for master node response
502
503
Returns:
504
ObjectApiResponse with deletion result
505
"""
506
```
507
508
## Usage Examples
509
510
### Complete Index Lifecycle
511
512
```python
513
from elasticsearch import Elasticsearch
514
515
client = Elasticsearch(hosts=['http://localhost:9200'])
516
517
# Create index with mappings and settings
518
client.indices.create(
519
index="products",
520
mappings={
521
"properties": {
522
"name": {"type": "text", "analyzer": "standard"},
523
"description": {"type": "text"},
524
"price": {"type": "double"},
525
"category": {"type": "keyword"},
526
"created_at": {"type": "date"},
527
"tags": {"type": "keyword"},
528
"location": {"type": "geo_point"}
529
}
530
},
531
settings={
532
"number_of_shards": 2,
533
"number_of_replicas": 1,
534
"analysis": {
535
"analyzer": {
536
"custom_analyzer": {
537
"type": "custom",
538
"tokenizer": "standard",
539
"filter": ["lowercase", "asciifolding"]
540
}
541
}
542
}
543
}
544
)
545
546
# Add an alias
547
client.indices.put_alias(
548
index="products",
549
name="current_products",
550
filter={"term": {"status": "active"}}
551
)
552
553
# Update mapping to add new field
554
client.indices.put_mapping(
555
index="products",
556
properties={
557
"rating": {"type": "float"},
558
"review_count": {"type": "integer"}
559
}
560
)
561
562
# Update settings
563
client.indices.put_settings(
564
index="products",
565
settings={
566
"index.refresh_interval": "30s",
567
"index.max_result_window": 20000
568
}
569
)
570
571
# Get index information
572
info = client.indices.get(index="products")
573
print(f"Index settings: {info.body['products']['settings']}")
574
print(f"Index mappings: {info.body['products']['mappings']}")
575
576
# Perform maintenance
577
client.indices.refresh(index="products")
578
client.indices.flush(index="products")
579
580
# Get statistics
581
stats = client.indices.stats(index="products")
582
print(f"Document count: {stats.body['indices']['products']['total']['docs']['count']}")
583
print(f"Store size: {stats.body['indices']['products']['total']['store']['size_in_bytes']}")
584
```
585
586
### Index Template Management
587
588
```python
589
# Create an index template
590
client.indices.put_index_template(
591
name="logs_template",
592
index_patterns=["logs-*"],
593
template={
594
"settings": {
595
"number_of_shards": 1,
596
"number_of_replicas": 0,
597
"index.lifecycle.name": "logs_policy"
598
},
599
"mappings": {
600
"properties": {
601
"@timestamp": {"type": "date"},
602
"level": {"type": "keyword"},
603
"message": {"type": "text"},
604
"host": {"type": "keyword"},
605
"service": {"type": "keyword"}
606
}
607
}
608
},
609
priority=100
610
)
611
612
# Indices created with matching patterns will use this template
613
client.index(
614
index="logs-2024-01-01",
615
document={
616
"@timestamp": "2024-01-01T10:00:00Z",
617
"level": "INFO",
618
"message": "Application started",
619
"host": "web-01",
620
"service": "api"
621
}
622
)
623
```