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. 50
      test/unit/query/search.js

2
package.json

@ -36,7 +36,7 @@
"express": "^4.8.8",
"geojson": "^0.2.0",
"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",
"toobusy": "^0.2.4"
},

4
query/search.js

@ -11,6 +11,10 @@ function generate( params ){
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
query.query.filtered.query = {
query_string : {

34
sanitiser/_latlonzoom.js

@ -9,9 +9,17 @@ function sanitize( req ){
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
var lat = parseFloat( params.lat, 10 );
if( isNaN( lat ) || lat < -90 || lat > 90 ){
if( is_invalid_lat(lat) ){
return {
'error': true,
'message': 'invalid param \'lat\': must be >-90 and <90'
@ -21,7 +29,7 @@ function sanitize( req ){
// lon
var lon = parseFloat( params.lon, 10 );
if( isNaN( lon ) || lon < -180 || lon > 180 ){
if( is_invalid_lon(lon) ){
return {
'error': true,
'message': 'invalid param \'lon\': must be >-180 and <180'
@ -37,6 +45,28 @@ function sanitize( req ){
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;
return { 'error': false };

50
test/unit/query/search.js

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

Loading…
Cancel
Save