python模糊查询es数据库脚本

使用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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值