python连接elasticsearch

import requests

import datetime

import time

url_search = """http://xxx:9200/cdblog-stat-error/_search"""

fmb_time = ((datetime.datetime.now()-datetime.timedelta(minutes=20)).strftime("%Y-%m-%dT%H:%M:%S+08:00"))

now_time = (datetime.datetime.now()).strftime("%Y-%m-%dT%H:%M:%S+08:00")

query = json.dumps({"query": {"bool": {"filter": [{"term": {"cdblog.alarm_words": "terribly wrong"}},{"range": {"@timestamp": {"gte": fmb_time,"lt": now_time}}}]}},"_source": {"includes": ["instinfo.instid","instinfo.ip","instinfo.port","instinfo.insttype","cdblog.timestamp"]}})

try:

    headers = {"Content-Type": 'application/json'}

    user="elastic"

    password="xxx"

    response = requests.get(url_search, data=query,headers=headers,auth=(user,password))

    results = json.loads(response.text)

except exceptions.Timeout as e:

    print('请求超时:'+str(e.message) + url_search)

    except exceptions.HTTPError as e:

    print('http请求错误:'+str(e.message) + url_search)

你可能感兴趣的:(python连接elasticsearch)