-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathimport.js
64 lines (53 loc) · 1.38 KB
/
import.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
'use strict';
/**
* 导入全部的数据
*/
var elasticsearch = require('elasticsearch'),
index = require('./models/index'),
Resource = index.Resource,
config = require('./config'),
client = new elasticsearch.Client({
host: config.elasticsearchHost,
log: 'error'
});
var _id = 0,
count = 0;
function run() {
Resource.find({_id: {$gt: _id}}).select('_id n s t c').limit(10).exec(function (err, value) {
if (err) {
throw new Error(err);
}
// 输出信息
console.log(_id + ' ' + count);
if (value.length <= 0) {
console.log('Done!');
return;
}
// 添加到elasticsearch
client.bulk({
index: 'antcolony',
type: 'resource',
body: formatData(value)
}, function (error, response) {
if (error) {
throw new Error(error);
}
// 继续下一组
count += 10;
_id = value[value.length - 1]._id;
process.nextTick(run);
});
});
}
function formatData(data) {
var result = [];
for (var i = 0, j = data.length; i < j; i++) {
var item = data[i].toJSON();
result.push({create: {_id: item._id}});
delete item._id;
result.push(item);
}
return result;
}
console.log('Running......');
run();