Browse Source

feat: Merge pull request #703 from pelias/autocomplete-boundary-rect

Add boundary.rect (bounding box) parameter to /v1/autocomplete
pull/704/head v3.6.0
Julian Simioni 8 years ago committed by GitHub
parent
commit
510f0095c1
  1. 14
      query/autocomplete.js
  2. 2
      sanitizer/_geo_autocomplete.js
  3. 37
      test/ciao/autocomplete/boundary_rect_valid.coffee
  4. 71
      test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js
  5. 20
      test/unit/query/autocomplete.js

14
query/autocomplete.js

@ -49,6 +49,7 @@ query.score( peliasQuery.view.population( views.pop_subquery ) );
// non-scoring hard filters // non-scoring hard filters
query.filter( peliasQuery.view.sources ); query.filter( peliasQuery.view.sources );
query.filter( peliasQuery.view.layers ); query.filter( peliasQuery.view.layers );
query.filter( peliasQuery.view.boundary_rect );
// -------------------------------- // --------------------------------
@ -109,6 +110,19 @@ function generateQuery( clean ){
}); });
} }
// boundary rect
if( check.number(clean['boundary.rect.min_lat']) &&
check.number(clean['boundary.rect.max_lat']) &&
check.number(clean['boundary.rect.min_lon']) &&
check.number(clean['boundary.rect.max_lon']) ){
vs.set({
'boundary:rect:top': clean['boundary.rect.max_lat'],
'boundary:rect:right': clean['boundary.rect.max_lon'],
'boundary:rect:bottom': clean['boundary.rect.min_lat'],
'boundary:rect:left': clean['boundary.rect.min_lon']
});
}
// run the address parser // run the address parser
if( clean.parsed_text ){ if( clean.parsed_text ){
textParser( clean.parsed_text, vs ); textParser( clean.parsed_text, vs );

2
sanitizer/_geo_autocomplete.js

@ -1,5 +1,6 @@
var geo_common = require ('./_geo_common'); var geo_common = require ('./_geo_common');
var LAT_LON_IS_REQUIRED = false; var LAT_LON_IS_REQUIRED = false;
var RECT_IS_REQUIRED = false;
// validate inputs, convert types and apply defaults // validate inputs, convert types and apply defaults
module.exports = function sanitize( raw, clean ){ module.exports = function sanitize( raw, clean ){
@ -9,6 +10,7 @@ module.exports = function sanitize( raw, clean ){
try { try {
geo_common.sanitize_point( 'focus.point', clean, raw, LAT_LON_IS_REQUIRED ); geo_common.sanitize_point( 'focus.point', clean, raw, LAT_LON_IS_REQUIRED );
geo_common.sanitize_rect( 'boundary.rect', clean, raw, RECT_IS_REQUIRED );
} }
catch (err) { catch (err) {
messages.errors.push( err.message ); messages.errors.push( err.message );

37
test/ciao/autocomplete/boundary_rect_valid.coffee

@ -0,0 +1,37 @@
#> focus point
path: '/v1/autocomplete?text=cairo&boundary.rect.min_lat=30&boundary.rect.max_lat=32&boundary.rect.min_lon=29&boundary.rect.max_lon=31'
#? 200 ok
response.statusCode.should.be.equal 200
response.should.have.header 'charset', 'utf8'
response.should.have.header 'content-type', 'application/json; charset=utf-8'
#? valid geocoding block
should.exist json.geocoding
should.exist json.geocoding.version
should.exist json.geocoding.attribution
should.exist json.geocoding.query
should.exist json.geocoding.engine
should.exist json.geocoding.engine.name
should.exist json.geocoding.engine.author
should.exist json.geocoding.engine.version
should.exist json.geocoding.timestamp
#? valid geojson
json.type.should.be.equal 'FeatureCollection'
json.features.should.be.instanceof Array
#? expected errors
should.not.exist json.geocoding.errors
#? expected warnings
should.not.exist json.geocoding.warnings
#? inputs
json.geocoding.query['text'].should.eql 'cairo'
json.geocoding.query['size'].should.eql 10
json.geocoding.query['boundary.rect.min_lat'].should.eql 30
json.geocoding.query['boundary.rect.max_lat'].should.eql 32
json.geocoding.query['boundary.rect.min_lon'].should.eql 29
json.geocoding.query['boundary.rect.max_lon'].should.eql 31

71
test/unit/fixture/autocomplete_linguistic_bbox_san_francisco.js

@ -0,0 +1,71 @@
module.exports = {
'query': {
'bool': {
'must': [{
'constant_score': {
'query': {
'match': {
'name.default': {
'analyzer': 'peliasQueryPartialToken',
'boost': 100,
'query': 'test',
'type': 'phrase',
'operator': 'and',
'slop': 3
}
}
}
}
}],
'should':[{
'function_score': {
'query': {
'match_all': {}
},
'max_boost': 20,
'score_mode': 'first',
'boost_mode': 'replace',
'functions': [{
'field_value_factor': {
'modifier': 'log1p',
'field': 'popularity',
'missing': 1
},
'weight': 1
}]
}
},{
'function_score': {
'query': {
'match_all': {}
},
'max_boost': 20,
'score_mode': 'first',
'boost_mode': 'replace',
'functions': [{
'field_value_factor': {
'modifier': 'log1p',
'field': 'population',
'missing': 1
},
'weight': 3
}]
}
}],
'filter': [{
'geo_bounding_box': {
'type': 'indexed',
'center_point': {
'top': 37.83239,
'right': -122.35698,
'bottom': 37.70808,
'left': -122.51489
}
}
}]
}
},
'sort': [ '_score' ],
'size': 20,
'track_scores': true
};

20
test/unit/query/autocomplete.js

@ -189,6 +189,26 @@ module.exports.tests.query = function(test, common) {
t.deepEqual(compiled.body, expected, 'autocomplete: valid boundary.country query'); t.deepEqual(compiled.body, expected, 'autocomplete: valid boundary.country query');
t.end(); t.end();
}); });
test('autocomplete + bbox around San Francisco', function(t) {
var query = generate({
text: 'test',
'boundary.rect.max_lat': 37.83239,
'boundary.rect.max_lon': -122.35698,
'boundary.rect.min_lat': 37.70808,
'boundary.rect.min_lon': -122.51489,
tokens: ['test'],
tokens_complete: [],
tokens_incomplete: ['test']
});
var compiled = JSON.parse( JSON.stringify( query ) );
var expected = require('../fixture/autocomplete_linguistic_bbox_san_francisco');
t.deepEqual(compiled.type, 'autocomplete', 'query type set');
t.deepEqual(compiled.body, expected, 'autocomplete_linguistic_focus_null_island');
t.end();
});
}; };
module.exports.all = function (tape, common) { module.exports.all = function (tape, common) {

Loading…
Cancel
Save