-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitialize.py
59 lines (44 loc) · 1.71 KB
/
initialize.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
from elasticsearch7 import Elasticsearch, helpers
import json
import configs
import os
import time
from datetime import datetime
from time import mktime
def load_songs():
song_dir=os.path.join(os.getcwd(), "songs")
for filename in os.listdir(song_dir):
with open(os.path.join(song_dir, filename), 'r') as f:
data = json.load(f)
date=data['published_on']
time_obj_default = time.strptime(date, "%b %d, %Y")
a= datetime.fromtimestamp(mktime(time_obj_default))
data['published_on']=str(a)[:10]
yield{
"_index": configs.INDEX,
"_source":data
}
if __name__=="__main__":
print("\nInitializing . . .")
es_client = Elasticsearch(HOST=configs.HOST, PORT=configs.PORT)
print("\nConnecting . . .")
print(es_client.info())
exists = es_client.indices.exists(index=configs.INDEX)
print("\nIndex Already Exists :",exists)
if exists:
result = es_client.indices.delete(index=configs.INDEX)
print("\nExisting Index Deleting, Result")
print(result)
result = es_client.indices.create(index=configs.INDEX,body=configs.PARAMS)
print("\nNew Index Creating, Result")
print(result)
# result = es_client.indices.put_settings(index=configs.INDEX,body=configs.PARAMS.get("settings"))
# print("\nConfiguring Settings, Result")
# print(result)
# result = es_client.indices.put_mapping(index=configs.INDEX,body=configs.PARAMS.get("mappings"))
# print("\nConfiguring Mapping, Result")
# print(result)
print("Adding Data . . .")
result = helpers.bulk(es_client,load_songs())
print("Data Added, Result")
print(result)