You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1 KiB

10 years ago
/**
10 years ago
cmd can be any valid ES query command
10 years ago
**/
var peliasLogger = require( 'pelias-logger' ).get( 'service/search' );
var microtime = require( 'microtime' );
10 years ago
function service( backend, cmd, cb ){
var startTime = microtime.nowDouble();
10 years ago
// query new backend
backend().client.search( cmd, function( err, data ){
peliasLogger.verbose( 'time elasticsearch query took:', microtime.nowDouble() - startTime );
10 years ago
// handle backend errors
if( err ){ return cb( err ); }
// log total ms elasticsearch reported the query took to execute
peliasLogger.verbose( 'time elasticsearch reported:', data.took / 1000 );
10 years ago
// map returned documents
var docs = [];
if( data && data.hits && data.hits.total && Array.isArray(data.hits.hits)){
docs = data.hits.hits.map( function( hit ){
// map metadata in to _source so we
// can serve it up to the consumer
hit._source._id = hit._id;
hit._source._type = hit._type;
10 years ago
return hit._source;
});
}
// fire callback
return cb( null, docs );
});
}
module.exports = service;