如何从python进行批量索引到elasticsearch

RCP*_*RCP 0 python json elasticsearch elasticsearch-bulk-api

我有将近1万个json文档,我想通过使用来自python的elasticsearch bulk api将所有这些文档推送到elasticsearch。我浏览了一些文档,但没有任何解决方案。

result=es.bulk(index="index1", doc_type="index123", body=jsonvalue)
helpers.bulk(es,doc) 
Run Code Online (Sandbox Code Playgroud)

我都尝试了两次,但没有结果,我收到此错误

elasticsearch.exceptions.RequestError: TransportError(400, u'illegal_argument_exception', u'Malformed action/metadata line [1], expected START_OBJECT or END_OBJECT but found [VALUE_STRING]')
Run Code Online (Sandbox Code Playgroud)

请帮我

Jai*_*rma 5

我更喜欢使用助手模块中存在的批量方法进行批量索引。请尝试以下操作:

from elasticsearch import helpers
res = helpers.bulk(es, jsonvalue, chunk_size=1000, request_timeout=200)
Run Code Online (Sandbox Code Playgroud)

您的jsonvalue必须遵循特定格式。它必须是10K json文档的列表,每个文档具有以下字段:

doc = {
    '_index': 'your-index',
    '_type': 'your-type',
    '_id': 'your-id',
    'field_1': 'value_1',
    ...
}
Run Code Online (Sandbox Code Playgroud)

因此,您最终的jsonvalue如下所示:

jsonvalue = [
    {
    '_index': 'your-index',
    '_type': 'your-type',
    '_id': 'your-id',
    'field_1': 'value_1',
    ...
},
    {
    '_index': 'your-index',
    '_type': 'your-type',
    '_id': 'your-id',
    'field_1': 'value_2',
    ...
},
    {
    '_index': 'your-index',
    '_type': 'your-type',
    '_id': 'your-id',
    'field_1': 'value_3',
    ...
}
]
Run Code Online (Sandbox Code Playgroud)