Last active
February 7, 2020 12:52
-
-
Save fred-o/7543bf8772172b144fa441020c76f37c to your computer and use it in GitHub Desktop.
Elasticsearch scrolling data extraction
This file contains 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
const ElasticSearch = require('elasticsearch') | |
function scrollingSearch (es, index, type, size, scroll, body, stagger = 0) { | |
return { | |
[Symbol.asyncIterator]: async function* next () { | |
let count = 0 | |
let r = await es.search({ index, type, size, scroll, body }) | |
while (count < r.hits.total) { | |
for (let hit of r.hits.hits) { | |
await new Promise((resolve, reject) => setTimeout(resolve, stagger)) | |
yield hit._source | |
count++ | |
} | |
r = await es.scroll({ scrollId: r._scroll_id, scroll }) | |
} | |
} | |
} | |
} | |
(async function () { | |
let es = new ElasticSearch.Client({ | |
host: 'http://mes01.a.tt.se:9200' | |
}) | |
for await (let hit of scrollingSearch(es, 'dist-text', 'ttninjs', 500, '5m', { | |
query: { | |
range: { | |
versioncreated: { | |
gte: '2018-01-30', | |
lte: '2019-12-16' | |
} | |
} | |
} | |
})) { | |
console.log(hit.versioncreated) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment