【问题标题】:how to do bulk indexing to elasticsearch from python如何从python对elasticsearch进行批量索引
【发布时间】:2017-08-23 14:36:43
【问题描述】:

我有近 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]')

请帮帮我

【问题讨论】:

    标签: python json elasticsearch elasticsearch-bulk-api


    【解决方案1】:

    我更喜欢使用 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',
        ...
    }
    ]
    

    【讨论】:

    • 我不能在“_source”中写入我的 json 文档吗:{ }
    • _source 约定是 elasticsearch 在内部存储数据并以这种方式返回的方式。您应该在字典的第一级传递您的 json 文档。
    • 对不起,我没听懂你,请告诉我应该在 helpers.bulk() 中哪里使用“doc”,我应该在哪里传递我的 json 文档?
    • 是的,我的 json 值是上述格式,我应该在哪里传递该值,我应该在哪里传递 helpers.bulk() 中的“doc”?
    • 试过样本数据 jsonvalue = { '_index': 'your-index', '_type': 'your-type', '_id': 'your-id', 'field_1':{ "priority": "CRITICAL"} } helpers.bulk(es, jsonvalue, chunk_size=1000, request_timeout=200) 但出现错误 elasticsearch.exceptions.RequestError: TransportError(400, u'action_request_validation_exception', u'Validation Failed: 1:缺少索引;2:缺少类型;3:缺少索引;4:缺少类型;5:缺少索引;6:缺少类型;7:缺少索引;8)
    猜你喜欢
    • 2012-12-09
    • 2016-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-17
    • 2017-11-25
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多