Browse Source

Merge pull request #68 from pelias/split-comma-admin

Analyzing the query to find admin values
pull/81/head 1.1.5
Harish Krishna 10 years ago
parent
commit
f74b2c50e9
  1. 24
      query/search.js
  2. 10
      sanitiser/_input.js
  3. 52
      test/unit/query/search.js
  4. 24
      test/unit/sanitiser/suggest.js

24
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', 'admin1_abbr', 'admin2', '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);

10
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).trim();
}
return { 'error': false };
}

52
test/unit/query/search.js

@ -57,12 +57,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': {
@ -109,12 +110,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': {
@ -155,12 +157,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': {
@ -190,12 +193,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': {

24
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 ],

Loading…
Cancel
Save