This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Save a record, directly to elasticsearch | |
* @param {Object} record Record to be saved | |
* @param {String} index_name Index to save to | |
* @param {String} type type to save to | |
* @param {Function} callback called on complete: req, result | |
* @param {String} prefix url for DB ('http://localhost:9200/') | |
*/ | |
exports.save = function(record, index_name, type, callback, prefix, id){ | |
prefix = prefix || 'http://localhost:9200/'; | |
if (!type || !index_name ){ | |
callback({message: "you need to set both type and index"}); | |
}else{ | |
var url = prefix + index_name + '/' + type; | |
if (id){ | |
url += "/" + id; | |
} | |
// console.log("PUT", url, record); | |
request({"method": "PUT", "uri": url, "body": JSON.stringify(record)}, function(error, response, body){ | |
if (error){ | |
callback(error); | |
}else{ | |
callback(false, body); | |
} | |
}); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var estools = require('estools'); | |
var argv = require('optimist') | |
.usage('Replicate data from one Elasticsearch index to another.\nUsage: $0 -i [index-name] -o [index-name]') | |
.demand('index-in').alias('index-in', 'i').describe('index-in', 'Input index') | |
.default('db-in','http://localhost:9200/').describe('db-in', 'Input database') | |
.demand('index-out').alias('index-out', 'o').describe('index-out', 'Output index') | |
.default('db-out','http://localhost:9200/').describe('db-out', 'Output database') | |
.argv; | |
estools.create(argv["db-out"], argv["index-out"], {"settings":{"index":{"number_of_shards":"1","number_of_replicas":"0"}}}, function(){ | |
estools.getAll(function(err, hit){ | |
if (!err){ | |
estools.save(hit["_source"], argv["index-out"], hit["_type"], function(err, record){ | |
if (!err){ | |
console.log(record); | |
}else{ | |
console.log(arguments); | |
} | |
}, argv["db-out"], hit["_id"]); | |
} | |
}, argv["db-in"], argv["index-in"]); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment