-
Notifications
You must be signed in to change notification settings - Fork 184
/
advanced_index_actions_sample.py
108 lines (93 loc) · 3.31 KB
/
advanced_index_actions_sample.py
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
#!/usr/bin/env python
# SPDX-License-Identifier: Apache-2.0
#
# The OpenSearch Contributors require contributions made to
# this file be licensed under the Apache-2.0 license or a
# compatible open source license.
#
# Modifications Copyright OpenSearch Contributors. See
# GitHub history for details.
import os
import time
from opensearchpy import OpenSearch
# For cleaner output, comment in the two lines below to disable warnings and informational messages
# import urllib3
# urllib3.disable_warnings()
def main() -> None:
"""
demonstrates various functions to operate on the index
(e.g. clear different levels of cache, refreshing the index)
"""
# Set up
client = OpenSearch(
hosts=["https://localhost:9200"],
use_ssl=True,
verify_certs=False,
http_auth=("admin", os.getenv("OPENSEARCH_PASSWORD", "admin")),
)
client.indices.create(index="movies")
print("'movies' index created!")
# Test Clear Index Cache
client.indices.clear_cache(index="movies")
print("Cache for 'movies' index cleared!")
client.indices.clear_cache(index="movies", query=True)
print("Query cache for 'movies' index cleared!")
client.indices.clear_cache(index="movies", fielddata=True, request=True)
print("Field data and request cache for 'movies' index cleared!")
# Test Flush Index
client.indices.flush(index="movies")
print("'movies' index flushed!")
# Test Refresh Index
client.indices.refresh(index="movies")
print("'movies' index refreshed!")
# Test Close or Open Index
client.indices.close(index="movies")
print("'movies' index closed!")
time.sleep(2) # add sleep to ensure the index has time to close
client.indices.open(index="movies")
print("'movies' index opened!")
# Test Force Merge Index
client.indices.forcemerge(index="movies")
print("'movies' index force merged!")
# Test Clone
client.indices.put_settings(
index="movies", body={"index": {"blocks": {"write": True}}}
)
print("Write operations blocked for 'movies' index!")
time.sleep(2)
client.indices.clone(index="movies", target="movies_clone")
print("'movies' index cloned to 'movies_clone'!")
client.indices.put_settings(
index="movies", body={"index": {"blocks": {"write": False}}}
)
print("Write operations enabled for 'movies' index!")
# Test Split
client.indices.create(
index="books",
body={
"settings": {
"index": {
"number_of_shards": 5,
"number_of_routing_shards": 30,
"blocks": {"write": True},
}
}
},
)
print("'books' index created!")
time.sleep(2) # add sleep to ensure the index has time to become read-only
client.indices.split(
index="books",
target="bigger_books",
body={"settings": {"index": {"number_of_shards": 10}}},
)
print("'books' index split into 'bigger_books'!")
client.indices.put_settings(
index="books", body={"index": {"blocks": {"write": False}}}
)
print("Write operations enabled for 'books' index!")
# Cleanup
client.indices.delete(index=["movies", "books", "movies_clone", "bigger_books"])
print("All indices deleted!")
if __name__ == "__main__":
main()