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. 7
      query/search.js
  3. 5
      query/suggest.js
  4. 6
      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. 7
      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"

7
query/search.js

@ -4,13 +4,16 @@ var logger = require('../src/logger'),
function generate( params ){
var centroid = {
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);

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

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

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