-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusr_search_v10.py
More file actions
205 lines (184 loc) · 8.15 KB
/
Copy pathusr_search_v10.py
File metadata and controls
205 lines (184 loc) · 8.15 KB
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
# Import the required modules
import requests
import json
import sys
from functools import partial
from itertools import takewhile
# https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters
# Dictionary of all reserved characters that will be replaced
# Unable to add \ character to the below list - TODO
reps = {
'-':'\-',
'+':'\+',
'=':'\=',
'&&':'\&&',
'||':'\||',
'>':'\>',
'<':'\<',
'!':'\!',
'(':'\(',
')':'\)',
'{':'\{',
'}':'\}',
'[':'\[',
']':'\]',
'^':'\^',
'"':'\"',
'~':'\~',
'*':'\*',
'?':'\?',
':':'\:',
'/':'\/'}
# define our method
def replace_all(text, dic):
for i, j in dic.items():
text = text.replace(i, j)
return text
def get_tokens(stream=sys.stdin):
token = []
for char in iter(partial(stream.read, 1), ''):
if char.isspace(): # use any space as a separator
if token:
yield ''.join(token)
del token[:]
else:
token.append(char)
if token:
yield ''.join(token)
print("Enter the user names to search, followed by SPACE $ sign")
UserInputQ = ' '.join(takewhile(lambda s: s != '$', get_tokens()))
#replace reserved characters
UserInputQ = replace_all(UserInputQ, reps)
Qstring = "("+UserInputQ+")||(\\-"+UserInputQ+")||("+UserInputQ+"\\-)||(\\/"+UserInputQ+")||("+UserInputQ+"\\/)||(\\'"+UserInputQ+")||("+UserInputQ+"\\')||(\\,"+UserInputQ+",)||("+UserInputQ+"\\,)||("+UserInputQ+".\\[0\\-9\\])||(\\[0\\-9\\]."+UserInputQ+")"
#print(UserSearchString)
query = json.dumps({
"size": 100000,
"_source": [ "customer.ezPayId","customer.accountNumber","customer.serviceContact.name","customer.billingContact.name","customer.serviceContact.address.*"],
"query": {
"bool": {
"must_not": [
{ "term": {
"customer.lob": {
"value": "residential"
}
}}
],
"should": [
{ "query_string": {"query": UserInputQ} },
{ "regexp":{ "customer.serviceContact.name": UserInputQ+".<0-9>"} },
{ "regexp":{ "customer.billingContact.name": UserInputQ+".<0-9>"} },
{ "regexp":{ "customer.serviceContact.name": "<0-9>."+UserInputQ } },
{ "regexp":{ "customer.billingContact.name": "<0-9>."+UserInputQ } }
]
}
},
"filter":{
"or": [
{ "term": { "customer.serviceContact.name": [UserInputQ] } },
{ "term": { "customer.billingContact.name": [UserInputQ] } },
{ "regexp":{ "customer.serviceContact.name": UserInputQ+".<0-9>"} },
{ "regexp":{ "customer.billingContact.name": UserInputQ+".<0-9>"} },
{ "regexp":{ "customer.serviceContact.name": "<0-9>."+UserInputQ } },
{ "regexp":{ "customer.billingContact.name": "<0-9>."+UserInputQ } },
{ "regexp":{ "customer.serviceContact.name": "[0-9-',(/)a-zA-Z].+"+UserInputQ } },
{ "regexp":{ "customer.billingContact.name": "[0-9-',(/)a-zA-Z].+"+UserInputQ } },
{ "regexp":{ "customer.serviceContact.name": UserInputQ+".+[0-9-',(/)a-zA-Z]" } },
{ "regexp":{ "customer.billingContact.name": UserInputQ+".+[0-9-',(/)a-zA-Z]" } }
]
}
})
print(query)
# Send HTTP request
elastic_ActiveCust_Response = requests.get('http://ES_IPADDRESS:9202/activecustomers/custdetails/_search', data = query )
# Check response status code
#print(elastic_ActiveCust_Response.raise_for_status())
if elastic_ActiveCust_Response.status_code != 200:
print("No Response from Elastic Search engine for active customers")
sys.exit()
# Send HTTP request
elastic_InActiveCust_Response = requests.get('http://ES_IPADDRESS:9202/inactivecustomers/custdetails/_search', data = query )
# Check response status code
#print(elastic_InActiveCust_Response.raise_for_status())
if elastic_InActiveCust_Response.status_code != 200:
print("No Response from Elastic Search engine for active customers")
sys.exit()
# Get active users json response
active_json_response = elastic_ActiveCust_Response.json();
# Get inactive users json response
inactive_json_response = elastic_InActiveCust_Response.json();
print("Dumping data to NewIP_User_SearchResult_V9.csv");
# Open file to write search result data.
output_file = open('NewIP_User_SearchResult_V9.csv','w')
# Set header information of the CSV file
header_line = "Customer Unique Id,Account Number,Customer Status,Service Name, Billing Name,\
Service State,Service City,Service Street,Service Zip"
output_file.write(header_line + '\n')
Acustomers = active_json_response['hits']['hits']
#json.dump(Acustomers, open('activeusrs.json','w'))
for customer in Acustomers:
output_line = '="'+customer['_source']['customer']['ezPayId']+'",'+ \
'"'+customer['_source']['customer']['accountNumber']+'",'+ \
'"Active"'
if customer['_source']['customer']['serviceContact'].get('name'):
output_line = output_line +',"'+customer['_source']['customer']['serviceContact']['name']+'"'
else:
output_line = output_line +'," "'
if customer['_source']['customer'].get('billingContact'):
if customer['_source']['customer']['billingContact'].get('name'):
output_line = output_line +',"'+customer['_source']['customer']['billingContact']['name']+'"'
else:
output_line = output_line +'," "'
else:
output_line = output_line +'," "'
if customer['_source']['customer']['serviceContact']['address'].get('state'):
output_line = output_line +',"'+customer['_source']['customer']['serviceContact']['address']['state']+'"'
else:
output_line = output_line +'," "'
if customer['_source']['customer']['serviceContact']['address'].get('city'):
output_line = output_line +',"'+customer['_source']['customer']['serviceContact']['address']['city']+'"'
else:
output_line = output_line +'," "'
if customer['_source']['customer']['serviceContact']['address'].get('street'):
output_line = output_line +',"'+customer['_source']['customer']['serviceContact']['address']['street']+'"'
else:
output_line = output_line +'," "'
if customer['_source']['customer']['serviceContact']['address'].get('postalCode'):
output_line = output_line +',"'+customer['_source']['customer']['serviceContact']['address']['postalCode']+'"'
else:
output_line = output_line +'," "'
output_file.write(output_line + '\n')
IAcustomers = inactive_json_response['hits']['hits']
#json.dump(bcustomers, open('inactiveusrs.json','w'))
for bcustomer in IAcustomers:
boutput_line = '="'+bcustomer['_source']['customer']['ezPayId']+'",'+ \
'"'+bcustomer['_source']['customer']['accountNumber']+'",'+ \
'"InActive"'
if bcustomer['_source']['customer']['serviceContact'].get('name'):
boutput_line = boutput_line +',"'+bcustomer['_source']['customer']['serviceContact']['name']+'"'
else:
boutput_line = boutput_line +'," "'
if bcustomer['_source']['customer'].get('billingContact'):
if bcustomer['_source']['customer']['billingContact'].get('name'):
boutput_line = boutput_line +',"'+bcustomer['_source']['customer']['billingContact']['name']+'"'
else:
boutput_line = boutput_line +'," "'
else:
boutput_line = boutput_line +'," "'
if bcustomer['_source']['customer']['serviceContact']['address'].get('state'):
boutput_line = boutput_line +',"'+bcustomer['_source']['customer']['serviceContact']['address']['state']+'"'
else:
boutput_line = boutput_line +'," "'
if bcustomer['_source']['customer']['serviceContact']['address'].get('city'):
boutput_line = boutput_line +',"'+bcustomer['_source']['customer']['serviceContact']['address']['city']+'"'
else:
boutput_line = boutput_line +'," "'
if bcustomer['_source']['customer']['serviceContact']['address'].get('street'):
boutput_line = boutput_line +',"'+bcustomer['_source']['customer']['serviceContact']['address']['street']+'"'
else:
boutput_line = boutput_line +'," "'
if bcustomer['_source']['customer']['serviceContact']['address'].get('postalCode'):
boutput_line = boutput_line +',"'+bcustomer['_source']['customer']['serviceContact']['address']['postalCode']+'"'
else:
boutput_line = boutput_line +'," "'
output_file.write(boutput_line + '\n')
output_file.close()