@@ -15,6 +15,14 @@ class Elastic{
1515 }
1616 this . batches = 0 ;
1717 this . _scroll_id = null ;
18+
19+ this . _targetConfigCheckIf = property => flag => {
20+ switch ( flag ) {
21+ case 'given' : return ! ! config . target [ property ] ;
22+ case 'same' : return config . target [ property ] === config . source [ property ] ;
23+ default : throw new Error ( `Error with mapping: ${ property } ` ) ;
24+ }
25+ }
1826 }
1927
2028 async init ( ) {
@@ -78,7 +86,6 @@ class Elastic{
7886 return accumulator . concat ( [ { index} , hit . _source ] )
7987 } , [ ] ) ;
8088
81- //console.log(body);
8289 console . log ( 'Elastic Doc Count' , this . target . doc_count . elastic ) ;
8390 return this . targetClient . bulk ( { body} ) ;
8491 }
@@ -110,21 +117,40 @@ class Elastic{
110117 }
111118
112119 async _transferMapping ( source , target ) {
113- let mapping = await source . indices . getMapping ( {
120+ let sourceMapping = await source . indices . getMapping ( {
114121 index : config . source . index ,
115122 type : config . source . type
116123 } )
117- console . log ( "Mapping: " , JSON . stringify ( mapping ) ) ;
124+
125+ let mapping = this . _buildMappingFrom ( sourceMapping ) ;
118126
119127 if ( ! await target . indices . exists ( { index : config . target . index } ) )
120128 await target . indices . create ( { index : config . target . index } ) ;
121129
122130 return await target . indices . putMapping ( {
123131 index : config . target . index ,
124132 type : config . target . type ,
125- body : mapping [ config . source . index ] . mappings
133+ body : mapping
126134 } ) ;
127135 }
136+
137+ //sourceMapping:
138+ //{<index>: {"mappings": {<type>: {<the mapping>}}}}
139+ //To return:
140+ //{<type>: <mapping>}
141+ _buildMappingFrom ( sourceMapping ) {
142+ let base = { } ;
143+ let type = Object . assign ( { } , sourceMapping [ config . source . index ] . mappings [ config . source . type ] ) ;
144+
145+ //if target 'type' given in config is different from the type in source elastic, change it
146+ if ( this . _targetConfigCheckIf ( 'type' ) ( 'given' ) && ! this . _targetConfigCheckIf ( 'type' ) ( 'same' ) )
147+ base [ config . target . type ] = type ;
148+ else
149+ base [ config . source . type ] = type ;
150+
151+ //base = {<type>: <mapping>}
152+ return base ;
153+ }
128154}
129155
130156module . exports = { Elastic}
0 commit comments