如何在Python中应用批量api的新映射?

时间:2018-05-30 09:15:14

标签: python elasticsearch mapping

我使用bulk API将文档从远程弹性文件复制到本地文件。我想应用新的映射。在第一个函数中,我将新映射应用为风箱:

def create_mapping(es, idx, document_type):
    mymapping = {"mappings": {document_type: { \
        "properties": { \
            "prijs": {"type": "integer"},\
            "datum": {"type": "date"},\
            "kilometerstand": {"type": "integer"}
            }}}}

    if es.indices.exists(index=idx):
        es.indices.delete(index=idx)

    es.indices.create(index=idx, body=mymapping)

在第二个函数中,我使用bulk复制文档:

def copy_index(remote_es, local_es, idx):
    ....
    bulk(local_es, response_list, chunk_size=5000)

但我收到错误:document(s) failed to index

修改

如果我按bulk添加多个两个文档,则会显示:

raise BulkIndexError('%i document(s) failed to index.' % len(errors), errors) BulkIndexError: (u'2 document(s) failed to index.', [{u'index': {u'status': 400, u'_type': u'kliks', u'_index': u'logstash-2018.02.07', u'error': {u'caused_by': {u'reason': u'Invalid format: "2018-02-07 01:00:51" is malformed at " 01:00:51"', u'type': u'illegal_argument_exception'}, u'reason': u'failed to parse [datum]', u'type': u'mapper_parsing_exception'}, ....

1 个答案:

答案 0 :(得分:0)

我指定了date格式并修正了错误:

"datum": {"type": "date", "format":"yyyy-MM-dd HH:mm:ss"}