Browse Source

using async (parallel)

msv2
Harish Krishna 10 years ago
parent
commit
5f8c3da14e
  1. 43
      controller/suggest.js
  2. 3
      package.json

43
controller/suggest.js

@ -1,5 +1,6 @@
var geojsonify = require('../helper/geojsonify').suggest; var geojsonify = require('../helper/geojsonify').suggest;
var async = require('async');
function setup( backend, query ){ function setup( backend, query ){
@ -11,13 +12,10 @@ function setup( backend, query ){
backend = backend || require('../src/backend'); backend = backend || require('../src/backend');
var query_admin = require('../query/suggest_admin'); var query_admin = require('../query/suggest_admin');
var query_poi = require('../query/suggest_poi'); var query_poi = require('../query/suggest_poi');
// **query_poi** command
var cmd = { var cmd = {
index: 'pelias', index: 'pelias'
body: query_poi( req.clean )
}; };
var query_backend = function(cmd, callback) {
// query backend // query backend
backend().client.suggest( cmd, function( err, data ){ backend().client.suggest( cmd, function( err, data ){
@ -31,27 +29,23 @@ function setup( backend, query ){
docs = data['pelias'][0].options || []; docs = data['pelias'][0].options || [];
} }
// **query_admin** command callback(null, docs);
var cmd = { });
index: 'pelias',
body: query_admin( req.clean )
}; };
// query backend async.parallel({
backend().client.suggest( cmd, function( err, data ){ admin: function(callback){
cmd.body = query_admin( req.clean );
var docs2 = []; query_backend(cmd, callback);
},
// handle backend errors poi: function(callback){
if( err ){ return next( err ); } cmd.body = query_poi( req.clean );
query_backend(cmd, callback);
// map response to a valid FeatureCollection
if( data && Array.isArray( data.pelias ) && data.pelias.length ){
docs2 = data['pelias'][0].options || [];
} }
},
/** --- combine 2 doc sets --- **/ function(err, results) {
var combined = docs2.slice(0, 3).concat(docs); // results is now equals to: {admin: docs, poi: docs}
var combined = results.poi.slice(0, 3).concat(results.admin);
// convert docs to geojson // convert docs to geojson
var geojson = geojsonify( combined ); var geojson = geojsonify( combined );
@ -61,9 +55,6 @@ function setup( backend, query ){
// respond // respond
return res.status(200).json( geojson ); return res.status(200).json( geojson );
});
}); });
} }

3
package.json

@ -36,7 +36,8 @@
"geojson": "^0.2.0", "geojson": "^0.2.0",
"geopipes-elasticsearch-backend": "0.0.8", "geopipes-elasticsearch-backend": "0.0.8",
"pelias-esclient": "0.0.25", "pelias-esclient": "0.0.25",
"toobusy": "^0.2.4" "toobusy": "^0.2.4",
"async": "^0.9.0"
}, },
"devDependencies": { "devDependencies": {
"ciao": "^0.3.4", "ciao": "^0.3.4",

Loading…
Cancel
Save