Skip to content

Commit ae4010a

Browse files
author
Gaurang Pansare
committed
major changes:
-- fixed issue in case of target type different from source type -- changed default path in sample-config to output/data.json
1 parent 0f64c56 commit ae4010a

File tree

4 files changed

+33
-6
lines changed

4 files changed

+33
-6
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Custom
22
config.js
3+
output
34

45
# Logs
56
logs

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Since Node.js is single-threaded, `producer` and `consumer` cannot simultaneousl
77
### Usage:
88
1. Clone the repo.
99
2. Run `npm install` in the directory.
10-
3. Add `config.js` using [`config-sample.js`][1]
10+
3. Add `config.js` using [`config-sample.js`][1]. Exercise caution while setting source and target elastic hosts.
1111
4. (Optional) Set appropriate values in [`options.js`][2]
1212
5. Run the script
1313
```

config-sample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ module.exports = Object.freeze({
1515
"type": "_doc",
1616

1717
//if options.consume.byFile is not set, this will be ignored
18-
"filePath": "data.json",
18+
"filePath": "output/data.json",
1919

2020
//if options.consume.byElastic is not set, this will be ignored
2121
"host": "localhost:9200",

elastic.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

130156
module.exports = {Elastic}

0 commit comments

Comments
 (0)