python调es借口查询数据
from elasticsearch import Elasticsearch
import pymysql
import datetime
from dateutil.relativedelta import relativedelta
beforday = (datetime.date.today() + relativedelta(days=-1)).strftime('%Y.%m.%d')
class Getdata:
def conn_es(self,index_name,query):
es_host = ['10.12.0.26','10.12.0.18','10.12.0.19']
self.index_name = index_name
self.query = query
es = Elasticsearch(
es_host,
sniff_on_start = True,
sniff_on_connection_fail = True,
sniff_timeout = 60,
port = 9200
)
page_num = 100
res = es.search(index= self.index_name, body = self.query, scroll = '5m', size = page_num)
results = res['hits']['hits']
total = res['hits']['total']
scroll_id = res['_scroll_id']
every_num = int(total/page_num)
for i in range(0,every_num+1):
query_scroll = es.scroll(scroll_id = scroll_id ,scroll = '5m')['hits']['hits']
results += query_scroll
return results