From 84a9b3e20ed07fa5fa151fea05c0091928dbd205 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 17 Feb 2015 19:24:11 -0500 Subject: [PATCH 1/7] splitting on comma (trying to find/assume admin values) --- sanitiser/_input.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sanitiser/_input.js b/sanitiser/_input.js index 436c0c73..f1626179 100644 --- a/sanitiser/_input.js +++ b/sanitiser/_input.js @@ -3,6 +3,7 @@ function sanitize( req ){ req.clean = req.clean || {}; var params= req.query; + var delim = ','; // ensure the input params are a valid object if( Object.prototype.toString.call( params ) !== '[object Object]' ){ @@ -16,8 +17,17 @@ function sanitize( req ){ 'message': 'invalid param \'input\': text length, must be >0' }; } + req.clean.input = params.input; + // for admin matching during query time + // split 'flatiron, new york, ny' into 'flatiron' and ' new york, ny' + var delim_index = params.input.indexOf(delim); + if ( delim_index !== -1 ) { + req.clean.input = params.input.substring(0, delim_index); + req.clean.input_admin = params.input.substring(delim_index +1); + } + return { 'error': false }; } From 2127ebe258f2ab48f0d3ede84c4fb7731690d6bc Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 17 Feb 2015 19:25:09 -0500 Subject: [PATCH 2/7] switching to bool with a 'must' match (AND) and a few 'should's (OR) --- query/search.js | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/query/search.js b/query/search.js index 9d9916d3..7c52fcfa 100644 --- a/query/search.js +++ b/query/search.js @@ -22,12 +22,28 @@ function generate( params ){ // add search condition to distance query query.query.filtered.query = { - query_string : { - query: params.input, - fields: ['name.default'], - default_operator: 'OR' + 'bool': { + 'must': [{ + 'match': { + 'name.default': params.input + } + } + ] } }; + + if (params.input_admin) { + var admin_fields = ['admin0', 'admin1', 'alpha3']; + query.query.filtered.query.bool.should = []; + + admin_fields.forEach(function(admin_field) { + var match = {}; + match[admin_field] = params.input_admin; + query.query.filtered.query.bool.should.push({ + 'match': match + }); + }); + } query.sort = query.sort.concat(sort); From 9013b4484eeeda2e3550a9c6a7a5dec4388fa25e Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 17 Feb 2015 19:25:57 -0500 Subject: [PATCH 3/7] fixing tests --- test/unit/query/search.js | 52 +++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/test/unit/query/search.js b/test/unit/query/search.js index 2d99d0ff..4e4e59d0 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -61,12 +61,13 @@ module.exports.tests.query = function(test, common) { 'query': { 'filtered': { 'query': { - 'query_string': { - 'query': 'test', - 'fields': [ - 'name.default' - ], - 'default_operator': 'OR' + 'bool': { + 'must': [{ + 'match': { + 'name.default': 'test' + } + } + ] } }, 'filter': { @@ -113,12 +114,13 @@ module.exports.tests.query = function(test, common) { 'query': { 'filtered': { 'query': { - 'query_string': { - 'query': 'test', - 'fields': [ - 'name.default' - ], - 'default_operator': 'OR' + 'bool': { + 'must': [{ + 'match': { + 'name.default': 'test' + } + } + ] } }, 'filter': { @@ -159,12 +161,13 @@ module.exports.tests.query = function(test, common) { 'query': { 'filtered': { 'query': { - 'query_string': { - 'query': 'test', - 'fields': [ - 'name.default' - ], - 'default_operator': 'OR' + 'bool': { + 'must': [{ + 'match': { + 'name.default': 'test' + } + } + ] } }, 'filter': { @@ -194,12 +197,13 @@ module.exports.tests.query = function(test, common) { 'query': { 'filtered': { 'query': { - 'query_string': { - 'query': 'test', - 'fields': [ - 'name.default' - ], - 'default_operator': 'OR' + 'bool': { + 'must': [{ + 'match': { + 'name.default': 'test' + } + } + ] } }, 'filter': { From a427c058f7344cd221e2eb8a81bee764a4593f8e Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Wed, 18 Feb 2015 02:21:29 -0500 Subject: [PATCH 4/7] adding admin1_abbr and admin2 to the admin_fields array --- query/search.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/query/search.js b/query/search.js index 7c52fcfa..65551cde 100644 --- a/query/search.js +++ b/query/search.js @@ -33,7 +33,7 @@ function generate( params ){ }; if (params.input_admin) { - var admin_fields = ['admin0', 'admin1', 'alpha3']; + var admin_fields = ['admin0', 'admin1', 'admin1_abbr', 'admin2', 'alpha3']; query.query.filtered.query.bool.should = []; admin_fields.forEach(function(admin_field) { From a003b7b9bfe4f07b4342dd1181d566c31384bcc1 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 24 Feb 2015 17:23:59 -0500 Subject: [PATCH 5/7] strip out leading and trailing whitespace if any --- sanitiser/_input.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanitiser/_input.js b/sanitiser/_input.js index f1626179..22b150b5 100644 --- a/sanitiser/_input.js +++ b/sanitiser/_input.js @@ -25,7 +25,7 @@ function sanitize( req ){ var delim_index = params.input.indexOf(delim); if ( delim_index !== -1 ) { req.clean.input = params.input.substring(0, delim_index); - req.clean.input_admin = params.input.substring(delim_index +1); + req.clean.input_admin = params.input.substring(delim_index + 1).trim(); } return { 'error': false }; From d2d76be8faa9993024d201f9fab5965e0e153a82 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 24 Feb 2015 18:23:47 -0500 Subject: [PATCH 6/7] changing a commented line to reflect reality --- sanitiser/_input.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sanitiser/_input.js b/sanitiser/_input.js index 22b150b5..b1de5e63 100644 --- a/sanitiser/_input.js +++ b/sanitiser/_input.js @@ -21,7 +21,7 @@ function sanitize( req ){ req.clean.input = params.input; // for admin matching during query time - // split 'flatiron, new york, ny' into 'flatiron' and ' new york, ny' + // split 'flatiron, new york, ny' into 'flatiron' and 'new york, ny' var delim_index = params.input.indexOf(delim); if ( delim_index !== -1 ) { req.clean.input = params.input.substring(0, delim_index); From 5f30559680c11f3f052fd51815e7d608bc5831c7 Mon Sep 17 00:00:00 2001 From: Harish Krishna Date: Tue, 24 Feb 2015 18:24:21 -0500 Subject: [PATCH 7/7] testing inputs with comma --- test/unit/sanitiser/suggest.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/unit/sanitiser/suggest.js b/test/unit/sanitiser/suggest.js index 6ea903e5..a576f6de 100644 --- a/test/unit/sanitiser/suggest.js +++ b/test/unit/sanitiser/suggest.js @@ -2,6 +2,7 @@ var suggest = require('../../../sanitiser/suggest'), _sanitize = suggest.sanitize, middleware = suggest.middleware, + delim = ',', defaultError = 'invalid param \'input\': text length, must be >0', defaultClean = { input: 'test', lat:0, @@ -54,6 +55,29 @@ module.exports.tests.sanitize_input = function(test, common) { }); }; +module.exports.tests.sanitize_input_with_delim = function(test, common) { + var inputs = [ 'a,bcd', '123 main st, admin1', ',,,', ' ' ]; + + test('valid inputs with a comma', function(t) { + inputs.forEach( function( input ){ + sanitize({ input: input, lat: 0, lon: 0 }, function( err, clean ){ + var expected = JSON.parse(JSON.stringify( defaultClean )); + expected.input = input; + + var delim_index = input.indexOf(delim); + if (delim_index!==-1) { + expected.input = input.substring(0, input.indexOf(delim)); + expected.input_admin = input.substring(delim_index + 1).trim(); + } + + t.equal(err, undefined, 'no error'); + t.deepEqual(clean, expected, 'clean set correctly (' + input + ')'); + }); + }); + t.end(); + }); +}; + module.exports.tests.sanitize_lat = function(test, common) { var lats = { invalid: [ -181, -120, -91, 91, 120, 181 ],