Browse Source

making geo bias optional - first pass with all tests passing

pull/66/head
Harish Krishna 10 years ago
parent
commit
650e88d79f
  1. 49
      query/search.js
  2. 5
      query/suggest.js
  3. 6
      sanitiser/_geo.js
  4. 3
      test/unit/sanitiser/coarse.js
  5. 7
      test/unit/sanitiser/suggest.js

49
query/search.js

@ -4,16 +4,59 @@ var logger = require('../src/logger'),
function generate( params ){
var query = {
'query': {
'filtered': {
'query': {
'match_all': {}
},
'filter' : {
'bool': {
'must': []
}
}
}
},
'size': params.size
};
// TODO: DRY (the following lines are repeated here and in geopipes-elasticsearchbackend)
// query/geo_bbox - cleanup necessary #techdebt
if (params.bbox) {
var options = {
top : Number( params.bbox.top ).toFixed(2), // @note: make filter cachable
right : Number( params.bbox.right ).toFixed(2), // precision max ~1.113km off
bottom: Number( params.bbox.bottom ).toFixed(2),
left : Number( params.bbox.left ).toFixed(2),
size: params.size || 1,
field: params.field || 'center_point'
};
var filter = {
'geo_bounding_box' : {
'_cache': true // Speed up duplicate queries. Memory impact?
}
};
filter.geo_bounding_box[ options.field ] = {
'top' : options.top,
'right' : options.right,
'bottom': options.bottom,
'left' : options.left,
};
query.query.filtered.filter.bool.must.push( filter );
}
if ( params.lat && params.lon ){
var centroid = {
lat: params.lat,
lon: params.lon
};
var query = queries.distance( centroid, { size: params.size } );
query = queries.distance( centroid, { size: params.size } );
if (params.bbox) {
query = queries.bbox ( centroid, { size: params.size, bbox: params.bbox } );
}
}
// add search condition to distance query
query.query.filtered.query = {

5
query/suggest.js

@ -36,7 +36,7 @@ function generate( params, query_mixer, fuzziness ){
'context': {
'dataset': layers || this.params.layers,
'location': {
'value': [ this.params.lon, this.params.lat ],
'value': [ ],
'precision': precision || this.get_precision()
}
},
@ -45,6 +45,9 @@ function generate( params, query_mixer, fuzziness ){
}
}
};
if (!isNaN(this.params.lon) && !isNaN(this.params.lat)) {
this.cmd[name].completion.context.location.value = [ this.params.lon, this.params.lat ];
}
};
var cmd = new CmdGenerator(params);

6
sanitiser/_geo.js

@ -18,6 +18,7 @@ function sanitize( req ){
};
// lat
if (!isNaN(params.lat)) {
var lat = parseFloat( params.lat, 10 );
if( is_invalid_lat(lat) ){
return {
@ -26,8 +27,10 @@ function sanitize( req ){
};
}
clean.lat = lat;
}
// lon
if (!isNaN(params.lon)) {
var lon = parseFloat( params.lon, 10 );
if( is_invalid_lon(lon) ){
return {
@ -36,13 +39,12 @@ function sanitize( req ){
};
}
clean.lon = lon;
}
// zoom level
var zoom = parseInt( params.zoom, 10 );
if( !isNaN( zoom ) ){
clean.zoom = Math.min( Math.max( zoom, 1 ), 18 ); // max
} else {
clean.zoom = 10; // default
}
// bbox

3
test/unit/sanitiser/coarse.js

@ -53,8 +53,7 @@ module.exports.tests.middleware_success = function(test, common) {
size: 10,
layers: [ 'admin0', 'admin1', 'admin2', 'neighborhood' ],
lat: 0,
lon: 0,
zoom: 10
lon: 0
};
t.equal(message, undefined, 'no error message set');
t.deepEqual(req.clean, defaultClean);

7
test/unit/sanitiser/suggest.js

@ -4,12 +4,11 @@ var suggest = require('../../../sanitiser/suggest'),
middleware = suggest.middleware,
defaultError = 'invalid param \'input\': text length, must be >0',
defaultClean = { input: 'test',
lat: 0,
lat:0,
layers: [ 'geoname', 'osmnode', 'osmway', 'admin0', 'admin1', 'admin2', 'neighborhood',
'osmaddress', 'openaddresses' ],
lon: 0,
size: 10,
zoom: 10
size: 10
},
sanitize = function(query, cb) { _sanitize({'query':query}, cb); };
@ -173,7 +172,7 @@ module.exports.tests.sanitize_bbox = function(test, common) {
module.exports.tests.sanitize_zoom = function(test, common) {
test('invalid zoom value', function(t) {
sanitize({ zoom: 'a', input: 'test', lat: 0, lon: 0 }, function( err, clean ){
t.equal(clean.zoom, 10, 'default zoom set');
t.equal(clean.zoom, undefined, 'zoom not set');
t.end();
});
});

Loading…
Cancel
Save