Browse Source

Merge pull request #66 from pelias/geobias-optional

Making Geobias optional
pull/72/head 1.1.3
Harish Krishna 10 years ago
parent
commit
28b0d062a6
  1. 2
      package.json
  2. 15
      query/search.js
  3. 5
      query/suggest.js
  4. 36
      sanitiser/_geo.js
  5. 83
      test/unit/query/search.js
  6. 26
      test/unit/query/suggest.js
  7. 3
      test/unit/sanitiser/coarse.js
  8. 9
      test/unit/sanitiser/suggest.js

2
package.json

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

15
query/search.js

@ -4,13 +4,16 @@ var logger = require('../src/logger'),
function generate( params ){
var centroid = {
lat: params.lat,
lon: params.lon
};
var centroid = null;
if ( params.lat && params.lon ){
centroid = {
lat: params.lat,
lon: params.lon
};
}
var query = queries.distance( centroid, { size: params.size } );
if (params.bbox) {
query = queries.bbox ( centroid, { size: params.size, bbox: params.bbox } );
}

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': null,
'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);

36
sanitiser/_geo.js

@ -18,32 +18,34 @@ function sanitize( req ){
};
// lat
var lat = parseFloat( params.lat, 10 );
if( is_invalid_lat(lat) ){
return {
'error': true,
'message': 'invalid param \'lat\': must be >-90 and <90'
};
if (!isNaN(params.lat)) {
var lat = parseFloat( params.lat, 10 );
if( is_invalid_lat(lat) ){
return {
'error': true,
'message': 'invalid param \'lat\': must be >-90 and <90'
};
}
clean.lat = lat;
}
clean.lat = lat;
// lon
var lon = parseFloat( params.lon, 10 );
if( is_invalid_lon(lon) ){
return {
'error': true,
'message': 'invalid param \'lon\': must be >-180 and <180'
};
if (!isNaN(params.lon)) {
var lon = parseFloat( params.lon, 10 );
if( is_invalid_lon(lon) ){
return {
'error': true,
'message': 'invalid param \'lon\': must be >-180 and <180'
};
}
clean.lon = lon;
}
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
// bbox = bottom_left lat, bottom_left lon, top_right lat, top_right lon

83
test/unit/query/search.js

@ -63,6 +63,89 @@ module.exports.tests.query = function(test, common) {
t.end();
});
test('valid query without lat/lon', function(t) {
var query = generate({
input: 'test', size: 10,
bbox: {
top: 47.47,
right: -61.84,
bottom: 11.51,
left: -103.16
},
layers: ['test']
});
var expected = {
'query': {
'filtered': {
'query': {
'query_string': {
'query': 'test',
'fields': [
'name.default'
],
'default_operator': 'OR'
}
},
'filter': {
'bool': {
'must': [
{
'geo_bounding_box': {
'center_point': {
'top': '47.47',
'right': '-61.84',
'bottom':'11.51',
'left': '-103.16'
},
'_cache': true
}
}
]
}
}
}
},
'sort': [],
'size': 10
};
t.deepEqual(query, expected, 'valid search query');
t.end();
});
test('valid query with no lat/lon and no bbox', function(t) {
var query = generate({
input: 'test', size: 10,
layers: ['test']
});
var expected = {
'query': {
'filtered': {
'query': {
'query_string': {
'query': 'test',
'fields': [
'name.default'
],
'default_operator': 'OR'
}
},
'filter': {
'bool': {
'must': []
}
}
}
},
'size': 10
};
t.deepEqual(query, expected, 'valid search query');
t.end();
});
test('valid query without bbox', function(t) {
var query = generate({
input: 'test', size: 10,

26
test/unit/query/suggest.js

@ -37,6 +37,32 @@ module.exports.tests.query = function(test, common) {
t.deepEqual(query, expected, 'valid suggest query');
t.end();
});
test('valid query without lat/lon', function(t) {
var query = generate({
input: 'test', size: 10,
layers: ['test']
});
var expected = {
text: 'test',
0: {
completion: {
field: 'suggest',
size: 10,
context: {
dataset: [ 'test' ],
location: {
precision: 1,
value: null
}
},
fuzzy: { fuzziness: 0 },
}
}
};
t.deepEqual(query, expected, 'valid suggest query');
t.end();
});
};
module.exports.tests.precision = function(test, common) {

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);

9
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
lon: 0,
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