This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
180 lines (167 loc) · 6.71 KB
/
routes.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var config = require('./config.json');
var models = require('./models');
var es = require('elasticsearch');
var db = new es.Client(config.elasticSearch);
var Q = require('q');
module.exports = [
{ method: 'GET', path: '/businesses', handler: getBusinesses, config: {validate: {query: models.businesses}}},
{ method: 'GET', path: '/amendments', handler: getAmendments, config: {validate: {query: models.amendments}}},
{ method: 'GET', path: '/mergers', handler: getMergers, config: {validate: {query: models.mergers}}},
{ method: 'GET', path: '/officers', handler: getOfficers, config: {validate: {query: models.officers}}}
];
function getBusinesses(request, reply) {
var data = {results: 0, businesses: []};
buildSearch(request.query,'2,3,6,8,9',models.businesses)
.then(function(res) {
data.businesses = res.matches;
data.results = res.hits;
var addtlData = [];
for (var i = 0; i < res.matches.length; i++) {
var query = {id: res.matches[i].id.toString()};
addtlData.push(
buildSearch(query, '4', models.amendments),
buildSearch(query, '7', models.mergers),
buildSearch(query, '5', models.officers)
);
}
return Q.all(addtlData);
})
.then(function(res) {
console.log(res);
for (var i = 0; i < data.length; i++) {
data.businesses[i].amendments = res[i * 3].matches;
data.businesses[i].mergers = res[i * 3 + 1].matches;
data.businesses[i].officers = res[i * 3 + 2].matches;
}
return data;
})
.done(function(data) {
reply(data);
})
}
function getAmendments(request, reply) {
var data = {results: 0, amendments: []};
buildSearch(request.query,'4',models.amendments)
.then(function(res) {
data.amendments = res.matches;
data.results = res.hits;
var addtlData = [];
for (var i = 0; i < res.matches.length; i++) {
var query = {id: res.matches[i].id.toString()};
addtlData.push(buildSearch(query, '2,3,6,8,9', models.businesses));
}
return Q.all(addtlData);
})
.then(function(res) {
for (var i = 0; i < data.amendments.length; i++) {
if(res[i].matches.length > 0) {
data.amendments[i].name = res[i].matches[0].name;
}
}
return data;
})
.done(function(data) {reply(data);});
}
function getMergers(request, reply) {
var data = {results: 0, mergers: []};
buildSearch(request.query,'7',models.mergers)
.then(function(res) {
data.mergers = res.matches;
data.results = res.hits;
var addtlData = [];
for (var i = 0; i < res.matches.length; i++) {
var query = {id: res.matches[i].id.toString()};
var surv_query = {id: res.matches[i].survivor_id.toString()};
addtlData.push(buildSearch(query, '2,3,6,8,9', models.businesses));
addtlData.push(buildSearch(surv_query, '2,3,6,8,9', models.businesses));
}
return Q.all(addtlData);
})
.then(function(res) {
for (var i = 0; i < data.mergers.length; i++) {
if(res[i * 2].matches.length > 0) {
data.mergers[i].name = res[i * 2].matches[0].name;
}
if(res[i * 2 + 1].matches.length > 0) {
data.mergers[i].survivor_name = res[i * 2 + 1].matches[0].name;
}
}
return data;
})
.done(function(data) {reply(data);});
}
function getOfficers(request, reply) {
var data = {results: 0, officers: []};
buildSearch(request.query,'5',models.officers)
.then(function(res) {
data.officers = res.matches;
data.results = res.hits;
var addtlData = [];
for (var i = 0; i < res.matches.length; i++) {
var query = {id: res.matches[i].id.toString()};
addtlData.push(buildSearch(query, '2,3,6,8,9', models.businesses));
}
return Q.all(addtlData);
})
.then(function(res) {
for (var i = 0; i < data.officers.length; i++) {
if(res[i].matches.length > 0) {
data.officers[i].business_name = res[i].matches[0].name;
}
}
return data;
})
.done(function(data) {reply(data);});
}
//@todo move build search to own class and test
function buildSearch(query, type, model) {
var notAddedToQuery = ['coordinates', 'dist', 'start', 'limit'];
var query_params = [];
var search_query = {};
var matches = [];
var deferred = Q.defer();
for (var k in query) {
if (notAddedToQuery.indexOf(k) === -1) {
var term = {};
term[k] = query[k];
query_params.push({match: term});
}
}
search_query.index = 'business';
search_query.from = query.start || 0;
search_query.size = query.limit || 10;
search_query.type = type;
search_query.body = {};
if ('coordinates' in query) {
search_query.body.query = {};
search_query.body.query.filtered = {};
search_query.body.query.filtered.filter = {};
search_query.body.query.filtered.filter.geo_distance = {
distance: query['dist'].toString() + 'm',
coordinates: [query['coordinates'][0], query['coordinates'][1]]
};
if (query_params.length > 0) {
search_query.body.query.filtered.query = {bool: {must: query_params}};
}
} else {
search_query.body.query = {bool: {must: query_params}};
}
db.search(search_query).then(function(body) {
var hits = body.hits.hits;
for (var i = 0; i < hits.length; i++) {
matches[i] = {};
for (var k in model["_inner"]["children"]) {
var key = model["_inner"].children[k].key;
if (hits[i]["_source"].hasOwnProperty(key)) {
matches[i][key] = hits[i]["_source"][key];
}
}
}
var response = {hits: body.hits.total, matches: matches};
deferred.resolve(response);
}, function (err) {
deferred.reject(err);
console.trace(err.message);
});
return deferred.promise;
}