您正在寻找的是分页。
您可以通过查询固定大小并设置
from参数来实现您的目标。既然你要设定显示在250个搜索结果批次,您可以设置
size =250与每个连续的查询,增加值
from的
250。
GET /_search?size=250 ---- return first 250 resultsGET /_search?size=250&from=250 ---- next 250 results GET /_search?size=250&from=500 ---- next 250 results
相反,
Scan & scroll让您通过一次搜索即可检索大量结果,并且理想地用于诸如将数据重新索引为新索引之类的操作。不建议将其用于实时显示搜索结果。
Scan & scroll简要地解释一下,它的主要作用是扫描与扫描请求一起提供的查询的索引并返回a
scroll_id。这
scroll_id可以被传递到下一个滚动请求返回下一批结果。
考虑以下示例-
# Initialize the scrollpage = es.search( index = 'yourIndex', doc_type = 'yourType', scroll = '2m', search_type = 'scan', size = 1000, body = { # Your query's body })sid = page['_scroll_id']scroll_size = page['hits']['total']# Start scrollingwhile (scroll_size > 0): print "Scrolling..." page = es.scroll(scroll_id = sid, scroll = '2m') # 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']) print "scroll size: " + str(scroll_size) # Do something with the obtained page在以上示例中,发生了以下事件-
- 滚动条已初始化。这将返回第一批结果以及scroll_id
- 对于每个后续滚动请求,将发送更新的
scroll_id
(在先前的滚动请求中接收到)并返回下一批结果。 - 滚动时间基本上是使搜索上下文保持活动状态的时间。如果未在设置的时间范围内发送下一个滚动请求,则搜索上下文将丢失并且结果将不会返回。这就是为什么不应将其用于包含大量文档的索引的实时结果显示的原因。



