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
|
import itertools
import string
import json
from datetime import datetime
from elasticsearch import Elasticsearch
import elasticsearch
import elasticsearch.helpers
from elasticsearch.client import IndicesClient, CatClient
import sys, os, getopt
from file_utils import readFileToJson
from config_properties import getGlobalVar
def createIndex(client, indexName, createBody):
try:
print "start createIndex"
if (client == None):
client = Elasticsearch(['localhost'])
esIndexClient = IndicesClient(client)
res = deleteIndex(client, indexName)
if (res != 0):
print "operation failed"
return 2
create_res=elasticsearch.client.IndicesClient.create(esIndexClient, index=indexName, body=createBody)
print "create index response: ", create_res
if (create_res['acknowledged'] != True):
print "failed to create index"
return 1
else:
print "index ",indexName, " created successfully"
return 0
except Exception, error:
print "An exception was thrown!"
print str(error)
return 2
def deleteIndex(client, indexName):
try:
print "start deleteIndex"
if (client == None):
client = Elasticsearch(['localhost'])
esIndexClient = IndicesClient(client)
isExists=elasticsearch.client.IndicesClient.exists(esIndexClient, indexName)
if ( isExists == True ):
delete_res=elasticsearch.client.IndicesClient.delete(esIndexClient, index=indexName)
if (delete_res['acknowledged'] != True):
print "failed to delete index"
return 1
else:
print "index ",indexName, " deleted"
return 0
else:
print "index not found - assume already deleted"
return 0
except Exception, error:
print "An exception was thrown!"
print str(error)
return 2
def copyIndex(client, fromIndex, toIndex):
try:
print "start copyIndex"
if (client == None):
client = Elasticsearch(['localhost'])
client.indices.refresh(index=fromIndex)
count=client.search(fromIndex, search_type='count')
print "original index count: ",count
docNum, docErrors = elasticsearch.helpers.reindex(client, fromIndex, toIndex)
print "copy result: ", docNum, docErrors
if (docNum != count['hits']['total']):
print "Failed to copy all documents. expected: ", count['hits']['total'], " actual: ", docNum
return 1
# if (len(docErrors) != 0):
# print "copy returned with errors"
# print docErrors
# return 1
return 0
except Exception, error:
print "An exception was thrown!"
print str(error)
return 2
def usage():
print 'USAGE: ', sys.argv[0], '-o <operation : create | delete | move> -n <indexName> -a <address> -f <mappingFile (for create)> -t <toIndex (for move operation)>'
def main(argv):
print "start script with ", len(sys.argv), 'arguments.'
print "=============================================="
try:
opts, args = getopt.getopt(argv, "h:o:a:n:f:t:", ["operation","address","indexName","file","toIndex"])
except getopt.GetoptError:
usage()
sys.exit(2)
host = None
for opt, arg in opts:
print opt, arg
if opt == '-h':
usage()
sys.exit(2)
elif opt in ('-f', '--file'):
mapping=readFileToJson(arg)
elif opt in ('-a', '--address'):
host=arg
elif opt in ('-o', '--operation'):
operation=arg
elif opt in ('-n', '--indexName'):
indexName=arg
elif opt in ('-t', '--toIndex'):
destIndexName=arg
if (operation == None):
usage()
sys.exit(2)
elif (host == None):
print "address is mandatory argument"
usage()
sys.exit(2)
elif operation == 'create':
print "create new index ", indexName
client = Elasticsearch([{'host': host, 'timeout':5}] )
res = createIndex(client, indexName, mapping)
elif operation == 'delete':
print "delete index ", indexName
client = Elasticsearch([{'host': host, 'timeout':5}] )
res = deleteIndex(client, indexName)
elif operation == 'move':
print "move index ", indexName, " to ", destIndexName
client = Elasticsearch([{'host': host, 'timeout':5}] )
res = copyIndex(client, indexName, destIndexName)
else:
usage()
exit(2)
if res != 0:
print "ERROR: operation Failed"
exit(1)
if __name__ == "__main__":
main(sys.argv[1:])
|