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

python 复制代码
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 = []
相关推荐
爱笑的眼睛111 小时前
超越MSE与交叉熵:深度解析损失函数的动态本质与高阶设计
java·人工智能·python·ai
Rose sait2 小时前
【环境配置】Linux配置虚拟环境pytorch
linux·人工智能·python
过期动态3 小时前
JDBC高级篇:优化、封装与事务全流程指南
android·java·开发语言·数据库·python·mysql
一世琉璃白_Y3 小时前
pg配置国内数据源安装
linux·python·postgresql·centos
liwulin05063 小时前
【PYTHON】COCO数据集中的物品ID
开发语言·python
小鸡吃米…3 小时前
Python - XML 处理
xml·开发语言·python·开源
我赵帅的飞起3 小时前
python国密SM4加解密
python·sm4加解密·国密sm4加解密
yaoh.wang4 小时前
力扣(LeetCode) 1: 两数之和 - 解法思路
python·程序人生·算法·leetcode·面试·跳槽·哈希算法
liwulin05064 小时前
【PYTHON-YOLOV8N】关于YOLO的推理训练图片的尺寸
开发语言·python·yolo
我送炭你添花5 小时前
Pelco KBD300A 模拟器:04+1.Python 打包详解:历史、发展与多种方式对比
python·测试工具·运维开发