From 24e54144c8df31d39e0e0abc07ccf35f3dc5fa3c Mon Sep 17 00:00:00 2001 From: Alex Loyko Date: Tue, 24 Oct 2017 13:17:27 -0400 Subject: [PATCH] query module support + some minor changes --- controller/intersections.js | 31 ++----------------- .../predicates/is_intersection_layer.js | 6 +++- query/search_intersections.js | 4 +-- routes/v1.js | 4 +-- 4 files changed, 11 insertions(+), 34 deletions(-) diff --git a/controller/intersections.js b/controller/intersections.js index ff61eb96..c7902569 100644 --- a/controller/intersections.js +++ b/controller/intersections.js @@ -1,33 +1,6 @@ const _ = require('lodash'); const iso3166 = require('iso3166-1'); -const util = require('../util/arrayHelper'); - -/* -this function returns an object that denotes an intersection of form: -{ - street1: value1, - street2: value2 -} -*/ -function parseIntersections(text) { - var str1 = '', str2 = ''; - if(text.trim().length > 1) { - var words = text.toLowerCase().split(' '); - // remove all the whitespaces - words = util.removeWhitespaceElements(words); - words = util.EWStreetsSanitizer(words); - words = util.addOrdinality(words); - // only treat input as intersection if contains '&' or 'and' - const delimiter = _.includes(text, '&') ? '&' : 'and'; - const delimiterIndex = words.indexOf(delimiter); - - str1 = util.wordsToSentence(words, 0, delimiterIndex); - str2 = util.wordsToSentence(words, delimiterIndex+1, words.length); - } else { - throw 'Missing streets in the intersection'; - } - return { street1: str1, street2: str2 }; -} +const intersectionsParser = require('../helper/intersectionsParsing'); function setup(should_execute) { function controller( req, res, next ){ @@ -38,7 +11,7 @@ function setup(should_execute) { // parse text with query parser //const parsed_text = text_analyzer.parse(req.clean.text); - const parsed_text = parseIntersections(req.clean.text); + const parsed_text = intersectionsParser(req.clean.text); if (parsed_text !== undefined) { // if a known ISO2 country was parsed, convert it to ISO3 diff --git a/controller/predicates/is_intersection_layer.js b/controller/predicates/is_intersection_layer.js index 9117f0c5..51b96b72 100644 --- a/controller/predicates/is_intersection_layer.js +++ b/controller/predicates/is_intersection_layer.js @@ -1,5 +1,9 @@ const _ = require('lodash'); module.exports = (request, response) => { - return _.includes(request.query.text, '&') || _.includes(request.query.text, ' and '); + if(request) { + return _.includes(request.query.text, '&') || _.includes(request.query.text, ' and '); + } + + return false; }; diff --git a/query/search_intersections.js b/query/search_intersections.js index 97966ac7..3f5a978a 100644 --- a/query/search_intersections.js +++ b/query/search_intersections.js @@ -195,8 +195,8 @@ function generateQuery(clean) { ] } } ] - } - } + } + } }; } diff --git a/routes/v1.js b/routes/v1.js index 078024ce..6dd4299b 100644 --- a/routes/v1.js +++ b/routes/v1.js @@ -132,7 +132,7 @@ function addRoutes(app, peliasConfig) { ); // defines whether to skip libpostal and control should be switched to intersection processing - const IntersectionParserShouldExecute = all ( + const intersectionParserShouldExecute = all ( isIntersectionLayer, not(hasRequestErrors) ); @@ -274,7 +274,7 @@ function addRoutes(app, peliasConfig) { sanitizers.search.middleware(peliasConfig.api), middleware.requestLanguage, middleware.calcSize(), - controllers.intersection(IntersectionParserShouldExecute), + controllers.intersection(intersectionParserShouldExecute), controllers.libpostal(libpostalShouldExecute), controllers.placeholder(placeholderService, geometricFiltersApply, placeholderGeodisambiguationShouldExecute), controllers.placeholder(placeholderService, geometricFiltersDontApply, placeholderIdsLookupShouldExecute),