-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
48 lines (36 loc) · 1.16 KB
/
Copy pathmain.py
File metadata and controls
48 lines (36 loc) · 1.16 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
"""Module for inserting documents into Elasticsearch."""
import pandas as pd
import urllib3
from dateutil import parser
from elasticsearch import Elasticsearch
from elasticsearch.helpers import bulk
urllib3.disable_warnings()
df = pd.read_csv("csv/collection.csv")
df["date"] = df["date"].apply(parser.parse)
df = df.astype(
{
"id": "int",
"body": "string",
"title": "string",
"date": "datetime64[ns, UTC]",
"court": "string",
"click_context": "string",
"copy_context": "string",
"expanded_copy_context": "string",
}
)
es = Elasticsearch(
"https://localhost:9200",
basic_auth=("elastic", "changeme"),
verify_certs=False,
ssl_show_warn=False,
)
documents = df.to_dict(orient="records")
def doc_generator(data):
"""Generator function for creating documents."""
for _, document in enumerate(data):
yield {"_index": "documents", "_id": document["id"], "_source": document}
print("Inserting documents into Elasticsearch...")
success, failed = bulk(es, doc_generator(documents))
print(f"Successfully inserted: {success} documents")
print(f"Failed to insert: {failed} documents")