Browse Source

progress commit

msv2
Peter Johnson 10 years ago
parent
commit
6558cc9289
  1. 4
      app.js
  2. 52
      controller/suggest.js
  3. 50
      controller/suggest_admin.js
  4. 46
      controller/suggest_poi.js
  5. 29
      query/suggest_admin.js
  6. 0
      query/suggest_poi.js

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

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

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

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

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

0
query/suggest.js → query/suggest_poi.js

Loading…
Cancel
Save