-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsert.py
More file actions
56 lines (45 loc) · 1.65 KB
/
Copy pathinsert.py
File metadata and controls
56 lines (45 loc) · 1.65 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
import os
import requests
from dotenv import load_dotenv
from elasticsearch import Elasticsearch, RequestsHttpConnection, helpers
if __name__ == '__main__':
ans = input(
"Are you sure? document will be duplicated if you do that! Enter y to insure: ")
if ans != "y" and ans != "Y":
exit()
input_path = r"extract.txt"
load_dotenv()
print(os.environ["ESURL"])
try:
# connect to Elasticsearch DB
es = Elasticsearch(os.environ["ESURL"], connection_class=RequestsHttpConnection, use_ssl=True,
verify_certs=False, max_retries=5, retry_on_timeout=True, send_get_body_as='POST')
requests.packages.urllib3.disable_warnings()
i = 0
actions = []
with open(input_path, "r", encoding="utf-8") as f:
for line in f:
i += 1
# print(line[:-1])
category, entity = line[:-1].split("<->")
# print(category, entity)
actions.append({
"_index": "wiki-category2",
"_op_type": "index",
"_source": {
"category": category,
"entity": entity
}
})
if i % 1000 == 0:
print(f"insert {i} documents")
helpers.bulk(es, actions)
actions = []
# break
if len(actions) != 0:
helpers.bulk(es, actions)
actions = []
print("insert complete!")
print("total line:", i)
except Exception as e:
print(e)