-
Notifications
You must be signed in to change notification settings - Fork 0
/
replicator.js
131 lines (106 loc) · 2.96 KB
/
replicator.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"use strict";
const _ = require("lodash");
const getCheckpoint = async (task, sourceClient) => {
const { index, id } = task.source.getCheckpointOpts;
console.log("getCheckpoint index: " + index);
console.log("getCheckpoint id: " + id);
const getResult = await sourceClient.get(
{
id: id,
index: index,
type: "_doc",
},
{ ignore: [404] }
);
const data = _.get(getResult, "body._source.latest_commit", null);
console.log("getCheckpoint, latest_commit: " + data);
return Promise.resolve(data);
};
const setCheckpoint = async (task, sourceClient, latestCommit) => {
if (!latestCommit) return;
const { index, id } = task.source.getCheckpointOpts;
console.log(
"setCheckpoint, setting index/id = value: " +
index +
"/" +
id +
" = " +
latestCommit
);
await sourceClient.index({
id: id,
index: index,
body: {
latest_commit: latestCommit,
},
});
};
const syncDocuments = async (
task,
sourceClient,
destinationClient,
checkpoint
) => {
// the field that has the updated date in it
const updatedField = task.source.searchUpdatedField;
let query = { range: { [updatedField]: { gt: 0 } } };
if (checkpoint !== null) {
console.log("syncDocuments, using checkpoint value: " + checkpoint);
query = {
range: {
[updatedField]: {
gt: checkpoint,
},
},
};
}
const params = {
...task.source.searchOpts,
body: {
sort: [{ updated: { order: "asc" } }],
query: query,
},
};
let sourceResponse = await sourceClient.search(params);
let totalRecords = 0;
let latestCommit = checkpoint;
while (true) {
const sourceHits = sourceResponse.body.hits.hits;
// no data, just finish
if (sourceHits.length === 0) break;
const destinationBulkData = sourceHits.flatMap((doc) => {
return [
{ index: { _index: task.destination.index, _id: doc._id } },
doc._source,
];
});
await destinationClient.bulk({
body: destinationBulkData,
});
totalRecords += sourceHits.length;
console.log("Done bulk update of " + sourceHits.length + " records");
// get the last update latest_commit, and save that as a checkpoint before getting
// the next batch
const latestCommitInBatch =
sourceHits[sourceHits.length - 1]._source[updatedField];
if (latestCommitInBatch) {
await setCheckpoint(task, sourceClient, latestCommitInBatch);
latestCommit = latestCommitInBatch;
}
if (totalRecords >= _.get(sourceResponse, "body.hits.total.value", 0)) {
console.log("Breaking loop completed all hits");
break;
}
if (!sourceResponse.body._scroll_id) break;
sourceResponse = await sourceClient.scroll({
scrollId: sourceResponse.body._scroll_id,
scroll: params.scroll,
});
}
return Promise.resolve({ totalRecords, latestCommit });
};
module.exports = {
syncDocuments,
getCheckpoint,
setCheckpoint,
};