Browse Source

Merge pull request #37 from pelias/service_wrapper

Service wrapper
pull/39/head 1.0.0
Peter Johnson a.k.a. insertcoffee 10 years ago
parent
commit
8370341450
  1. 26
      controller/doc.js
  2. 14
      controller/search.js
  3. 12
      controller/suggest.js
  4. 43
      service/mget.js
  5. 30
      service/search.js
  6. 28
      service/suggest.js

26
controller/doc.js

@ -1,15 +1,15 @@
var service = { mget: require('../service/mget') };
var geojsonify = require('../helper/geojsonify').search;
function setup( backend ){
// allow overriding of dependencies
backend = backend || require('../src/backend');
backend = new backend();
function controller( req, res, next ){
var docs = req.clean.ids.map( function(id) {
var query = req.clean.ids.map( function(id) {
return {
_index: 'pelias',
_type: id.type,
@ -17,25 +17,10 @@ function setup( backend ){
};
});
// backend command
var cmd = {
body: {
docs: docs
}
};
// query new backend
backend.client.mget( cmd, function( err, data ){
var docs = [];
// handle backend errors
if( err ){ return next( err ); }
service.mget( backend, query, function( err, docs ){
if( data && data.docs && Array.isArray(data.docs) && data.docs.length ){
docs = data.docs.map( function( doc ){
return doc._source;
});
}
// error handler
if( err ){ return next( err ); }
// convert docs to geojson
var geojson = geojsonify( docs );
@ -45,6 +30,7 @@ function setup( backend ){
// respond
return res.status(200).json( geojson );
});
}

14
controller/search.js

@ -1,4 +1,5 @@
var service = { search: require('../service/search') };
var geojsonify = require('../helper/geojsonify').search;
function setup( backend, query ){
@ -18,20 +19,13 @@ function setup( backend, query ){
if (req.clean.layers) {
cmd.type = req.clean.layers;
}
// query backend
backend().client.search( cmd, function( err, data ){
var docs = [];
// query backend
service.search( backend, cmd, function( err, docs ){
// handle backend errors
// error handler
if( err ){ return next( err ); }
if( data && data.hits && data.hits.total && Array.isArray(data.hits.hits)){
docs = data.hits.hits.map( function( hit ){
return hit._source;
});
}
// convert docs to geojson
var geojson = geojsonify( docs );

12
controller/suggest.js

@ -1,4 +1,5 @@
var service = { suggest: require('../service/suggest') };
var geojsonify = require('../helper/geojsonify').suggest;
function setup( backend, query ){
@ -16,18 +17,11 @@ function setup( backend, query ){
};
// query backend
backend().client.suggest( cmd, function( err, data ){
service.suggest( backend, cmd, function( err, docs ){
var docs = [];
// handle backend errors
// error handler
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 );

43
service/mget.js

@ -0,0 +1,43 @@
/**
query must be an array of hashes, structured like so:
{
_index: 'myindex',
_type: 'mytype',
_id: 'myid'
}
**/
function service( backend, query, cb ){
// backend command
var cmd = {
body: {
docs: query
}
};
// query new backend
backend().client.mget( cmd, function( err, data ){
// handle backend errors
if( err ){ return cb( err ); }
// map returned documents
var docs = [];
if( data && Array.isArray(data.docs) ){
docs = data.docs.map( function( doc ){
return doc._source;
});
}
// fire callback
return cb( null, docs );
});
}
module.exports = service;

30
service/search.js

@ -0,0 +1,30 @@
/**
cmd can be any valid ES query command
**/
function service( backend, cmd, cb ){
// query new backend
backend().client.search( cmd, function( err, data ){
// handle backend errors
if( err ){ return cb( err ); }
// map returned documents
var docs = [];
if( data && data.hits && data.hits.total && Array.isArray(data.hits.hits)){
docs = data.hits.hits.map( function( hit ){
return hit._source;
});
}
// fire callback
return cb( null, docs );
});
}
module.exports = service;

28
service/suggest.js

@ -0,0 +1,28 @@
/**
cmd can be any valid ES suggest command
**/
function service( backend, cmd, cb ){
// query new backend
backend().client.suggest( cmd, function( err, data ){
// handle backend errors
if( err ){ return cb( err ); }
// map returned documents
var docs = [];
if( data && Array.isArray( data.pelias ) && data.pelias.length ){
docs = data['pelias'][0].options || [];
}
// fire callback
return cb( null, docs );
});
}
module.exports = service;
Loading…
Cancel
Save