Elasticsearch Python

Here is my code,

from datetime import datetime 
from elasticsearch import Elasticsearch, helpers
from pprint import pprint
import json 
import time 


es = Elasticsearch(['http://localhost:9200'], http_auth=('elastic', 'elastic'))

f = open("json/data.json", "r")
data = json.loads(f.read())
pprint(data)

# Closing file 
f.close()

response = helpers.bulk(
    es,
    data,
    index = "newChannel",
    doc_type = "_doc"
)


print(response)

















When i Run this code it's give me an error like this --

le "/home/harsh/.local/lib/python3.6/site-packages/elasticsearch/helpers/actions.py", line 187, in _process_bulk_chunk_success
    raise BulkIndexError("%i document(s) failed to index." % len(errors), errors)
elasticsearch.helpers.errors.BulkIndexError: ('4 document(s) failed to index.', [{'index': {'_index': 'newchannel', '_type': '_doc', '_id': 'nhELyX4BvsLZtjn4MKrv', 'status': 400, 'error': {'type': 'mapper_parsing_exception', 'reason': 'failed to parse', 'caused_by': {'type': 'not_x_content_exception', 'reason': 'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes'}}, 'data': 'took'}}, {'index': {'_index': 'newchannel', '_type': '_doc', '_id': 'nxELyX4BvsLZtjn4MKrv', 'status': 400, 'error': {'type': 'mapper_parsing_exception', 'reason': 'failed to parse', 'caused_by': {'type': 'not_x_content_exception', 'reason': 'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes'}}, 'data': 'timed_out'}}, {'index': {'_index': 'newchannel', '_type': '_doc', '_id': 'oBELyX4BvsLZtjn4MKrv', 'status': 400, 'error': {'type': 'mapper_parsing_exception', 'reason': 'failed to parse', 'caused_by': {'type': 'not_x_content_exception', 'reason': 'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes'}}, 'data': '_shards'}}, {'index': {'_index': 'newchannel', '_type': '_doc', '_id': 'oRELyX4BvsLZtjn4MKrv', 'status': 400, 'error': {'type': 'mapper_parsing_exception', 'reason': 'failed to parse', 'caused_by': {'type': 'not_x_content_exception', 'reason': 'Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes'}}, 'data': 'hits'}}])

The error was caused by that data was not formatted appropriately. What does data look like?

1 Like

ok got it @Tomo_M

Data is not in proper form it's Done Now

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.