Hello everyone!
Need a help here. Here is my code:
import json
import os
from elasticsearch import Elasticsearch, RequestError
from elasticsearch.helpers import bulk
def create_index(es_client, index_name, settings):
try:
es_client.indices.create(index_name, body=settings)
print("Index created")
return True
except Exception as err:
if err.args[1] == "resource_already_exists_exception":
print("Index already exists")
return True
return False
def upload(index_name, data):
for i, item in enumerate( data,1):
print(f"item {i} from {len(data)} in process...")
yield {
"_index": index_name,
"_source": item
}
if __name__== '__main__':
folder = "Python"
ALL_DATA = []
for file in os.listdir(folder):
filepath = os.path.join(folder,file)
if filepath.endswith(".json"):
with open(filepath, "r", encoding="utf-8") as f_in:
data = json.load(f_in)
ALL_DATA += data
es = Elasticsearch()
with open("mapping.json", "r", encoding="utf-8") as f_in:
settings = json.load(f_in)
index_name = "data_test"
if create_index (es, index_name, settings):
bulk(es, upload(index_name, data))
This code seems to run into a trouble. It gives me "raise bulkindexerror( i document(s) failed to index. len(errors) errors)" error, and it won't upload more than 6 documents. What should I fix in this code for it to work?