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})