-
Notifications
You must be signed in to change notification settings - Fork 3
/
search.js
260 lines (219 loc) · 7.77 KB
/
search.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'use strict';
var env = require('require-env'),
escape = require('pg-escape'),
pg = require('pg'),
sanitizer = require('sanitizer'),
getPlace = require('../library/get-place.js'),
hipcamp = require('../lib/hipcamp.js');
function buildQuery(dbQuery, data, callback) {
// score = (total tweets + total photos + total checkins + total tips) (* 10 if they have activity data)
var fullQuery, activitiesColumnSQLslug, activitiesWhereSQLslug, queryArray;
// TODO site_tweets needs to be updated with new superunit_ids
// superunit_ids
var scoreSubQuery = [
', (',
' WITH stats AS (',
' SELECT * FROM park_stats WHERE park_stats.superunit_id = shortlist.superunit_id',
' ),',
' hipcamp AS (',
' SELECT (activities ->> \'activityCount\')::integer AS activities FROM site_hipcamp_activities WHERE site_hipcamp_activities.su_id = shortlist.superunit_id',
' )',
' SELECT (stats.flickr_photo_count + stats.instagram_photo_count + stats.tweet_count + stats.foursquare_venue_count + stats.foursquare_tip_count) * (CASE WHEN COALESCE(hipcamp.activities, 0) > 0 THEN 10 ELSE 1 END) AS score',
' FROM stats,',
' hipcamp',
') AS score',
].join('\n');
var scoreOrderBy = 'score DESC,';
//
// Decide which kind of search this is
//
if (data._query.with && data._query.with.length && data._query.near && data._query.near.length) { //Has amenities & near filter
// With
//
// Split activities into an array
//
queryArray = sanitizer.sanitize(data._query.with).split('+');
//
// Limit the amount of activities which can be passed in
//
queryArray.length = Math.min(queryArray.length, 20);
//
// Build column aliases for JSON select
//
activitiesColumnSQLslug = queryArray.map(function(activity) {
return "activities->'"+activity+"' as "+activity;
}).join(',');
//
// Build where statement for JSON select
//
activitiesWhereSQLslug = queryArray.map(function(activity) {
return " act."+activity+"::text='true'";
}).join(' AND ');
// Near
var limit = ((data.options) ? data.options.limit : null) || 100000,
not = ((data.options) ? data.options.not : null) ? ' AND superunit_id <> ' + parseInt(data.options.not) : '';
return getPlace(data._query.near, function(err, place) {
if (err) {
return callback(err);
}
fullQuery = {
text: escape([
'SELECT',
' *,',
' ST_AsGeoJSON(geom) AS geometry,',
' ST_AsGeoJSON(ST_Centroid(geom)) AS centroid,',
' ST_Distance(geom, ST_SetSRID(ST_MakePoint($1, $2), 4326)) AS distance',
' %s',
'FROM (',
' SELECT',
' *',
' FROM cpad_superunits_4326',
' INNER JOIN (',
' SELECT',
' su_id,',
' activities,',
' %s',
' FROM site_hipcamp_activities',
' ) act',
' ON act.su_id = cpad_superunits_4326.superunit_id',
' WHERE ST_DWithin(geom, ST_SetSRID(ST_MakePoint($1, $2), 4326), .3)',
' %s',
' AND unit_name ILIKE %L',
' LIMIT $3',
') AS shortlist',
'ORDER BY %s distance ASC'
].join('\n'), scoreSubQuery, activitiesColumnSQLslug, not, '%' + dbQuery + '%', scoreOrderBy),
values: [place.coordinates[1], place.coordinates[0], limit]
};
return callback(null, fullQuery);
});
} else if (data._query.near && data._query.near.length) { //Has near filter
var limit = ((data.options) ? data.options.limit : null) || 100000,
not = ((data.options) ? data.options.not : null) ? ' AND superunit_id <> ' + parseInt(data.options.not) : '';
return getPlace(data._query.near, function(err, place) {
if (err) {
return callback(err);
}
fullQuery = {
text: escape([
'SELECT',
' *,',
' ST_AsGeoJSON(geom) AS geometry,',
' ST_AsGeoJSON(ST_Centroid(geom)) AS centroid,',
' ST_Distance(geom, ST_SetSRID(ST_MakePoint($1, $2), 4326)) AS distance',
' %s',
'FROM (',
' SELECT',
' *',
' FROM cpad_superunits_4326',
' WHERE ST_DWithin(geom, ST_SetSRID(ST_MakePoint($1, $2), 4326), .3)',
' %s',
' AND unit_name ILIKE %L',
' LIMIT $3',
') AS shortlist',
'ORDER BY %s distance ASC'
].join('\n'), scoreSubQuery, not, '%' + dbQuery + '%', scoreOrderBy),
values: [place.coordinates[1], place.coordinates[0], limit]
};
return callback(null, fullQuery);
});
} else if (data._query.with && data._query.with.length) { //Has ammenities filter
//
// Split activities into an array
//
queryArray = sanitizer.sanitize(data._query.with).split('+');
//
// Limit the amount of activities which can be passed in
//
queryArray.length = Math.min(queryArray.length, 20);
//
// Build column aliases for JSON select
//
activitiesColumnSQLslug = queryArray.map(function(activity) {
return "activities -> '" + activity + "' as " + activity;
}).join(',');
//
// Build where statement for JSON select
//
activitiesWhereSQLslug = queryArray.map(function(activity) {
return ' act.' + activity + "::text='true'";
}).join(' AND ');
fullQuery = {
text: escape([
'SELECT',
' shortlist.superunit_id,',
' *',
' %s',
'FROM (',
' SELECT',
' su_id,',
' activities,',
' %s',
' FROM site_hipcamp_activities',
') act',
'INNER JOIN cpad_superunits_4326 shortlist ON shortlist.superunit_id = act.su_id',
'WHERE %s',
' AND unit_name ILIKE %L',
'ORDER BY %s shortlist.unit_name ASC'
].join('\n'), scoreSubQuery, activitiesColumnSQLslug, activitiesWhereSQLslug, '%' + dbQuery + '%', scoreOrderBy)
};
return callback(null, fullQuery);
} else {
fullQuery = {
text: escape([
'SELECT',
' shortlist.superunit_id,',
' shortlist.unit_name as name,',
' act.activities,',
' *',
' %s',
'FROM cpad_superunits_4326 shortlist',
'INNER JOIN site_hipcamp_activities act ON (shortlist.superunit_id = act.su_id)',
'WHERE shortlist.unit_name ILIKE %L',
'ORDER BY %s shortlist.unit_name ASC'
].join('\n'), scoreSubQuery, '%' + dbQuery + '%', scoreOrderBy)
};
return callback(null, fullQuery);
}
}
module.exports = function(data, _callback) {
var dbLimit = '',
dbQuery = '', // default
searchQuery;
return pg.connect(env.require('DATABASE_URL'), function(err, client, done) {
var callback = function() {
done();
return _callback.apply(null, arguments);
};
if (data.limit) {
dbLimit = ' LIMIT ' + data.limit;
}
searchQuery = data.query || data._query.q;
if (searchQuery) {
dbQuery = sanitizer.sanitize(searchQuery.toLowerCase()).split('+').join(' ') || sanitizer.sanitize(dbQuery);
}
buildQuery(dbQuery, data, function(err, fullQuery) {
if (err) {
return callback(err);
}
return client.query(fullQuery, function(err, result) {
if(err) {
console.error('error running query', err);
return callback(err);
}
//
// Format activity data
//
result.rows.map(function(row) {
row.activity = {};
row.activity.items = hipcamp.filterActivityData(row.activities);
return row;
});
return callback(null, {
parks : result.rows,
title : sanitizer.sanitize(dbQuery)
});
});
});
});
};