Browse Source

search service

pull/37/head
Peter Johnson 10 years ago
parent
commit
0301fe8e74
  1. 14
      controller/search.js
  2. 30
      service/search.js

14
controller/search.js

@ -1,4 +1,5 @@
var service = { search: require('../service/search') };
var geojsonify = require('../helper/geojsonify').search; var geojsonify = require('../helper/geojsonify').search;
function setup( backend, query ){ function setup( backend, query ){
@ -18,20 +19,13 @@ function setup( backend, query ){
if (req.clean.layers) { if (req.clean.layers) {
cmd.type = 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( 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 // convert docs to geojson
var geojson = geojsonify( docs ); var geojson = geojsonify( docs );

30
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;
Loading…
Cancel
Save