码迷,mamicode.com
首页 > 其他好文 > 详细

Elasticsearch分页查询

时间:2019-10-17 15:30:43      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:==   result   hidden   sts   isp   page   diff   meta   hits   

global index
global CLIENT
index = "guajibao-ipused-2019.10.13"
CLIENT = Elasticsearch(hosts=es_host,port=port,timeout=timeout)



def load_all_data(scroll_id):
    if scroll_id:
        page = CLIENT.scroll(scroll_id=scroll_id, scroll=10m)
        scroll_id = page[_scroll_id]
    else:
        page = CLIENT.search(index=index, scroll=10m, size=100)
        scroll_id = page[_scroll_id]
    print(page)
    data = page["hits"][hits]
    return {"data": data, "scroll_id": scroll_id, "length": len(data)}





例子

def load_all_data(scroll_id):
    if scroll_id:
        page = CLIENT.scroll(scroll_id=scroll_id, scroll=10m)
        scroll_id = page[_scroll_id]
    else:
        page = CLIENT.search(index=index, scroll=10m, size=100)
        scroll_id = page[_scroll_id]
    # print(page)
    data = page["hits"][hits]
    total = page[hits][total][value]
    print(data)
    return  {"data": data, "scroll_id": scroll_id, "length": len(data), "total": total}




if __name__ == "__main__":
    result = load_all_data(None)
    total =  result[total]
    scroll_id = result[scroll_id]
    for i in range(int(total/100)):
        scroll_id = load_all_data(scroll_id)[scroll_id]

 


转至元数据结尾

 

转至元数据起始

 

global index
global CLIENT
index = "guajibao-ipused-2019.10.13"
CLIENT = Elasticsearch(hosts=es_host,port=port,timeout=timeout)



def load_all_data(scroll_id):
    if scroll_id:
        page = CLIENT.scroll(scroll_id=scroll_id, scroll=‘10m‘)
        scroll_id = page[‘_scroll_id‘]
    else:
        page = CLIENT.search(index=index, scroll=‘10m‘size=100)
        scroll_id = page[‘_scroll_id‘]
    print(page)
    data = page["hits"][‘hits‘]
    return {"data": data, "scroll_id": scroll_id, "length"len(data)}

 

 

例子

def load_all_data(scroll_id):
    if scroll_id:
        page = CLIENT.scroll(scroll_id=scroll_id, scroll=‘10m‘)
        scroll_id = page[‘_scroll_id‘]
    else:
        page = CLIENT.search(index=index, scroll=‘10m‘size=100)
        scroll_id = page[‘_scroll_id‘]
    # print(page)
    data = page["hits"][‘hits‘]
    total = page[‘hits‘][‘total‘][‘value‘]
    print(data)
    return  {"data": data, "scroll_id": scroll_id, "length"len(data), "total": total}




if __name__ == "__main__":
    result = load_all_data(None)
    total =  result[‘total‘]
    scroll_id = result[‘scroll_id‘]
    for i in range(int(total/100)):
        scroll_id = load_all_data(scroll_id)[‘scroll_id‘]

Elasticsearch分页查询

标签:==   result   hidden   sts   isp   page   diff   meta   hits   

原文地址:https://www.cnblogs.com/xiao-xue-di/p/11692212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!