Browse Source

search with optional bbox - initial commit +tests

pull/60/head
Harish Krishna 10 years ago
parent
commit
ebf162f29e
  1. 2
      package.json
  2. 4
      query/search.js
  3. 34
      sanitiser/_latlonzoom.js
  4. 52
      test/unit/query/search.js

2
package.json

@ -36,7 +36,7 @@
"express": "^4.8.8", "express": "^4.8.8",
"geojson": "^0.2.0", "geojson": "^0.2.0",
"geojson-extent": "^0.3.1", "geojson-extent": "^0.3.1",
"geopipes-elasticsearch-backend": "0.0.8", "geopipes-elasticsearch-backend": "git://github.com/geopipes/elasticsearch-backend#geo_bbox",
"pelias-esclient": "0.0.25", "pelias-esclient": "0.0.25",
"toobusy": "^0.2.4" "toobusy": "^0.2.4"
}, },

4
query/search.js

@ -11,6 +11,10 @@ function generate( params ){
var query = queries.distance( centroid, { size: params.size } ); var 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 // add search condition to distance query
query.query.filtered.query = { query.query.filtered.query = {
query_string : { query_string : {

34
sanitiser/_latlonzoom.js

@ -9,9 +9,17 @@ function sanitize( req ){
params = {}; params = {};
} }
var is_invalid_lat = function(lat) {
return isNaN( lat ) || lat < -90 || lat > 90;
};
var is_invalid_lon = function(lon) {
return isNaN( lon ) || lon < -180 || lon > 180;
};
// lat // lat
var lat = parseFloat( params.lat, 10 ); var lat = parseFloat( params.lat, 10 );
if( isNaN( lat ) || lat < -90 || lat > 90 ){ if( is_invalid_lat(lat) ){
return { return {
'error': true, 'error': true,
'message': 'invalid param \'lat\': must be >-90 and <90' 'message': 'invalid param \'lat\': must be >-90 and <90'
@ -21,7 +29,7 @@ function sanitize( req ){
// lon // lon
var lon = parseFloat( params.lon, 10 ); var lon = parseFloat( params.lon, 10 );
if( isNaN( lon ) || lon < -180 || lon > 180 ){ if( is_invalid_lon(lon) ){
return { return {
'error': true, 'error': true,
'message': 'invalid param \'lon\': must be >-180 and <180' 'message': 'invalid param \'lon\': must be >-180 and <180'
@ -37,6 +45,28 @@ function sanitize( req ){
clean.zoom = 10; // default clean.zoom = 10; // default
} }
// bbox
if (params.bbox) {
var bbox = [];
var bboxArr = params.bbox.split(',');
if( Array.isArray(bboxArr) && bboxArr.length === 4 ){
bboxArr.forEach(function(latlon, index) {
latlon = parseFloat(latlon, 10);
if ( !(index % 2 === 0 ? is_invalid_lat(latlon) : is_invalid_lon(latlon)) ) {
bbox.push(latlon);
}
});
if (bbox.length === 4) {
clean.bbox = bbox;
} else {
return {
'error': true,
'message': 'invalid bbox'
};
}
}
}
req.clean = clean; req.clean = clean;
return { 'error': false }; return { 'error': false };

52
test/unit/query/search.js

@ -15,16 +15,17 @@ module.exports.tests.query = function(test, common) {
var query = generate({ var query = generate({
input: 'test', size: 10, input: 'test', size: 10,
lat: 29.49136, lon: -82.50622, lat: 29.49136, lon: -82.50622,
bbox: { bbox: [47.47, -61.84, 11.51, -103.16],
bottom_left: { // bbox: { //TODO write a test where no bbox is provided
lat: 11.51053655297385, // bottom_left: {
lon: -103.16362455862279 // lat: 11.51053655297385,
}, // lon: -103.16362455862279
top_right: { // },
lat: 47.472183447026154, // top_right: {
lon: -61.84881544137721 // lat: 47.472183447026154,
} // lon: -61.84881544137721
}, // }
// },
layers: ['test'] layers: ['test']
}); });
@ -44,34 +45,25 @@ module.exports.tests.query = function(test, common) {
'bool': { 'bool': {
'must': [ 'must': [
{ {
'geo_distance': { 'geo_bounding_box': {
'distance': '50km',
'distance_type': 'plane',
'optimize_bbox': 'indexed',
'_cache': true,
'center_point': { 'center_point': {
'lat': '29.49', 'top_right': {
'lon': '-82.51' 'lat': '47.47',
'lon': '-61.84'
},
'bottom_left': {
'lat': '11.51',
'lon': '-103.16'
}
} }
} }
} }
] ]
} }
}
}
},
'sort': [
{
'_geo_distance': {
'center_point': {
'lat': 29.49136,
'lon': -82.50622
},
'order': 'asc',
'unit': 'km'
} }
} }
], },
'sort': [],
'size': 10 'size': 10
}; };

Loading…
Cancel
Save