diff --git a/app.js b/app.js index 987b5bcc..e563723b 100644 --- a/app.js +++ b/app.js @@ -20,6 +20,8 @@ sanitisers.reverse = require('./sanitiser/reverse'); var controllers = {}; controllers.index = require('./controller/index'); controllers.suggest = require('./controller/suggest'); +controllers.suggest_poi = require('./controller/suggest_poi'); +controllers.suggest_admin = require('./controller/suggest_admin'); controllers.search = require('./controller/search'); /** ----------------------- routes ----------------------- **/ @@ -29,6 +31,8 @@ app.get( '/', controllers.index() ); // suggest API app.get( '/suggest', sanitisers.suggest.middleware, controllers.suggest() ); +app.get( '/suggest/poi', sanitisers.suggest.middleware, controllers.suggest_poi() ); +app.get( '/suggest/admin', sanitisers.suggest.middleware, controllers.suggest_admin() ); // search API app.get( '/search', sanitisers.search.middleware, controllers.search() ); diff --git a/controller/suggest.js b/controller/suggest.js index 36c3cc98..27da5946 100644 --- a/controller/suggest.js +++ b/controller/suggest.js @@ -3,16 +3,19 @@ var geojsonify = require('../helper/geojsonify').suggest; function setup( backend, query ){ - // allow overriding of dependencies - backend = backend || require('../src/backend'); - query = query || require('../query/suggest'); - function controller( req, res, next ){ - // backend command + // combine the 2 queries + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + var query_admin = require('../query/suggest_admin'); + var query_poi = require('../query/suggest_admin'); + + // **query_poi** command var cmd = { index: 'pelias', - body: query( req.clean ) + body: query_poi( req.clean ) }; // query backend @@ -28,14 +31,39 @@ function setup( backend, query ){ docs = data['pelias'][0].options || []; } - // convert docs to geojson - var geojson = geojsonify( docs ); + // **query_admin** command + var cmd = { + index: 'pelias', + body: query_admin( req.clean ) + }; + + // query backend + backend().client.suggest( cmd, function( err, data ){ + + var docs2 = []; + + // handle backend errors + if( err ){ return next( err ); } + + // map response to a valid FeatureCollection + if( data && Array.isArray( data.pelias ) && data.pelias.length ){ + docs2 = data['pelias'][0].options || []; + } + + /** --- combine 2 doc sets --- **/ + var combined = docs2.slice(0, 3).concat(docs); + + // convert docs to geojson + var geojson = geojsonify( combined ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); - // response envelope - geojson.date = new Date().getTime(); + }); - // respond - return res.status(200).json( geojson ); }); } diff --git a/controller/suggest_admin.js b/controller/suggest_admin.js new file mode 100644 index 00000000..743865a5 --- /dev/null +++ b/controller/suggest_admin.js @@ -0,0 +1,50 @@ + +/** + README: http://www.elasticsearch.org/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-suggest +**/ + +var geojsonify = require('../helper/geojsonify').suggest; + +function setup( backend, query ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + query = query || require('../query/suggest_admin'); + + function controller( req, res, next ){ + + // backend command + var cmd = { + index: 'pelias', + body: query( req.clean ) + }; + + // query backend + backend().client.suggest( cmd, function( err, data ){ + + var docs = []; + + // handle backend errors + if( err ){ return next( err ); } + + // map response to a valid FeatureCollection + if( data && Array.isArray( data.pelias ) && data.pelias.length ){ + docs = data['pelias'][0].options || []; + } + + // convert docs to geojson + var geojson = geojsonify( docs ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); + }); + + } + + return controller; +} + +module.exports = setup; \ No newline at end of file diff --git a/controller/suggest_poi.js b/controller/suggest_poi.js new file mode 100644 index 00000000..9e30b41a --- /dev/null +++ b/controller/suggest_poi.js @@ -0,0 +1,46 @@ + +var geojsonify = require('../helper/geojsonify').suggest; + +function setup( backend, query ){ + + // allow overriding of dependencies + backend = backend || require('../src/backend'); + query = query || require('../query/suggest_poi'); + + function controller( req, res, next ){ + + // backend command + var cmd = { + index: 'pelias', + body: query( req.clean ) + }; + + // query backend + backend().client.suggest( cmd, function( err, data ){ + + var docs = []; + + // handle backend errors + if( err ){ return next( err ); } + + // map response to a valid FeatureCollection + if( data && Array.isArray( data.pelias ) && data.pelias.length ){ + docs = data['pelias'][0].options || []; + } + + // convert docs to geojson + var geojson = geojsonify( docs ); + + // response envelope + geojson.date = new Date().getTime(); + + // respond + return res.status(200).json( geojson ); + }); + + } + + return controller; +} + +module.exports = setup; \ No newline at end of file diff --git a/query/suggest_admin.js b/query/suggest_admin.js new file mode 100644 index 00000000..bb004fd8 --- /dev/null +++ b/query/suggest_admin.js @@ -0,0 +1,29 @@ + +var logger = require('../src/logger'); + +// Build pelias suggest query +function generate( params ){ + + var cmd = { + 'pelias' : { + 'text' : params.input, + 'completion' : { + 'size' : params.size, + 'field' : 'suggest', + 'context': { + 'dataset': ['admin0','admin1','admin2'], + 'location': { + 'value': [ params.lon, params.lat ], + 'precision': 1 + } + } + } + } + }; + + // logger.log( 'cmd', JSON.stringify( cmd, null, 2 ) ); + return cmd; + +} + +module.exports = generate; \ No newline at end of file diff --git a/query/suggest.js b/query/suggest_poi.js similarity index 100% rename from query/suggest.js rename to query/suggest_poi.js