diff --git a/query/search.js b/query/search.js index a13c156f..0e9a6b74 100644 --- a/query/search.js +++ b/query/search.js @@ -32,14 +32,6 @@ function generate( params ){ // should query contitions query.query.filtered.query.bool.should = []; - // add shingles should query - // note: this is required for partial phrase matching - query.query.filtered.query.bool.should.push({ - 'match': { - 'shingle.default': params.input - } - }); - if (params.input_admin) { var admin_fields = ['admin0', 'admin1', 'admin1_abbr', 'admin2', 'alpha3']; @@ -52,6 +44,14 @@ function generate( params ){ }); } + // add phrase matching query + // note: this is required for shingle/phrase matching + query.query.filtered.query.bool.should.push({ + 'match': { + 'phrase.default': params.input + } + }); + query.sort = query.sort.concat( sort( params ) ); return query; diff --git a/sanitiser/_details.js b/sanitiser/_details.js index 2b568dd9..6dc2b119 100644 --- a/sanitiser/_details.js +++ b/sanitiser/_details.js @@ -18,13 +18,7 @@ function sanitize( req, default_value ){ } if (params.details !== undefined) { - var details = params.details; - - if (typeof params.details === 'string') { - details = params.details === 'true'; - } - - clean.details = details === true || details === 1; + clean.details = isTruthy(params.details); } else { clean.details = default_value; } @@ -35,5 +29,13 @@ function sanitize( req, default_value ){ } +function isTruthy(val) { + if (typeof val === 'string') { + return ['true', '1', 'yes', 'y'].indexOf(val) !== -1; + } + + return val === 1 || val === true; +} + // export function module.exports = sanitize; diff --git a/service/search.js b/service/search.js index d0882170..15b5cd14 100644 --- a/service/search.js +++ b/service/search.js @@ -18,6 +18,9 @@ function service( backend, cmd, cb ){ // handle backend errors if( err ){ return cb( err ); } + // log total ms elasticsearch reported the query took to execute + peliasLogger.verbose( 'time elasticsearch reported:', data.took / 1000 ); + // map returned documents var docs = []; if( data && data.hits && data.hits.total && Array.isArray(data.hits.hits)){ diff --git a/test/unit/query/search.js b/test/unit/query/search.js index 9a09d29e..a56bafa1 100644 --- a/test/unit/query/search.js +++ b/test/unit/query/search.js @@ -76,20 +76,16 @@ var expected = { 'filtered': { 'query': { 'bool': { - 'must': [ - { - 'match': { - 'name.default': 'test' - } + 'must': [{ + 'match': { + 'name.default': 'test' } - ], - 'should': [ - { - 'match': { - 'shingle.default': 'test' - } + }], + 'should': [{ + 'match': { + 'phrase.default': 'test' } - ] + }] } }, 'filter': { @@ -162,20 +158,16 @@ module.exports.tests.query = function(test, common) { 'filtered': { 'query': { 'bool': { - 'must': [ - { - 'match': { - 'name.default': 'test' - } + 'must': [{ + 'match': { + 'name.default': 'test' } - ], - 'should': [ - { - 'match': { - 'shingle.default': 'test' - } + }], + 'should': [{ + 'match': { + 'phrase.default': 'test' } - ] + }] } }, 'filter': { @@ -206,20 +198,16 @@ module.exports.tests.query = function(test, common) { 'filtered': { 'query': { 'bool': { - 'must': [ - { - 'match': { - 'name.default': 'test' - } + 'must': [{ + 'match': { + 'name.default': 'test' } - ], - 'should': [ - { - 'match': { - 'shingle.default': 'test' - } + }], + 'should': [{ + 'match': { + 'phrase.default': 'test' } - ] + }] } }, 'filter': { @@ -239,7 +227,7 @@ module.exports.tests.query = function(test, common) { } ] } - } + } } }, 'sort': [ diff --git a/test/unit/sanitiser/reverse.js b/test/unit/sanitiser/reverse.js index 6dbcda3a..bb1a6a98 100644 --- a/test/unit/sanitiser/reverse.js +++ b/test/unit/sanitiser/reverse.js @@ -137,7 +137,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); }); - var valid_values = [true, 'true', 1]; + var valid_values = [true, 'true', 1, '1', 'yes', 'y']; valid_values.forEach(function(details) { test('valid details param ' + details, function(t) { sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ @@ -154,7 +154,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); }); - var valid_false_values = ['false', false, 0]; + var valid_false_values = ['false', false, 0, '0', 'no', 'n']; valid_false_values.forEach(function(details) { test('test setting false explicitly ' + details, function(t) { sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ diff --git a/test/unit/sanitiser/search.js b/test/unit/sanitiser/search.js index 37763165..77377c94 100644 --- a/test/unit/sanitiser/search.js +++ b/test/unit/sanitiser/search.js @@ -278,7 +278,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); }); - var valid_values = ['true', true, 1]; + var valid_values = ['true', true, 1, '1', 'yes', 'y']; valid_values.forEach(function(details) { test('valid details param ' + details, function(t) { sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ @@ -288,7 +288,7 @@ module.exports.tests.sanitize_details = function(test, common) { }); }); - var valid_false_values = ['false', false, 0]; + var valid_false_values = ['false', false, 0, '0', 'no', 'n']; valid_false_values.forEach(function(details) { test('test setting false explicitly ' + details, function(t) { sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){