mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
128 lines
3.8 KiB
128 lines
3.8 KiB
8 years ago
|
const _ = require('lodash');
|
||
8 years ago
|
|
||
|
// This middleware component trims the results array by granularity when
|
||
|
// FallbackQuery was used. FallbackQuery is used for inputs like
|
||
|
// `1090 N Charlotte St, Lancaster, PA` where the address may not exist and
|
||
|
// we must fall back to trying `Lancaster, PA`. If the address does exist then
|
||
|
// FallbackQuery will return results for:
|
||
|
// - address+city+state
|
||
|
// - city+state
|
||
|
// - state
|
||
|
//
|
||
|
// Because the address matched, we're not interested in city+state or state, so
|
||
|
// this component removes results that aren't the most granular.
|
||
|
|
||
|
// layers in increasing order of granularity
|
||
8 years ago
|
// some places where this list differs in order from trimByGranularity:
|
||
|
// - because house number and street are in a single field, street hits must be considered
|
||
|
// more important than addresses due to how ES matches
|
||
|
// - country outranks dependency, this was done to ensure that "country=United States" doesn't
|
||
|
// bump up US dependencies containing "United States" above the country
|
||
8 years ago
|
// - retain both borough and locality results if both exist for when city=Manhattan is
|
||
|
// supplied we want to retain borough=Manhattan and city=Manhattan results
|
||
8 years ago
|
const layers = [
|
||
8 years ago
|
'venue',
|
||
|
'address',
|
||
8 years ago
|
'street',
|
||
8 years ago
|
'neighbourhood',
|
||
|
['borough', 'locality'],
|
||
|
'localadmin',
|
||
|
'county',
|
||
|
'macrocounty',
|
||
|
'region',
|
||
|
'macroregion',
|
||
|
'country',
|
||
|
'dependency'
|
||
|
];
|
||
|
|
||
|
// these layers are strictly used to drive one special case:
|
||
|
// - when there was a borough explicitly supplied
|
||
|
// for example, if the user passed borough=manhattan and city=new york
|
||
|
// then we want to preserve just boroughs if they're most granular and throw away
|
||
|
// city results. In the usual case where no borough is passed, the city value
|
||
|
// is looked up as a borough in the off chance that the user passed
|
||
|
// city=Manhattan
|
||
|
const explicit_borough_layers = [
|
||
8 years ago
|
'venue',
|
||
|
'address',
|
||
8 years ago
|
'street',
|
||
8 years ago
|
'neighbourhood',
|
||
|
'borough',
|
||
|
'locality',
|
||
|
'localadmin',
|
||
|
'county',
|
||
|
'macrocounty',
|
||
|
'region',
|
||
|
'macroregion',
|
||
8 years ago
|
'country',
|
||
|
'dependency'
|
||
8 years ago
|
];
|
||
|
|
||
|
// this helper method returns `true` if every result has a matched_query
|
||
|
// starting with `fallback.`
|
||
|
function isFallbackQuery(results) {
|
||
|
return results.every(function(result) {
|
||
|
return result.hasOwnProperty('_matched_queries') &&
|
||
|
!_.isEmpty(result._matched_queries) &&
|
||
|
_.startsWith(result._matched_queries[0], 'fallback.');
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function hasRecordsAtLayers(results, layer) {
|
||
8 years ago
|
return results.some( (result) => {
|
||
8 years ago
|
if (_.isArray(layer)) {
|
||
|
return layer.some( (sublayer) => {
|
||
|
return result._matched_queries[0] === 'fallback.' + sublayer;
|
||
|
});
|
||
|
} else {
|
||
|
return result._matched_queries[0] === 'fallback.' + layer;
|
||
|
}
|
||
|
|
||
8 years ago
|
});
|
||
|
}
|
||
|
|
||
|
function retainRecordsAtLayers(results, layer) {
|
||
8 years ago
|
return results.filter( (result) => {
|
||
8 years ago
|
if (_.isArray(layer)) {
|
||
|
return layer.some( (sublayer) => {
|
||
|
return result._matched_queries[0] === 'fallback.' + sublayer;
|
||
|
});
|
||
|
}
|
||
|
else {
|
||
|
return result._matched_queries[0] === 'fallback.' + layer;
|
||
|
}
|
||
|
|
||
8 years ago
|
});
|
||
|
}
|
||
|
|
||
8 years ago
|
function getLayers(parsed_text) {
|
||
|
if (parsed_text && parsed_text.hasOwnProperty('borough')) {
|
||
|
return explicit_borough_layers;
|
||
|
}
|
||
|
return layers;
|
||
|
}
|
||
|
|
||
8 years ago
|
function setup() {
|
||
|
return function trim(req, res, next) {
|
||
|
// don't do anything if there are no results or there are non-fallback.* named queries
|
||
|
// there should never be a mixture of fallback.* and non-fallback.* named queries
|
||
|
if (_.isUndefined(res.data) || !isFallbackQuery(res.data)) {
|
||
|
return next();
|
||
|
}
|
||
|
|
||
8 years ago
|
const layers = getLayers(req.clean.parsed_text);
|
||
|
|
||
8 years ago
|
// start at the most granular possible layer. if there are results at a layer
|
||
|
// then remove everything not at that layer.
|
||
8 years ago
|
layers.forEach( (layer) => {
|
||
8 years ago
|
if (hasRecordsAtLayers(res.data, layer )) {
|
||
|
res.data = retainRecordsAtLayers(res.data, layer);
|
||
|
}
|
||
|
});
|
||
|
|
||
|
next();
|
||
|
};
|
||
|
}
|
||
|
|
||
|
module.exports = setup;
|