3

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

result=es.bulk(index="index1", doc_type="index123", body=jsonvalue)
helpers.bulk(es,doc) 

我都试过了,但没有结果,我收到了这个错误

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]')

请帮我

4

1 回答 1

8

我更喜欢使用 helpers 模块中的 bulk 方法进行批量索引。尝试以下操作:

from elasticsearch import helpers
res = helpers.bulk(es, jsonvalue, chunk_size=1000, request_timeout=200)

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

doc = {
    '_index': 'your-index',
    '_type': 'your-type',
    '_id': 'your-id',
    'field_1': 'value_1',
    ...
}

所以你最终的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',
    ...
}
]
于 2017-08-23T06:20:31.287 回答