简体   繁体   English

将 JSON 数组插入 Elasticsearch 使用 Python 批量 API

[英]Insert JSON Array into Elasticsearch Using Python Bulk API

I have a JSON array of 100 records, I want to insert it into elasticsearch,I tried it using the below code but it gives me JSON Error exception.我有一个包含 100 条记录的 JSON 数组,我想将它插入到 elasticsearch 中,我使用下面的代码进行了尝试,但它给了我 JSON 错误异常。 I am not sure where I am doing it wrong.我不确定我在哪里做错了。 The code which I am using to insert record is我用来插入记录的代码是


from esService.esClient import ESCient
from elasticsearch.helpers import bulk

#data is the json array object which has around 100 records in it .

#I want to insert it into elasticsearch in such a way 

#that each record is a entry into ES (I can remove the ID if required  ,I don't need the ID ) 

    def insert_bulk_record(self,index,data,id):
        docs = []
        doc = {
            "_index": index,
            "_id": id,
            "_source": data
        }
        docs.append(doc)

        bulk(self.esconnect, docs)

When I do bulk insert using the above code I get the below exception,当我使用上面的代码进行批量插入时,出现以下异常,

elasticsearch.helpers.errors.BulkIndexError: ('1 document(s) failed to index.', [{'index': {'_index': 'data_record', '_type': '_doc', '_id': '7908745568_0.csv', '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'}}

This error means that you are not passing the correct format to Elastic for indexing the data.此错误意味着您没有将正确的格式传递给 Elastic 以索引数据。 Bulk helpers accept single JSON objects and not JSON arrays. Bulk helpers接受单个 JSON 对象而不是 JSON arrays。

For example, you might try something like this:例如,您可以尝试这样的事情:

for d in data:
    docs = []
    doc = {
        "_index": index,
        "_source": d
    }
    docs.append(doc)

    bulk(self.esconnect, docs)

Have a look at this blog as well.也看看这个博客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM