-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathsolr_admin.py
254 lines (208 loc) · 7.79 KB
/
solr_admin.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
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
'''
GeoParser admin core Utility
- Store information about index configuration
- id - domain-name
- indexes - list of indexes for this domain.
- core_names - list of cores for those indexes.
- point_list - list of num of points points found
- idx_size_list - list of size of target index
- idx_field_list - csv list of fields for each document
- count - Current count of total cores created for this domain
- username - list of username for target indexes
- password - list of passwords for target indexes
'''
import yaml
import requests
from solr import create_core, headers, SOLR_URL, params
from keyczar_util import Crypter
ADMIN_CORE = 'admin'
'''
LIST OF FIELD START
Add all new fields in delete_index_core() which needs to be updated
'''
ADMIN_F_IDX_LIST = "indexes"
ADMIN_F_CORE_LIST = "core_names"
ADMIN_F_PNT_LEN_LIST = "point_len_list"
ADMIN_F_IDX_SIZE_LIST = "idx_size_list"
ADMIN_F_IDX_FIELD_LIST = "idx_field_list"
ADMIN_F_COUNT = "count"
ADMIN_F_USER_LIST = "username"
ADMIN_F_PASSWD_LIST = "password"
'''
LIST OF FIELD ENDS
'''
DEFAULT_IDX_FIELD = 'id,title'
'''
keyczar.Crypter object
'''
crypter = Crypter()
def _get_domain_admin(domain):
url = '{0}{1}/select?q=id:{2}&wt=json'.format(SOLR_URL, ADMIN_CORE, domain)
response = requests.get(url, headers=headers)
return yaml.safe_load(response.text)
def get_index_core(domain, index_path, user="user",passwd="pass"):
# TODO strip trailing /
if create_core(ADMIN_CORE):
response = _get_domain_admin(domain)
num_found = response['response']['numFound']
if(num_found == 0): # # No record found for this domain.
count = 1 # # Initialize a new one
else:
# Check if this index exist for this domain
all_idx = response['response']['docs'][0][ADMIN_F_IDX_LIST]
count = response['response']['docs'][0][ADMIN_F_COUNT][0]
if(index_path in all_idx):
index_arr = all_idx.index(index_path)
core_name = response['response']['docs'][0][ADMIN_F_CORE_LIST][index_arr]
# todo encrypt it
stored_user = response['response']['docs'][0][ADMIN_F_USER_LIST][index_arr]
stored_passwd = response['response']['docs'][0][ADMIN_F_PASSWD_LIST][index_arr]
# return existing core name with user name and passwprd
return core_name,stored_user,crypter.decrypt(stored_passwd)
# if not create a new count for this index
print "No existing core found for ", domain, index_path
count = count + 1
# get unique core name
core_name = "{0}_{1}".format(domain, count)
payload = {
"add":{
"doc":{
"id" : "{0}".format(domain) ,
ADMIN_F_IDX_LIST : {"add":"{0}".format(index_path)},
ADMIN_F_CORE_LIST : {"add":core_name},
ADMIN_F_PNT_LEN_LIST : {"add":0 },
ADMIN_F_IDX_SIZE_LIST : {"add":0 },
ADMIN_F_IDX_FIELD_LIST : {"add":DEFAULT_IDX_FIELD },
ADMIN_F_USER_LIST: {"add":"{0}".format(user) },
ADMIN_F_PASSWD_LIST: {"add":"{0}".format(crypter.encrypt(passwd) ) },
ADMIN_F_COUNT : {"set":count }
}
}
}
r = requests.post("{0}{1}/update".format(SOLR_URL, ADMIN_CORE), data=str(payload), params=params, headers=headers)
print r.text
if(not r.ok):
raise "Can't create core with core name {0}".format(core_name)
return
# return newly created core
return core_name,user,passwd
def get_all_domain_details():
resp = {}
if create_core(ADMIN_CORE):
url = '{0}{1}/select?q=*&wt=json'.format(SOLR_URL, ADMIN_CORE)
response = requests.get(url, headers=headers)
response = yaml.safe_load(response.text)['response']['docs']
for doc in response:
resp[doc["id"]] = doc[ADMIN_F_IDX_LIST]
return resp
def get_idx_details(domain, index_path):
'''
Return size of original index and number of points found till now
'''
if create_core(ADMIN_CORE):
response = _get_domain_admin(domain)['response']['docs'][0]
all_idx = response[ADMIN_F_IDX_LIST]
if(index_path in all_idx):
index_arr = all_idx.index(index_path)
return response[ADMIN_F_IDX_SIZE_LIST][index_arr], response[ADMIN_F_PNT_LEN_LIST][index_arr]
return 0, 0
def update_idx_details(domain, index_path, idx_size, pnt_size):
'''
Updates size of original index and number of points found till now
'''
if create_core(ADMIN_CORE):
response = _get_domain_admin(domain)['response']['docs'][0]
all_idx = response[ADMIN_F_IDX_LIST]
if(index_path in all_idx):
index_arr = all_idx.index(index_path)
response[ADMIN_F_PNT_LEN_LIST][index_arr] = pnt_size
response[ADMIN_F_IDX_SIZE_LIST][index_arr] = idx_size
payload = {
"add":{
"doc":{
"id" : "{0}".format(domain) ,
ADMIN_F_PNT_LEN_LIST : {"set":response[ADMIN_F_PNT_LEN_LIST] },
ADMIN_F_IDX_SIZE_LIST : {"set":response[ADMIN_F_IDX_SIZE_LIST] }
}
}
}
r = requests.post("{0}{1}/update".format(SOLR_URL, ADMIN_CORE), data=str(payload), params=params, headers=headers)
print r.text
if(not r.ok):
print payload
raise "Can't update idx details with core name {0} - {1}".format(domain, index_path)
return True
return False
def update_idx_field_csv(domain, index_path, idx_field_csv):
'''
Updates field_csv from original index to be shown on popups
'''
if create_core(ADMIN_CORE):
response = _get_domain_admin(domain)['response']['docs']
if len(response) == 1:
response = response[0]
all_idx = response[ADMIN_F_IDX_LIST]
if(index_path in all_idx):
index_arr = all_idx.index(index_path)
response[ADMIN_F_IDX_FIELD_LIST][index_arr] = "{0}".format(idx_field_csv)
payload = {
"add":{
"doc":{
"id" : "{0}".format(domain) ,
ADMIN_F_IDX_FIELD_LIST : {"set":response[ADMIN_F_IDX_FIELD_LIST] }
}
}
}
r = requests.post("{0}{1}/update".format(SOLR_URL, ADMIN_CORE), data=str(payload), params=params, headers=headers)
print r.text
if(not r.ok):
print payload
raise "Can't update idx details with core name {0} - {1}".format(domain, index_path)
return True
#
return False
def get_idx_field_csv(domain, index_path):
'''
Returns field_csv from original index to be shown on popups
'''
if create_core(ADMIN_CORE):
response = _get_domain_admin(domain)['response']['docs'][0]
all_idx = response[ADMIN_F_IDX_LIST]
if(index_path in all_idx):
index_arr = all_idx.index(index_path)
return response[ADMIN_F_IDX_FIELD_LIST][index_arr]
return 0, 0
def delete_index_core(domain, index_path):
# TODO strip trailing /
if create_core(ADMIN_CORE):
response = _get_domain_admin(domain)
num_found = response['response']['numFound']
if(num_found == 0): # # No record found for this domain.
return "No domain added with name - " + domain
else:
# Check if this index exist for this domain
all_idx = response['response']['docs'][0][ADMIN_F_IDX_LIST]
if(index_path not in all_idx):
return "No index added with name {0} for domain {1} ".format(index_path,domain)
index_in_arr = all_idx.index(index_path)
new_doc = response['response']['docs'][0]
print "Data now -", new_doc
del(new_doc[ADMIN_F_IDX_LIST][index_in_arr])
del(new_doc[ADMIN_F_CORE_LIST][index_in_arr])
del(new_doc[ADMIN_F_PNT_LEN_LIST][index_in_arr])
del(new_doc[ADMIN_F_IDX_SIZE_LIST][index_in_arr])
del(new_doc[ADMIN_F_IDX_FIELD_LIST][index_in_arr])
del(new_doc[ADMIN_F_USER_LIST][index_in_arr])
del(new_doc[ADMIN_F_PASSWD_LIST][index_in_arr])
print "Updated data - ", new_doc
payload = {
"add":{
"doc":new_doc
}
}
r = requests.post("{0}{1}/update".format(SOLR_URL, ADMIN_CORE), data=str(payload), params=params, headers=headers)
print r.text
if(not r.ok):
print "Can't delete index with name {0} for domain {1} ".format(index_path,domain)
else:
print "Deleted index with name {0} for domain {1} ".format(index_path,domain)