Python操作es批量upsert数据

from elasticsearch import Elasticsearch

from elasticsearch import helpers

# elasticsearch集群服务器的地址

es_servers = [

        '10.123.179.102:9200','10.123.179.103:9200','10.123.179.104:9200','10.123.179.105:9200'

    ]

# 创建elasticsearch客户端

es = Elasticsearch(

        es_servers,

        # 启动前嗅探es集群服务器

        sniff_on_start=True,

        # es集群服务器结点连接异常时是否刷新es节点信息

        sniff_on_connection_fail=True,

        # 每60秒刷新节点信息

        sniffer_timeout=60,

        #超时时间

        timeout=10000,

        #重试次数

        max_retries=10,

        #超时时重试

        retry_on_timeout=True

        )

#更新数据集

data = [{

    "_op_type": 'update',

    "_index": 'test_index',

    "_type": '_doc',

    "_id": 11,

    "doc_as_upsert":True,

    "doc": {"newkey": 'newvalue'}

    },{

    "_op_type": 'update',

    "_index": 'test_index',

    "_type": '_doc',

    "_id": 12,

    "doc_as_upsert":True,

    "doc": {"newkey": 'newvalue'}

    },{

    "_op_type": 'update',

    "_index": 'test_index',

    "_type": '_doc',

    "_id": 13,

    "doc_as_upsert":True,

    "doc": {"newkey": 'newvalue'}

    },{

    "_op_type": 'update',

    "_index": 'test_index',

    "_type": '_doc',

    "_id": 14,

    "doc_as_upsert":True,

    "doc": {"newkey": 'newvalue'}

    }

]

helpers.bulk(es, data)


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容