Elasticsearch分页查询

Posted xiao-xue-di

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Elasticsearch分页查询相关的知识,希望对你有一定的参考价值。

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分页查询的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch教程 elasticsearch 查询数据 | 分页查询

ElasticSearch——分页查询

elastic分页查询scroll

elasticsearch之分页查询

Elasticsearch分页查询超过10000就报错

ElasticSearch实战(二十九)-分页查询