使用es的wildcard方法查询elasticsearch数据库,匹配查询时间段、index、内容等,脚本如下
#!/usr/bin/python
# -*- coding: utf-8 -*-
# --------------------------------------------------
# @Time : 2020/10/10 11:40
# @Author : skywin88
# @File : elasticsearch_wildcard.py
# @Version : 1.0
# --------------------------------------------------
#直接执行默认查询时间段为下方默认匹配时间段
#带时间段参数执行例如:
#python elasticsearch_wildcard.py -gt 2020-09-27T08:00:00.056000000+08:00 -lt 2020-09-27T09:00:00.056000000+08:00
# --------------------------------------------------
#es用户名密码地址
esUsername = "admin"
esPassword = "Whrd123456!"
esAddress = "http://10.10.110.160:9200"
#匹配index规则
esIndex = "public.whrd.com-*"
#默认匹配时间段
esDategt = "2020-09-27T08:00:00.000000000+08:00"
esDatelt = "2020-09-27T22:00:00.000000000+08:00"
#匹配字段和匹配值(匹配值可自行增减,支持模糊和正则)
esWildcard = "json.status"
esWildcardvalue = ["*","20*","30*","40*","50*"]
# --------------------------------------------------
from elasticsearch import Elasticsearch
import json
import sys
import argparse
#带参数执行判断,如未带参数则使用默认值;-h可看说明
def parse_arguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument('-gt',default = esDategt,help = "input date and time and timezone e.g. -gt 2020-09-27T08:00:00.000000000+08:00")
parser.add_argument('-lt',default = esDatelt,help = "input date and time and timezone e.g. -lt 2020-09-27T09:00:00.000000000+08:00")
return parser.parse_args(argv)
def main(args):
print("查询时间段为:")
global esDategt
global esDatelt
esDategt = args.gt
esDatelt = args.lt
if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))
print(esDategt)
print(esDatelt)
#带用户密码连接es数据库
es = Elasticsearch(hosts=esAddress, http_auth=(esUsername,esPassword))
#循环查询esWildcardvalue列表中的匹配项
for value in esWildcardvalue:
body = {
"track_total_hits": "true",
"query": {
"bool": {
"must": [
{ "range":
{ "@timestamp":
{ "gt": esDategt,
"lt": esDatelt } }
},
{ "wildcard": { esWildcard: value } }
],
"should": []
}
},
"from": 0,
"size": 0,
"sort": { "@timestamp": "desc" },
"aggs": {}
}
res = es.search(index=esIndex, size=0, body=body)
hits = res["hits"]["total"]["value"]
print("匹配值:"+value)
print(hits)
# print("\n")
执行结果
D:/python3.7.3/python.exe e:/python/test01/elasticsearch_wildcard_list_requests.py -gt 2020-09-27T08:00:00.000000000+08:00 -lt 2020-09-27T09:00:00.000000000+08:00
查询时间段为:
2020-09-27T08:00:00.000000000+08:00
2020-09-27T09:00:00.000000000+08:00
匹配值:*
2137
匹配值:20*
1308
匹配值:30*
0
匹配值:40*
829
匹配值:50*
0