From 13635a2c16f3b2e90c363be40759e07ad099491b Mon Sep 17 00:00:00 2001 From: Stephen Hess Date: Mon, 14 Sep 2015 17:20:10 -0400 Subject: [PATCH] added boundary.country to search and reverse queries (with test fixtures) --- query/reverse.js | 10 ++++ query/search.js | 1 - sanitiser/reverse.js | 4 +- test/unit/fixture/reverse_standard.js | 6 ++- .../fixture/reverse_with_boundary_country.js | 54 +++++++++++++++++++ test/unit/fixture/search_boundary_country.js | 29 +++++----- test/unit/query/reverse.js | 14 +++++ test/unit/sanitiser/reverse.js | 9 ++-- 8 files changed, 104 insertions(+), 23 deletions(-) create mode 100644 test/unit/fixture/reverse_with_boundary_country.js diff --git a/query/reverse.js b/query/reverse.js index 1a2332bb..f91561aa 100644 --- a/query/reverse.js +++ b/query/reverse.js @@ -7,6 +7,9 @@ var peliasQuery = require('pelias-query'), //------------------------------ var query = new peliasQuery.layout.FilteredBooleanQuery(); +// mandatory matches +query.score( peliasQuery.view.boundary_country, 'must' ); + // scoring boost query.sort( peliasQuery.view.sort_distance ); @@ -49,6 +52,13 @@ function generateQuery( clean ){ }); } + // boundary country + if( clean.boundary && clean.boundary.country ){ + vs.set({ + 'boundary:country': clean.boundary.country + }); + } + return query.render( vs ); } diff --git a/query/search.js b/query/search.js index 977e7c1f..bc88516f 100644 --- a/query/search.js +++ b/query/search.js @@ -98,7 +98,6 @@ function generateQuery( clean ){ } // boundary country - // @todo: change these to the correct request variable names if( clean.boundary && clean.boundary.country ){ vs.set({ 'boundary:country': clean.boundary.country diff --git a/sanitiser/reverse.js b/sanitiser/reverse.js index ec86e289..da6bd558 100644 --- a/sanitiser/reverse.js +++ b/sanitiser/reverse.js @@ -6,7 +6,8 @@ var sanitizeAll = require('../sanitiser/sanitizeAll'), size: require('../sanitiser/_size'), details: require('../sanitiser/_details'), geo_reverse: require('../sanitiser/_geo_reverse'), - categories: require('../sanitiser/_categories') + categories: require('../sanitiser/_categories'), + boundary_country: require('../sanitiser/_boundary_country'), }; var sanitize = function(req, cb) { sanitizeAll(req, sanitizers, cb); }; @@ -25,4 +26,3 @@ module.exports.middleware = function( req, res, next ){ next(); }); }; - diff --git a/test/unit/fixture/reverse_standard.js b/test/unit/fixture/reverse_standard.js index b7cc058f..08a005e6 100644 --- a/test/unit/fixture/reverse_standard.js +++ b/test/unit/fixture/reverse_standard.js @@ -3,7 +3,9 @@ module.exports = { 'query': { 'filtered': { 'query': { - 'bool': {} + 'bool': { + 'must': [] + } }, 'filter': { 'bool': { @@ -40,4 +42,4 @@ module.exports = { ], 'size': 1, 'track_scores': true -}; \ No newline at end of file +}; diff --git a/test/unit/fixture/reverse_with_boundary_country.js b/test/unit/fixture/reverse_with_boundary_country.js new file mode 100644 index 00000000..f29569b2 --- /dev/null +++ b/test/unit/fixture/reverse_with_boundary_country.js @@ -0,0 +1,54 @@ + +module.exports = { + 'query': { + 'filtered': { + 'query': { + 'bool': { + 'must': [ + { + 'match': { + 'alpha3': { + 'analyzer': 'standard', + 'query': 'ABC' + } + } + } + ] + } + }, + 'filter': { + 'bool': { + 'must': [ + { + 'geo_distance': { + 'distance': '500km', + 'distance_type': 'plane', + 'optimize_bbox': 'indexed', + '_cache': true, + 'center_point': { + 'lat': 29.49136, + 'lon': -82.50622 + } + } + } + ] + } + } + } + }, + 'sort': [ + '_score', + { + '_geo_distance': { + 'center_point': { + 'lat': 29.49136, + 'lon': -82.50622 + }, + 'order': 'asc', + 'distance_type': 'plane' + } + } + ], + 'size': 1, + 'track_scores': true +}; diff --git a/test/unit/fixture/search_boundary_country.js b/test/unit/fixture/search_boundary_country.js index 94122797..f4a873bd 100644 --- a/test/unit/fixture/search_boundary_country.js +++ b/test/unit/fixture/search_boundary_country.js @@ -5,23 +5,24 @@ module.exports = { 'query': { 'bool': { 'must': [ - { - 'match': { - 'alpha3': { - 'analyzer': 'standard', - 'query': 'ABC' + { + 'match': { + 'alpha3': { + 'analyzer': 'standard', + 'query': 'ABC' + } } - } - }, - { - 'match': { - 'name.default': { - 'query': 'test', - 'boost': 1, - 'analyzer': 'peliasOneEdgeGram' + }, + { + 'match': { + 'name.default': { + 'query': 'test', + 'boost': 1, + 'analyzer': 'peliasOneEdgeGram' + } } } - }], + ], 'should': [{ 'match': { 'phrase.default': { diff --git a/test/unit/query/reverse.js b/test/unit/query/reverse.js index 521a4ffe..0d37fd1c 100644 --- a/test/unit/query/reverse.js +++ b/test/unit/query/reverse.js @@ -60,6 +60,20 @@ module.exports.tests.query = function(test, common) { }); t.end(); }); + + test('valid boundary.country reverse search', function(t) { + var query = generate({ + lat: 29.49136, lon: -82.50622, + boundary: { country: 'ABC' } + }); + + var compiled = JSON.parse( JSON.stringify( query ) ); + var expected = require('../fixture/reverse_with_boundary_country'); + + t.deepEqual(compiled, expected, 'valid reverse query with boundary.country'); + t.end(); + }); + }; module.exports.all = function (tape, common) { diff --git a/test/unit/sanitiser/reverse.js b/test/unit/sanitiser/reverse.js index 5c2a3a9f..1529c1e1 100644 --- a/test/unit/sanitiser/reverse.js +++ b/test/unit/sanitiser/reverse.js @@ -9,7 +9,8 @@ var reverse = require('../../../sanitiser/reverse'), lon: 0, size: 10, details: true, - categories: [] + categories: [], + boundary: { country: undefined } }, sanitize = function(query, cb) { _sanitize({'query':query}, cb); }; @@ -30,7 +31,7 @@ module.exports.tests.interface = function(test, common) { module.exports.tests.sanitisers = function(test, common) { test('check sanitiser list', function (t) { - var expected = ['layers', 'sources', 'size', 'details', 'geo_reverse', 'categories']; + var expected = ['layers', 'sources', 'size', 'details', 'geo_reverse', 'categories', 'boundary_country']; t.deepEqual(Object.keys(reverse.sanitiser_list), expected); t.end(); }); @@ -57,7 +58,7 @@ module.exports.tests.sanitize_lat = function(test, common) { var expected = JSON.parse(JSON.stringify( defaultClean )); expected.lat = parseFloat( lat ); t.equal(err, undefined, 'no error'); - t.deepEqual(clean, expected, 'clean set correctly (' + lat + ')'); + t.equal(clean.lat, parseFloat(lat), 'clean set correctly (' + lat + ')'); }); }); t.end(); @@ -84,7 +85,7 @@ module.exports.tests.sanitize_lon = function(test, common) { var expected = JSON.parse(JSON.stringify( defaultClean )); expected.lon = parseFloat( lon ); t.equal(err, undefined, 'no error'); - t.deepEqual(clean, expected, 'clean set correctly (' + lon + ')'); + t.equal(clean.lon, parseFloat(lon), 'clean set correctly (' + lon + ')'); }); }); t.end();