From 0301fe8e74da9c48ad02464d8cba55d59facc601 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Mon, 3 Nov 2014 14:16:32 +0000 Subject: [PATCH] search service --- controller/search.js | 14 ++++---------- service/search.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 service/search.js diff --git a/controller/search.js b/controller/search.js index 2554de2d..36f6c3f1 100644 --- a/controller/search.js +++ b/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 ); diff --git a/service/search.js b/service/search.js new file mode 100644 index 00000000..ff413382 --- /dev/null +++ b/service/search.js @@ -0,0 +1,30 @@ + +/** + + query can be any valid ES query + +**/ + +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; \ No newline at end of file