日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python更新es数据_python操作es增删改查

發布時間:2023/12/20 python 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python更新es数据_python操作es增删改查 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.查詢(search)

# 獲取案例庫信息

@app.route('/get_dcn_cases', methods=['GET', 'POST'])

def get_dcn_cases():

# 告警事件數據

dcn_cases_query = {

"_source": {

"includes": ["faultName", "endTime", "startTime", "checked", "solution"],

},

"sort": {"timestamp": {"order": "desc"}},

"size": 9999

}

res = es.search(index='dcn-cases', body=dcn_cases_query)

res_hits = res['hits']['hits']

dcn_cases_data = []

for i in res_hits:

i['_source']['id'] = i['_id']

dcn_cases_data.append(i['_source'])

print("dcn_cases_data:", dcn_cases_data)

return jsonify({'dcn_cases_data': dcn_cases_data})

2.增加

# 新增案例庫到es里面

from elasticsearch import helpers

@app.route('/add_case_to_es', methods=['GET', 'POST'])

def add_case_to_es():

# 獲取前端傳遞的案例添加參數

case_data = request.get_json()

# 獲取系統當前時間

t = time.time()

current_ts = int(round(t * 1000))

# 格式化我們能看懂的時間

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(current_ts / 1000)))

# 格式化es的可以看得懂的時間

print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime((current_ts / 1000) - 28800)))

case_data["timestamp"] = str(time.strftime('%Y-%m-%dT%H:%M:%S', time.localtime((current_ts / 1000) - 28800)))

case_data["type"] = "case"

app.logger.debug('case_data=%s', case_data)

bulks = []

_index = 'dcn-cases'

_type = 'doc'

bulks.append({

"_index": _index,

"_type": _type,

"_source": case_data

})

helpers.bulk(es, bulks)

# res_update = es.index(index='dcn-cases', doc_type='doc', body=case_data)

res_update = 1

return jsonify({'res_update': res_update})

3.刪除(根據id來刪除)(delete)

# 刪除案例庫里面的案例

@app.route('/del_case_to_es', methods=['GET', 'POST'])

def del_case_to_es():

# 獲取前端傳遞的案例添加參數

del_id_data = request.get_json()

app.logger.debug('del_id_data=%s', del_id_data)

app.logger.debug('del_id_data=%s', del_id_data['del_id'])

res_del = es.delete(index='dcn-cases', doc_type='doc', id=del_id_data['del_id'])

return jsonify({'res_del': res_del})

4.修改(update)

# 修改案例庫

@app.route('/update_case_to_es', methods=['GET', 'POST'])

def update_case_to_es():

# 獲取前端傳遞的查詢參數

case_data = request.get_json()

app.logger.debug('case_data=%s', case_data)

case_id = case_data['case_id']

# 移除字典中的id信息

del case_data['case_id']

res_update = es.update(index='dcn-cases', doc_type='doc', id=case_id, body={"doc": case_data})

print(res_update['_shards']['successful'])

return jsonify({'res_update': res_update})

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的python更新es数据_python操作es增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。