diff --git a/query/search.js b/query/search.js index bed5a63c..3e2a591a 100644 --- a/query/search.js +++ b/query/search.js @@ -1,7 +1,8 @@ var peliasQuery = require('pelias-query'), defaults = require('./search_defaults'), textParser = require('./text_parser'), - check = require('check-types'); + check = require('check-types'), + _ = require('lodash'); //------------------------------ // general-purpose search query @@ -120,7 +121,7 @@ function generateQuery( clean ){ } function getQuery(vs) { - if (hasStreet(vs)) { + if (hasStreet(vs) || isCityStateOnly(vs)) { return { type: 'fallback', body: fallbackQuery.render(vs) @@ -137,4 +138,17 @@ function hasStreet(vs) { return vs.isset('input:street'); } +function isCityStateOnly(vs) { + var isSet = function(layer) { + return vs.isset('input:' + layer); + }; + + var allowedFields = ['locality', 'region']; + var disallowedFields = ['query', 'category', 'housenumber', 'street', + 'neighbourhood', 'borough', 'postcode', 'county', 'country']; + + return allowedFields.every(isSet) && !disallowedFields.some(isSet); + +} + module.exports = generateQuery; diff --git a/test/unit/query/search.js b/test/unit/query/search.js index 36fbd17d..eae7e40a 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -270,6 +270,64 @@ module.exports.tests.query = function(test, common) { t.deepEqual(compiled.body, expected, 'valid search query with category filtering'); t.end(); }); + +}; + +module.exports.tests.city_state = function(test, common) { + test('only city and state set should return query', function(t) { + var clean = { + parsed_text: { + 'city': 'city value', + 'state': 'state value' + } + }; + + var query = generate(clean); + + t.notEqual(query, undefined, 'should not have returned undefined'); + t.end(); + + }); + + test('city- OR state-only should return undefined', function(t) { + ['city', 'state'].forEach(function(placeType) { + var clean = { + parsed_text: {} + }; + + clean.parsed_text[placeType] = placeType + ' value'; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + + }); + + t.end(); + + }); + + test('city and state with at least one other input: field should return undefined', function(t) { + ['query', 'category', 'number', 'neighbourhood', 'borough', 'postalcode', 'county', 'country'].forEach(function(placeType) { + var clean = { + parsed_text: { + 'city': 'city value', + 'state': 'state value' + } + }; + + clean.parsed_text[placeType] = placeType + ' value'; + + var query = generate(clean); + + t.equals(query, undefined, 'should have returned undefined'); + + }); + + t.end(); + + }); + }; module.exports.all = function (tape, common) {