Browse Source

rename shingle->phrase, move phrase matching 'should' condition below admin condition', fix formatting/tests

pull/153/head
Peter Johnson 10 years ago
parent
commit
99c6f7e879
  1. 16
      query/search.js
  2. 16
      sanitiser/_details.js
  3. 3
      service/search.js
  4. 62
      test/unit/query/search.js
  5. 4
      test/unit/sanitiser/reverse.js
  6. 4
      test/unit/sanitiser/search.js

16
query/search.js

@ -32,14 +32,6 @@ function generate( params ){
// should query contitions // should query contitions
query.query.filtered.query.bool.should = []; 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) { if (params.input_admin) {
var admin_fields = ['admin0', 'admin1', 'admin1_abbr', 'admin2', 'alpha3']; 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 ) ); query.sort = query.sort.concat( sort( params ) );
return query; return query;

16
sanitiser/_details.js

@ -18,13 +18,7 @@ function sanitize( req, default_value ){
} }
if (params.details !== undefined) { if (params.details !== undefined) {
var details = params.details; clean.details = isTruthy(params.details);
if (typeof params.details === 'string') {
details = params.details === 'true';
}
clean.details = details === true || details === 1;
} else { } else {
clean.details = default_value; 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 // export function
module.exports = sanitize; module.exports = sanitize;

3
service/search.js

@ -18,6 +18,9 @@ function service( backend, cmd, cb ){
// handle backend errors // handle backend errors
if( err ){ return cb( err ); } 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 // map returned documents
var docs = []; var docs = [];
if( data && data.hits && data.hits.total && Array.isArray(data.hits.hits)){ if( data && data.hits && data.hits.total && Array.isArray(data.hits.hits)){

62
test/unit/query/search.js

@ -76,20 +76,16 @@ var expected = {
'filtered': { 'filtered': {
'query': { 'query': {
'bool': { 'bool': {
'must': [ 'must': [{
{ 'match': {
'match': { 'name.default': 'test'
'name.default': 'test'
}
} }
], }],
'should': [ 'should': [{
{ 'match': {
'match': { 'phrase.default': 'test'
'shingle.default': 'test'
}
} }
] }]
} }
}, },
'filter': { 'filter': {
@ -162,20 +158,16 @@ module.exports.tests.query = function(test, common) {
'filtered': { 'filtered': {
'query': { 'query': {
'bool': { 'bool': {
'must': [ 'must': [{
{ 'match': {
'match': { 'name.default': 'test'
'name.default': 'test'
}
} }
], }],
'should': [ 'should': [{
{ 'match': {
'match': { 'phrase.default': 'test'
'shingle.default': 'test'
}
} }
] }]
} }
}, },
'filter': { 'filter': {
@ -206,20 +198,16 @@ module.exports.tests.query = function(test, common) {
'filtered': { 'filtered': {
'query': { 'query': {
'bool': { 'bool': {
'must': [ 'must': [{
{ 'match': {
'match': { 'name.default': 'test'
'name.default': 'test'
}
} }
], }],
'should': [ 'should': [{
{ 'match': {
'match': { 'phrase.default': 'test'
'shingle.default': 'test'
}
} }
] }]
} }
}, },
'filter': { 'filter': {
@ -239,7 +227,7 @@ module.exports.tests.query = function(test, common) {
} }
] ]
} }
} }
} }
}, },
'sort': [ 'sort': [

4
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) { valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) { test('valid details param ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ 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) { valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) { test('test setting false explicitly ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){

4
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) { valid_values.forEach(function(details) {
test('valid details param ' + details, function(t) { test('valid details param ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ 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) { valid_false_values.forEach(function(details) {
test('test setting false explicitly ' + details, function(t) { test('test setting false explicitly ' + details, function(t) {
sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){ sanitize({ input: 'test', lat: 0, lon: 0, details: details }, function( err, clean ){

Loading…
Cancel
Save