forked from code4hk/budget-explorer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexing.js
100 lines (81 loc) · 2.13 KB
/
indexing.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
var request = require('request');
var parse = require('csv-parse');
var _ = require('lodash');
var elasticsearch = require('elasticsearch');
var headTitles = require('./head-titles.json');
var client = new elasticsearch.Client({
host: 'localhost:9200'
});
var years = [];
for (var i = 2005; i <= 2014; i++) {
years.push(i);
}
function transformToDoc(row, head) {
var docs = [];
_.forEach(years, function(y) {
var doc = {};
var headKey = headTitles[head.toString()];
doc['key'] = row['Key'];
doc['key_not_analyzed'] = headKey + "_" + row['Key'];
doc['year'] = y;
doc['value'] = row[y.toString()];
doc['headId'] = head;
doc['head'] = headKey
console.log(doc['value']);
docs.push(doc);
})
return docs;
}
function sendIndexingCommands(client, indexingCommands) {
client.bulk({
body: indexingCommands
}, function(err, resp) {
// ...
console.log(resp);
});
}
var head = 122;
var parser = parse({
delimiter: ',',
columns: true
}, function(err, data) {
console.log('parsed');
console.log(data);
_.forEach(data, function(row) {
documents = transformToDoc(row, head);
var indexingCommands = [];
_.forEach(documents, function(doc) {
indexingCommands.push({
index: {
_index: 'budget',
_type: head,
id: doc.key + doc.year
}
});
indexingCommands.push(doc);
})
console.log(indexingCommands);
sendIndexingCommands(client, indexingCommands);
})
});
// client.search({
// q: 'pants'
// }).then(function(body) {
// var hits = body.hits.hits;
// }, function(error) {
// console.trace(error.message);
// });
console.log('send request to es');
var url =
"https://docs.google.com/spreadsheets/d/1bAwOqDhvItp9vJDm2NgIpbdguRI7BKc5tHtRtVFUo4Y/export?format=csv";
request
.get(url, function(err, response, body) {
// console.log(response.statusCode) // 200
// console.log(response.headers['content-type']) // 'image/png'
// console.log(response);
console.log(body);
}).pipe(parser);
// # 1 record
// for all years ?
//
// #more long - term 1 record 1 year then aggregate - > min, max, etc