1 |
|
2 | var debug = require('./_debug');
|
3 |
|
4 |
|
5 | function mgetExtractor( cb ){
|
6 |
|
7 |
|
8 | var extractor = function( error, resp ){
|
9 |
|
10 |
|
11 | debug.resp( resp );
|
12 |
|
13 |
|
14 | if( error ) return cb( error );
|
15 |
|
16 |
|
17 | if( 'object' == typeof resp && resp.hasOwnProperty('error') ) return cb( resp.error );
|
18 |
|
19 |
|
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 |
|
44 | else return cb();
|
45 | return cb( 'an unexpected error occured' );
|
46 | }
|
47 |
|
48 | return extractor;
|
49 | }
|
50 |
|
51 | module.exports = mgetExtractor; |
\ | No newline at end of file |