| endpoint | scroll |
|---|---|
| lang | javascript |
| es_version | 9.3 |
| client | @elastic/elasticsearch@9.3.0 |
Use client.scroll() to retrieve the next batch of results from a
scrolling search. Start by calling search with a scroll
parameter, then repeatedly call scroll until no more hits are
returned:
let response = await client.search({
index: "products",
scroll: "1m",
size: 2,
query: { match_all: {} },
});
let scrollId = response._scroll_id;
while (response.hits.hits.length > 0) {
for (const hit of response.hits.hits) {
console.log(`${hit._id}: ${hit._source.name}`);
}
response = await client.scroll({ scroll_id: scrollId, scroll: "1m" });
scrollId = response._scroll_id;
}Always clear the scroll context when you are done to free server
resources. Pass the last scroll_id you received:
await client.clearScroll({ scroll_id: scrollId });To clear every open scroll context at once, omit the scroll_id:
await client.clearScroll();The scrollSearch helper wraps scrolling into an async iterator.
Prefer this over manual scrolling in most cases:
for await (const result of client.helpers.scrollSearch({
index: "products",
query: { match_all: {} },
})) {
for (const hit of result.documents) {
console.log(`${hit.name}`);
}
}