Reputation:
I have a web url api which serving by the elastic search.
def elastic_search():
"""
Return full search using match_all
"""
try:
full_search= es.search(index="employees",scroll = '2m',size = 10,body={ "query": {"match_all": {}}})
hits_search = full_search['hits']['hits']
return hits_search
except Exception as e:
logger.exception("Error" + str(e))
raise
I have modified above code like below
sid = search["_scroll_id"]
scroll_size = search['hits']['total']
scroll_size = scroll_size['value']
# Start scrolling
while (scroll_size > 0):
#print("Scrolling...")
page = es.scroll(scroll_id = sid, scroll = '1m')
#print("Hits : ",len(page["hits"]["hits"]))
# Update the scroll ID
sid = page['_scroll_id']
# Get the number of results that we returned in the last scroll
scroll_size = len(page['hits']['hits'])
search_text = page['hits']['hits']
print (search_text)
My api is throwing []
because my last search_text
giving blank.
In the log it is printing each set of 7 employees. But My web url api is loading loading and last it showing blank page
Please help in updating in returning "hits_search" which is in elastic_search function
Upvotes: 5
Views: 1467
Reputation: 38502
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)
Upvotes: 2