-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll-finder.js
65 lines (52 loc) · 1.34 KB
/
scroll-finder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { Client } = require('@elastic/elasticsearch');
const config = require('./config.json');
const sourceURL = process.argv[2];
const sourceIndex = process.argv[3];
const sourceAuth = config[sourceURL] || {};
const finder = process.argv[process.argv.length - 1];
const finderFn = require(`./${finder}`);
const sourceClient = new Client({
node: `${sourceURL}`,
auth: {
username: sourceAuth.username,
password: sourceAuth.password
}
});
const SIZE = 100;
const run = async () => {
const response = await sourceClient.search({
index: sourceIndex,
scroll: '2m',
size: SIZE,
body: {
query: {
match_all: {}
}
}
});
const responseQueue = [];
const foundResult = [];
responseQueue.push(response);
// Transform
while (responseQueue.length) {
const body = responseQueue.shift();
const docs = body.hits.hits.map(d => d._source);
if (docs.length === 0) break;
for (const doc of docs) {
const found = await finderFn(doc);
if (found) {
foundResult.push(doc.id || doc);
}
};
console.log('.....debug', body._scroll_id);
// Get the next response if there are more quotes to fetch
responseQueue.push(
await sourceClient.scroll({
scroll_id: body._scroll_id,
scroll: '2m'
})
);
}
console.log(foundResult);
}
run();