Skip to content

Instantly share code, notes, and snippets.

@konsumer
Created March 19, 2012 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save konsumer/2128623 to your computer and use it in GitHub Desktop.
Save konsumer/2128623 to your computer and use it in GitHub Desktop.
/**
* 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);
}
});
}
};
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