-
-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathv1.js
385 lines (350 loc) · 15.9 KB
/
v1.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
const _ = require('lodash');
const path = require('path');
const requireAll = require('require-all');
const express = require('express');
const { Router } = express;
const sorting = require('pelias-sorting');
const elasticsearch = require('elasticsearch');
const {all, any, not} = require('predicates');
// imports
const sanitizers = requireAll(path.join(__dirname, '../sanitizer'));
const middleware = requireAll(path.join(__dirname, '../middleware'));
const controllers = requireAll(path.join(__dirname, '../controller'));
const queries = requireAll(path.join(__dirname, '../query'));
// predicates that drive whether controller/search runs
const predicates = requireAll(path.join(__dirname, '../controller/predicates'));
predicates.hasRequestCategories = predicates.hasRequestParameter('categories');
// shorthand for standard early-exit conditions
const hasResponseDataOrRequestErrors = any(predicates.hasResponseData, predicates.hasRequestErrors);
predicates.hasAdminOnlyResults = not(predicates.hasResultsAtLayers(['venue', 'address', 'street']));
const serviceWrapper = require('pelias-microservice-wrapper').service;
const configuration = requireAll(path.join(__dirname, '../service/configurations'));
/**
* Append routes to app
*
* @param {object} app
* @param {object} peliasConfig
*/
function addRoutes(app, peliasConfig) {
const esclient = elasticsearch.Client(_.extend({}, peliasConfig.esclient));
const pipConfiguration = new configuration.PointInPolygon(_.defaultTo(peliasConfig.api.services.pip, {}));
const pipService = serviceWrapper(pipConfiguration);
const isPipServiceEnabled = _.constant(pipConfiguration.isEnabled());
const placeholderConfiguration = new configuration.PlaceHolder(_.defaultTo(peliasConfig.api.services.placeholder, {}));
const placeholderService = serviceWrapper(placeholderConfiguration);
const isPlaceholderServiceEnabled = _.constant(placeholderConfiguration.isEnabled());
const changeLanguageConfiguration = new configuration.Language(_.defaultTo(peliasConfig.api.services.placeholder, {}));
const changeLanguageService = serviceWrapper(changeLanguageConfiguration);
const isChangeLanguageEnabled = _.constant(changeLanguageConfiguration.isEnabled());
const interpolationConfiguration = new configuration.Interpolation(_.defaultTo(peliasConfig.api.services.interpolation, {}));
const interpolationService = serviceWrapper(interpolationConfiguration);
const isInterpolationEnabled = _.constant(interpolationConfiguration.isEnabled());
// standard libpostal should use req.clean.text for the `address` parameter
const libpostalConfiguration = new configuration.Libpostal(
_.defaultTo(peliasConfig.api.services.libpostal, {}),
_.property('clean.text'));
const libpostalService = serviceWrapper(libpostalConfiguration);
// structured libpostal should use req.clean.parsed_text.address for the `address` parameter
const structuredLibpostalConfiguration = new configuration.Libpostal(
_.defaultTo(peliasConfig.api.services.libpostal, {}),
_.property('clean.parsed_text.address'));
const structuredLibpostalService = serviceWrapper(structuredLibpostalConfiguration);
// fallback to coarse reverse when regular reverse didn't return anything
const coarseReverseShouldExecute = all(
isPipServiceEnabled, not(predicates.hasRequestErrors), not(predicates.hasResponseData), not(predicates.isOnlyNonAdminLayers)
);
const libpostalShouldExecute = all(
not(predicates.hasRequestErrors),
not(predicates.isRequestSourcesOnlyWhosOnFirst)
);
// for libpostal to execute for structured requests, req.clean.parsed_text.address must exist
const structuredLibpostalShouldExecute = all(
not(predicates.hasRequestErrors),
predicates.hasParsedTextProperties.all('address')
);
// execute placeholder if libpostal only parsed as admin-only and needs to
// be geodisambiguated
const placeholderGeodisambiguationShouldExecute = all(
not(hasResponseDataOrRequestErrors),
isPlaceholderServiceEnabled,
// check request.clean for several conditions first
not(
any(
// layers only contains venue, address, or street
predicates.isOnlyNonAdminLayers,
// don't geodisambiguate if categories were requested
predicates.hasRequestCategories
)
),
any(
predicates.isRequestSourcesOnlyWhosOnFirst,
all(
predicates.isAdminOnlyAnalysis,
any(
predicates.isRequestSourcesUndefined,
predicates.isRequestSourcesIncludesWhosOnFirst
)
)
)
);
// execute placeholder if libpostal identified address parts but ids need to
// be looked up for admin parts
const placeholderIdsLookupShouldExecute = all(
not(hasResponseDataOrRequestErrors),
isPlaceholderServiceEnabled,
predicates.isRequestLayersAnyAddressRelated,
// check clean.parsed_text for several conditions that must all be true
all(
// run placeholder if clean.parsed_text has 'street'
predicates.hasParsedTextProperties.any('street'),
// don't run placeholder if there's a query or category
not(predicates.hasParsedTextProperties.any('query', 'category')),
// run placeholder if there are any adminareas identified
predicates.hasParsedTextProperties.any('neighbourhood', 'borough', 'city', 'county', 'state', 'country')
)
);
const searchWithIdsShouldExecute = all(
not(predicates.hasRequestErrors),
// don't search-with-ids if there's a query or category
not(predicates.hasParsedTextProperties.any('query', 'category')),
// at least one layer allowed by the query params must be related to addresses
predicates.isRequestLayersAnyAddressRelated,
// there must be a street
predicates.hasParsedTextProperties.any('street')
);
// placeholder should have executed, useful for determining whether to actually
// fallback or not (don't fallback to old search if the placeholder response
// should be honored as is)
const placeholderShouldHaveExecuted = any(
placeholderGeodisambiguationShouldExecute,
placeholderIdsLookupShouldExecute
);
// don't execute the cascading fallback query IF placeholder should have executed
// that way, if placeholder didn't return anything, don't try to find more things the old way
const fallbackQueryShouldExecute = all(
not(predicates.hasRequestErrors),
not(predicates.hasResponseData),
not(placeholderShouldHaveExecuted)
);
const shouldDeferToPeliasParser = any(
// we always want to try pelias parser based queries if there are no results
not(predicates.hasResponseData),
all(
// if there are only admin results, but parse contains more granular items than admin,
// then we want to defer to pelias parser based queries
predicates.hasAdminOnlyResults,
not(predicates.isAdminOnlyAnalysis),
// exception: if the 'sources' parameter is only wof, do not use pelias parser
// in that case Placeholder can return all the possible answers
not(predicates.isRequestSourcesOnlyWhosOnFirst),
)
);
// call search_pelias_parser query if pelias_parser was the parser
const searchPeliasParserShouldExecute = all(
not(predicates.hasRequestErrors),
predicates.isPeliasParse
);
// get language adjustments if:
// - there's a response
// - theres's a lang parameter in req.clean
const changeLanguageShouldExecute = all(
predicates.hasResponseData,
not(predicates.hasRequestErrors),
isChangeLanguageEnabled,
predicates.hasRequestParameter('lang')
);
// interpolate if:
// - there's a number and street
// - there are street-layer results (these are results that need to be interpolated)
const interpolationShouldExecute = all(
not(predicates.hasRequestErrors),
isInterpolationEnabled,
predicates.hasParsedTextProperties.all('housenumber', 'street'),
predicates.hasResultsAtLayers('street')
);
// execute under the following conditions:
// - there are no errors or data
// - request is not coarse OR pip service is disabled
const nonCoarseReverseShouldExecute = all(
not(hasResponseDataOrRequestErrors),
any(
not(predicates.isCoarseReverse),
not(isPipServiceEnabled)
)
);
// helpers to replace vague booleans
const geometricFiltersApply = true;
var base = '/v1/';
/** ------------------------- routers ------------------------- **/
var routers = {
index: createRouter([
controllers.markdownToHtml(peliasConfig.api, path.join(__dirname, '../public/apiDoc.md'))
]),
attribution: createRouter([
controllers.markdownToHtml(peliasConfig.api, path.join(__dirname, '../public/attribution.md'))
]),
search: createRouter([
sanitizers.search.middleware(peliasConfig.api),
middleware.requestLanguage,
middleware.sizeCalculator(),
controllers.libpostal(libpostalService, libpostalShouldExecute),
controllers.placeholder(placeholderService, geometricFiltersApply, placeholderGeodisambiguationShouldExecute),
controllers.placeholder(placeholderService, geometricFiltersApply, placeholderIdsLookupShouldExecute),
// try 3 different query types: address search using ids, cascading fallback, pelias parser
controllers.search(peliasConfig, esclient, queries.address_search_using_ids, searchWithIdsShouldExecute),
controllers.search(peliasConfig, esclient, queries.search, fallbackQueryShouldExecute),
sanitizers.defer_to_pelias_parser(peliasConfig.api, shouldDeferToPeliasParser), //run additional sanitizers needed for pelias parser
controllers.search(peliasConfig, esclient, queries.search_pelias_parser, searchPeliasParserShouldExecute),
middleware.trimByGranularity(),
middleware.distance('focus.point.'),
middleware.confidenceScore(peliasConfig.api),
middleware.confidenceScoreFallback(),
middleware.interpolate(interpolationService, interpolationShouldExecute, interpolationConfiguration),
middleware.sortResponseData(sorting, predicates.hasAdminOnlyResults),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
middleware.renamePlacenames(),
middleware.parseBBox(),
middleware.normalizeParentIds(),
middleware.changeLanguage(changeLanguageService, changeLanguageShouldExecute),
middleware.assignLabels(),
middleware.geocodeJSON(peliasConfig.api, base),
middleware.sendJSON
]),
structured: createRouter([
sanitizers.structured_geocoding.middleware(peliasConfig.api),
middleware.requestLanguage,
middleware.sizeCalculator(),
controllers.structured_libpostal(structuredLibpostalService, structuredLibpostalShouldExecute),
controllers.search(peliasConfig, esclient, queries.structured_geocoding, not(hasResponseDataOrRequestErrors)),
middleware.trimByGranularityStructured(),
middleware.distance('focus.point.'),
middleware.confidenceScore(peliasConfig.api),
middleware.confidenceScoreFallback(),
middleware.interpolate(interpolationService, interpolationShouldExecute, interpolationConfiguration),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
middleware.renamePlacenames(),
middleware.parseBBox(),
middleware.normalizeParentIds(),
middleware.changeLanguage(changeLanguageService, changeLanguageShouldExecute),
middleware.assignLabels(),
middleware.geocodeJSON(peliasConfig.api, base),
middleware.sendJSON
]),
autocomplete: createRouter([
sanitizers.autocomplete.middleware(peliasConfig.api),
middleware.requestLanguage,
middleware.sizeCalculator(),
controllers.search(peliasConfig, esclient, queries.autocomplete, not(hasResponseDataOrRequestErrors)),
middleware.distance('focus.point.'),
middleware.confidenceScore(peliasConfig.api),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
middleware.renamePlacenames(),
middleware.parseBBox(),
middleware.normalizeParentIds(),
middleware.changeLanguage(changeLanguageService, changeLanguageShouldExecute),
middleware.assignLabels(),
middleware.geocodeJSON(peliasConfig.api, base),
middleware.sendJSON
]),
reverse: createRouter([
sanitizers.reverse.middleware(peliasConfig.api),
middleware.requestLanguage,
middleware.sizeCalculator(2),
controllers.search(peliasConfig, esclient, queries.reverse, nonCoarseReverseShouldExecute),
controllers.coarse_reverse(pipService, coarseReverseShouldExecute),
middleware.distance('point.'),
// reverse confidence scoring depends on distance from origin
// so it must be calculated first
middleware.confidenceScoreReverse(),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
middleware.renamePlacenames(),
middleware.parseBBox(),
middleware.normalizeParentIds(),
middleware.changeLanguage(changeLanguageService, changeLanguageShouldExecute),
middleware.assignLabels(),
middleware.geocodeJSON(peliasConfig.api, base),
middleware.sendJSON
]),
nearby: createRouter([
sanitizers.nearby.middleware(peliasConfig.api),
middleware.requestLanguage,
middleware.sizeCalculator(),
controllers.search(peliasConfig, esclient, queries.reverse, not(hasResponseDataOrRequestErrors)),
middleware.distance('point.'),
// reverse confidence scoring depends on distance from origin
// so it must be calculated first
middleware.confidenceScoreReverse(),
middleware.applyOverrides(),
middleware.dedupe(),
middleware.accuracy(),
middleware.localNamingConventions(),
middleware.renamePlacenames(),
middleware.parseBBox(),
middleware.normalizeParentIds(),
middleware.changeLanguage(changeLanguageService, changeLanguageShouldExecute),
middleware.assignLabels(),
middleware.geocodeJSON(peliasConfig.api, base),
middleware.sendJSON
]),
place: createRouter([
sanitizers.place.middleware(peliasConfig.api),
middleware.requestLanguage,
controllers.place(peliasConfig.api, esclient),
middleware.expandDocument(peliasConfig.api, esclient),
middleware.accuracy(),
middleware.localNamingConventions(),
middleware.renamePlacenames(),
middleware.parseBBox(),
middleware.normalizeParentIds(),
middleware.changeLanguage(changeLanguageService, changeLanguageShouldExecute),
middleware.assignLabels(),
middleware.geocodeJSON(peliasConfig.api, base),
middleware.sendJSON
]),
status: createRouter([
controllers.status
])
};
// static data endpoints
app.get ( base, routers.index );
app.get ( base + 'attribution', routers.attribution );
app.get ( '/attribution', routers.attribution );
app.get ( '/status', routers.status );
// backend dependent endpoints
app.get ( base + 'place', routers.place );
app.get ( base + 'autocomplete', routers.autocomplete );
app.get ( base + 'search', routers.search );
app.post( base + 'search', routers.search );
app.get ( base + 'search/structured', routers.structured );
app.get ( base + 'reverse', routers.reverse );
app.get ( base + 'nearby', routers.nearby );
if (peliasConfig.api.exposeInternalDebugTools) {
app.use ( '/frontend', express.static('node_modules/pelias-compare/dist-api/'));
}
}
/**
* Helper function for creating routers
*
* @param {[{function}]} functions
* @returns {express.Router}
*/
function createRouter(functions) {
var router = Router(); // jshint ignore:line
functions.forEach(function (f) {
router.use(f);
});
return router;
}
module.exports.addRoutes = addRoutes;