-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.py
33 lines (31 loc) · 1.09 KB
/
indexer.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
import json
import uuid
from datetime import datetime
from elasticsearch import Elasticsearch, helpers
def jsonToElasticsearch(filename, url, username, password, indexname):
"""
Iterate over JSON file of player documents. Bulk send to ES index.
"""
es = Elasticsearch(url, http_auth=(username, password))
if not es.indices.exists(index=indexname):
print(f"Creating {indexname} index")
es.indices.create(indexname)
playerDocuments = []
with open(filename) as jsonFile:
playerData = json.load(jsonFile)
for player in playerData:
document = {
"_id": uuid.uuid4(),
"_index": indexname,
"timestamp": datetime.now(),
"player": player
}
playerDocuments.append(document)
print("Bulk adding players to Elasticsearch...")
try:
helpers.bulk(es, playerDocuments)
print("Successful bulk insert")
print(es.count(index=indexname))
except Exception as e:
print("Error during bulk insert into Elasticsearch")
print(str(e))