How to enable scroll functionality in elastic search How to enable scroll functionality in elastic search elasticsearch elasticsearch

How to enable scroll functionality in elastic search


I guess elasticsearch from and size will do the trick for you if you have doc less than ≤ 10k. But still if you want to use the scroll API then this is what you need,

    # declare a filter query dict object    match_all = {        "size": 7,        "query": {            "match_all": {}        }    }    # make a search() request to get all docs in the index    resp = client.search(        index = 'employees',        body = match_all,        scroll = '2s' # length of time to keep search context    )        # process the first 7 documents here from resp    # iterate over the document hits for each 'scroll'    for doc in resp['hits']['hits']:        print ("\n", doc['_id'], doc['_source'])        doc_count += 1        print ("DOC COUNT:", doc_count)        # keep track of pass scroll _id    old_scroll_id = resp['_scroll_id']    # use a 'while' iterator to loop over document 'hits'    while len(resp['hits']['hits']):        # make a request using the Scroll API        resp = client.scroll(            scroll_id = old_scroll_id,            size = 7,            scroll = '2s' # length of time to keep search context        )        # iterate over the document hits for each 'scroll'        for doc in resp['hits']['hits']:            print ("\n", doc['_id'], doc['_source'])            doc_count += 1            print ("DOC COUNT:", doc_count)

See Ref: https://kb.objectrocket.com/elasticsearch/how-to-use-python-to-make-scroll-queries-to-get-all-documents-in-an-elasticsearch-index-752