When using the scroll elasticSearch api, can I go to page n?

I am using elasticsearch api scrollbar. In some cases, I would like to return hits on page n without returning hits from previous pages. I believe it should be like an iterator. So I would just like to pass the iterator through the first few pages, but then actually return the hits of the nth page.

My current code

initial_request = client.search(index = index, doc_type = doc_type, body = q, scroll = str(wait_time) + 'm', search_type = 'scan', size = size) 

sid = initial_request['_scroll_id'] ## scroll id
total_hits = initial_request['hits']['total'] ## how many results there are. 
scroll_size = total_hits ## set this to a positive value initially

while scroll_size > 0:

    p += 1
    print "\t\t Scrolling to page %s ..." %p

    page = client.scroll(scroll_id = sid, scroll = str(wait_time) + 'm')

    sid = page['_scroll_id'] # Update the scroll ID
    scroll_size = len(page["hits"]["hits"]) ## no. of hits returned on this page
    ## then code to do stuff w/ that page hits. 

but page = client.scroll(...)sends hits of this page back to my local machine. I would just like passon the first n pages, and then start sending page images after that.

Any ideas?

+4
source share

Source: https://habr.com/ru/post/1656125/


All Articles