-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_to_es.py
174 lines (156 loc) · 4.64 KB
/
index_to_es.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
#!/usr/bin/python3
__author__ = 'vasilyen'
import sys
from elasticsearch import Elasticsearch
import elasticsearch
import csv
import os
from time import sleep
ES_HOST = 'http://127.0.0.1:9200'
INDEX = 'fec'
YEAR = sys.argv[1] if len(sys.argv) > 1 else '2016'
YR = YEAR[-2:]
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = SCRIPT_DIR + os.sep + 'data'
es = Elasticsearch(ES_HOST)
try:
es.indices.delete(index='fec')
except elasticsearch.exceptions.NotFoundError as e:
pass
es.indices.create(index='fec', ignore=[404, 400])
NUMBER_FIELDS = ['CAND_ELECTION_YR', 'TRANSACTION_AMT']
DATE_FIELDS = ['TRANSACTION_DT']
STRING_FIELDS = ['CMTE_TP',
'CAND_NAME',
'CAND_ST2',
'CMTE_ID',
'CAND_OFFICE_DISTRICT',
'CAND_PCC',
'CMTE_PTY_AFFILIATION',
'CAND_ZIP',
'ENTITY_TP',
'CONNECTED_ORG_NM',
'CAND_ICI',
'IMAGE_NUM',
'ORG_TP',
'CMTE_DSGN',
'NAME',
'CMTE_ST1',
'CMTE_FILING_FREQ',
'RPT_TP',
'SUB_ID',
'TRAN_ID',
'CMTE_ST2',
'CAND_CITY',
'CAND_ST',
'RPT_YR',
'AMNDT_IND',
'CATEGORY_DESC',
'CAND_STATUS',
'CMTE_ZIP',
'TRANSACTION_PGI',
'CAND_ID',
'BACK_REF_TRAN_ID',
'MEMO_CD',
'CAND_ST1',
'CMTE_CITY',
'CMTE_ST',
'MEMO_TEXT',
'CMTE_NM',
'FILE_NUM',
'CITY',
'PURPOSE',
'TRES_NM',
'CATEGORY',
'FORM_TP_CD',
'STATE',
'CAND_PTY_AFFILIATION',
'CAND_OFFICE',
'ZIP_CODE',
'LINE_NUM',
'CAND_OFFICE_ST',
'SCHED_TP_CD',
'EMPLOYER',
'OTHER_ID',
'TRANSACTION_TP',
'OCCUPATION',
]
def process_file(doc_type, fname):
print("Opening: " + fname)
with open(fname, 'r') as f:
reader = csv.DictReader(f)
first = True
for i, row in enumerate(reader):
if first:
make_mapping(doc_type, row.keys())
first = False
d = {k: v for k, v in row.items() if len(v) > 0}
try:
es.index(index='fec', doc_type=doc_type, body=d)
except Exception as e:
print("Couldn't index document")
print(d)
print(e)
sleep(20)
es.index(index='fec', doc_type=doc_type, body=d)
if i > 0 and i % 10000 == 0:
print("Indexed {} Items from {}".format(i, fname))
def make_mapping(doc_type, fields):
for field in fields:
print("Creating field " + field)
if field in STRING_FIELDS:
create_string_field(doc_type, field)
elif field in DATE_FIELDS:
create_date_field(doc_type, field)
elif field in NUMBER_FIELDS:
create_num_field(doc_type, field)
def create_string_field(doc_type, field):
es.indices.put_mapping(index=INDEX, doc_type=doc_type, body='''
{%(doc_type)s:{
"properties": {
%(field)s: {
"type": "string",
"fields": {
"raw": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}}
''' % {'doc_type': doc_type, 'field': field}
)
def create_date_field(doc_type, field):
es.indices.put_mapping(index=INDEX, doc_type=doc_type, body='''
{%(doc_type)s:{
"properties": {
%(field)s: {
"type": "date"
}
}
}}
''' % {'doc_type': doc_type, 'field': field}
)
def create_num_field(doc_type, field):
es.indices.put_mapping(index=INDEX, doc_type=doc_type, body='''
{%(doc_type)s:{
"properties": {
%(field)s: {
"type": "double"
}
}
}}
''' % {'doc_type': doc_type, 'field': field}
)
if __name__ == '__main__':
FILES = {
'indiv': '_indiv{}.csv'.format(YR),
# 'pas': '_pas2{}.csv'.format(YR),
# 'exp': '_exp{}.csv'.format(YR)
}
for file in FILES.keys():
if os.path.isfile(DATA_DIR + os.sep + FILES[file]):
process_file(file, DATA_DIR + os.sep + FILES[file])
else:
print("ERROR: Can't find file {}".format(DATA_DIR + os.sep + FILES[file]))