Created
January 23, 2019 12:49
-
-
Save esamattis/d1497d46dd4476c3625c0e4c3ce12c4b to your computer and use it in GitHub Desktop.
Use async iteration to iterate over large search results in Elasticsearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Client, SearchParams} from "elasticsearch"; | |
/** | |
* Iterate all search results one by one with async iteration without reading | |
* it all to memory | |
*/ | |
async function* scrollSearch<Document>(esClient: Client, params: SearchParams) { | |
let res = await esClient.search<Document>(params); | |
while (true) { | |
const sourceHits = res.hits.hits; | |
if (sourceHits.length === 0) { | |
break; | |
} | |
for (const hit of sourceHits) { | |
yield hit; | |
} | |
if (!res._scroll_id) { | |
break; | |
} | |
res = await esClient.scroll<Document>({ | |
scrollId: res._scroll_id, | |
scroll: params.scroll || "30s", | |
}); | |
} | |
} | |
async function logAllDocuments(esClient: Client) { | |
const body = { | |
type: "foo", | |
index: "bar", | |
body: { | |
match_all: {}, | |
}, | |
}; | |
for await (const hit of scrollSearch(esClient, {body})) { | |
console.log(hit._source); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment