python使用Elasticsearch库下载索引数据

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liaoyanyunde/article/details/82879282
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))

猜你喜欢

转载自blog.csdn.net/liaoyanyunde/article/details/82879282