当前位置: 首页 > news >正文

python查询elasticsearch 获取指定字段的值的list

from elasticsearch import Elasticsearch
from datetime import datetime, timedelta# 1.connect to Elasticsearch------------------------------------------------------------------------------------------------------
# prod连接到 Elasticsearch
es_of_prod = Elasticsearch('http://host:port', http_auth=("username", "pwd"), verify_certs=False, timeout=60)# 2.构建KQL-----------------------------------------------------------------------------------------------------------------------
# 查询index
index_of_query = "index-name"
# 获取当前时间并减去1小时
current_time = datetime.now()
previous_hour_time = current_time - timedelta(hours=1)
# 格式化为 "年-月-日 时"
previous_hour_time_formatted = previous_hour_time.strftime("%Y-%m-%d %H")
# 格式化为 "年-月-日 时:分"
previous_hourAndMin_time_formatted = previous_hour_time.strftime("%Y-%m-%d %H:%M")
# 构建 KQL 查询字符串
kql_query_xxx_prod = 'context:"*{query String}*" AND ext.time:"*'+previous_hour_time_formatted+'*"'
# 构建 KQL 查询字符串
kql_query_xxx_prod_hourAndMin = 'context:"*{query String}*" AND ext.time:"*'+previous_hourAndMin_time_formatted+'*"'
print(f"query condition: {kql_query_xxx_prod_hourAndMin}")# 3.构建KQL JSON------------------------------------------------------------------------------------------------------
# 当天 0 点
zero_today = current_time.replace(hour=0, minute=0, second=0, microsecond=0)
# 当天 24 点(直接取次日 0 点)
midnight_24 = zero_today + timedelta(days=1)
#环境情况:sit env:es version7.5.1,prd env:es version7.15.1,下面query_json在sit环境可以使用,在prd环境不能使用
kql_query_xxx_json = {"query": {"bool": {"must": [{"query_string": {"query":kql_query_xxx_prod_hourAndMin,"default_field": "*"  # 搜索所有字段}},{"range": {"@timestamp": {"gte": zero_today,  # 当天00:00:00"lt": midnight_24,   # 第二天00:00:00"time_zone": "+08:00"  # 指定时区(如北京时间)}}}]}}
}
# 4.es执行查询获得hits count大小---------------------------------------------------------------------------------------------------
previous_hour_xxx_count = es_of_prod.count(index = index_of_query,body = kql_query_xxx_json
)
print(f"previous_hour_xxx_count: {previous_hour_xxx_count['count']}")# 5.执行查询,获取某个字段的value的list----------------------------------------------------------------------------------------------
query_body = {"query_string": {"query": kql_query_xxx_prod_hourAndMin,"default_field": "*","analyze_wildcard": True,"lenient": True}
}
#size=10000,如果不设置size,默认只返回前10条数据
try:response = es_of_prod.search(index = index_of_query,query = query_body,size=10000)total = response['hits']['total']['value']print(f"获取到total:{total}")traceids = [hit['_source']['ext.traceId'] for hit in response['hits']['hits']]print(f"获取到{len(traceids)}条traceid记录")#可以遍历traceids,做后续的业务处理
except Exception as e:print(f"查询异常: {str(e)}")traceids = []
http://www.xdnf.cn/news/573049.html

相关文章:

  • 操作系统期末复习(一)
  • 淘宝扭蛋机小程序开发:开启电商娱乐新玩法
  • 工程项目交付质量低?如何构建标准化管理体系?
  • C++网络编程入门学习(四)-- GDB 调试 学习 笔记
  • 第9.1讲、Tiny Encoder Transformer:极简文本分类与注意力可视化实战
  • 计算机操作系统(十)调度的概念与层次,进程调度的时机与进程的调度方式
  • LVLM-AFAH论文精读
  • GitHub SSH Key 配置详细教程(适合初学者,Windows版)-学习记录4
  • CESM2.0 全流程解析:从环境搭建到多模块耦合模拟
  • 打开小程序提示请求失败(小程序页面空白)
  • Python实现蛋白质结构RMSD计算
  • RAG 挑战赛冠军方案解析:从数据解析到多路由器检索的工程实践,推荐阅读!
  • Android Framework开发环境搭建
  • 【Linux庖换现象丁解牛】—进程程序替换!
  • python训练营打卡第30天
  • C++--string类对象
  • 【ffmpeg】ffprobe基本用法
  • 想解决内容同质化难题?运营该从哪入手?
  • linux系统查看硬盘序列号
  • 129.在 Vue3 中使用 OpenLayers 实现点击获取重叠要素信息(支持多 Feature)
  • Spring Boot 登录实现:JWT 与 Session 全面对比与实战讲解
  • ES的倒排索引和正排索引的区别及适用场景?为什么倒排索引适合全文搜索?
  • 目标检测基础知识
  • 使用 electron-builder 打包与发布 Electron 应用
  • Prometheus+Grafana实现对服务的监控
  • 黑色矩形大于6识别
  • RTMP协议解析【二】
  • RTMP协议解析[一]
  • lcd气压表研发方案芯片——用于胎压检测
  • Ubuntu 20.04安装及配置docker