用python脚本更新Elasticsearch数据库

本文介绍了如何使用Python通过ElasticsearchAPI从一个本地文本文件中读取callid,然后根据callid更新Elasticsearch索引中相关文档的languagename字段。原始代码和针对新版本的优化版本都包含在内,展示了向_doc类型文档的批量更新过程。
摘要由CSDN通过智能技术生成

这段代码的主要功能是通过Elasticsearch与一个索引(index)进行交互,读取本地文本文件中的callid,并根据callid更新索引中相关文档的字段值。以下是详细解释: 

# -*- coding: utf-8 -*-
# @Time    : 2023/3/29 11:27
# @Author  : hjcui
# @Site    : 
# @File    : Update_ES.py
# @Software: PyCharm

# 原始的
from elasticsearch import Elasticsearch
import os

es_conn = Elasticsearch(['194.169.55.12:9200'])
index = 'cr-all-2023.03'
type = 'doc'
suffix = '505_'
src_file = r'./wewr_callid_0328.txt'

with open(src_file,'r',encoding='utf-8') as sf:
    for callid in sf:
        guid = suffix + callid.strip()
        es_query = \
            {
                "query": {
                    "term": {
                        "callid.keyword": {
                            "value": callid.strip()
                        }
                    }
                }
            }
        docs = es_conn.search(index=index,body=es_query)
        source = docs['hits']['hits'][0]['_source']
        # 如果要修改的字段值与数据库中的不一致时,显示'successful': 1,否则是'successful': 0
        result = es_conn.update(index=index,doc_type='doc',id=guid,body={'doc':{'languagename':"other"}})
        print(result)



# 针对新版本优化的
# -*- coding: utf-8 -*-
from elasticsearch import Elasticsearch, helpers
import os

es_conn = Elasticsearch(['194.169.55.12:9200'])
index = 'index-2023.03'
doc_type = '_doc'  # 自Elasticsearch 7.x版本起,推荐使用_doc作为默认类型
suffix = '505_'
src_file = r'./wewr_callid_0328.txt'

def update_docs(callids):
    actions = []
    for callid in callids:
        guid = suffix + callid.strip()
        es_query = {
            "_id": guid,
            "_index": index,
            "doc": {"languagename": "other"},
            "doc_as_upsert": True  # 如果文档不存在,则创建新文档(upsert)
        }
        actions.append({"update": es_query})

    if actions:
        # 批量执行更新操作
        result = helpers.bulk(es_conn, actions)
        print(f"成功更新/插入了{result[0]}个文档,失败了{result[1]}个。")

with open(src_file, 'r', encoding='utf-8') as sf:
    callids = [line.strip() for line in sf]
    
    # 对读取到的callids列表进行更新操作
    update_docs(callids)

  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值