Skip to content

Instantly share code, notes, and snippets.

Created March 19, 2012 23:54
What would you like to do?
* 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/')
*/ = 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"});
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(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')
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){["_source"], argv["index-out"], hit["_type"], function(err, record){
if (!err){
}, 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