1. 程式人生 > >python使用Elasticsearch庫下載索引資料

python使用Elasticsearch庫下載索引資料

from elasticsearch import Elasticsearch
es= Elasticsearch(hosts=[{'host': 'localhost', 'port': 9210}])
dealnum=0
if __name__=='__main__':
	#查詢條件
    es_search_options = {'query': {'match_all': {}}}
    #查詢的索引名稱
    es_index='dns_2018_09_26_ipdomainrelation'
    #查詢的文件名稱
    es_type='br2004'
    #翻頁查詢
resp =es.search(es_index,es_type,body=es_search_options,scroll="1m",size=100) print(len(resp['hits']['hits'])) scroll_id = resp['_scroll_id'] resp_docs = resp["hits"]["hits"] total = resp['hits']['total'] count = len(resp_docs) datas = resp_docs print("total:"+str(total)
) print(scroll_id) #迴圈翻頁查詢 while len(resp_docs)>0: scroll_id=resp['_scroll_id'] #對於版本1.0的es,scroll_id和body一定都要傳,否則會出錯 resp = es.scroll(scroll_id=scroll_id, body={'scroll_id':scroll_id},scroll="1m") resp_docs = resp["hits"]["hits"] datas.extend(resp_docs)
count += len(resp_docs) dealnum += 1 print("dealnum::=="+str(dealnum)) if count >= total: break print(len(datas))