UNPKG

1.3 kBJavaScriptView Raw
1
2var debug = require('./_debug');
3// debug.enabled = true;
4
5function mgetExtractor( cb ){
6
7 // es callback
8 var extractor = function( error, resp ){
9
10 // Response logger
11 debug.resp( resp );
12
13 // Handle errors from the es client
14 if( error ) return cb( error );
15
16 // Handle errors returned in the body
17 if( 'object' == typeof resp && resp.hasOwnProperty('error') ) return cb( resp.error );
18
19 // Check the response is valid ang contains at least one records
20 else if( 'object' == typeof resp && resp.hasOwnProperty('docs') &&
21 Array.isArray( resp.docs ) && resp.docs.length ){
22
23 var invalid = resp.docs.some( function( doc ){
24 return !doc.found;
25 });
26
27 if( invalid ){
28 debug.message( 'mgetExtractor: one or more node ids not found', resp.docs );
29 return cb( 'one or more node ids not found' );
30 }
31
32 var results = resp.docs.map( function( doc ){
33 var source = doc._source;
34 source._index = doc._index;
35 source._type = doc._type;
36 source._id = doc._id;
37 return source;
38 });
39
40 return cb( undefined, results );
41 }
42
43 // The query returned 0 results
44 else return cb();
45 return cb( 'an unexpected error occured' );
46 }
47
48 return extractor;
49}
50
51module.exports = mgetExtractor;
\No newline at end of file