-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes-utils.js
69 lines (63 loc) · 1.48 KB
/
es-utils.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
module.exports.sendToElastic = (client, data) => {
let body = [];
let dataLen;
if (Array.isArray(data)) {
dataLen = data.length;
if (dataLen == 0) return;
data.forEach((doc) => {
body.push({ index: { _index: doc.type, _type: doc.type, _id: doc.id } });
body.push(doc)
})
}
else {
dataLen = Object.keys(data).length;
if (dataLen == 0) return;
Object.keys(data).forEach((key) => {
let doc = data[key];
body.push({ index: { _index: doc.type, _type: doc.type, _id: key } });
body.push(doc)
})
}
indexPreparedBulk(client, body, 10000)
}
module.exports.ensureIndex=(client, settings, cbFinish, cbError)=>{
client.indices.get({index:"obec"})
.then(
(result)=>{
console.log("index exist");
cbFinish();
}
,(reject)=>{
console.log;("no index");
if (cbError) cbError(reject);
}
)
.catch( (err)=>{
if (cbError) cbError(err);
})
}
function indexPreparedBulk(client, bulk, maxBulk, cbFinish, cbError) {
if (bulk.length<=0) {
if (cbFinish) cbFinish();
return;
}
client.bulk(
{
body: bulk.slice(0, maxBulk * 2)
},
(err, res) => {
if (err){
console.log("ES error while bulk", err);
if (cbError) cbError(err);
}
else {
let indexed=bulk.length>maxBulk/2?maxBulk:bulk.length/2;
if (res.errors){
console.log(JSON.stringify(res,true,4))
}
console.log(`indexed ${indexed} remaining ${bulk.length/2-indexed}` );
process.nextTick(()=>{indexPreparedBulk(client, bulk.slice(maxBulk * 2),maxBulk);})
}
}
)
}