Skip to content

Instantly share code, notes, and snippets.

@johnywith1n
Created February 15, 2014 01:31
Show Gist options
  • Save johnywith1n/9013097 to your computer and use it in GitHub Desktop.
Save johnywith1n/9013097 to your computer and use it in GitHub Desktop.
Issue with elasticsearch node client.close
'use strict';
var elasticsearch = require('elasticsearch');
var deferred = require('deferred');
function Foo () {
this.client = new elasticsearch.Client({
hosts : 'localhost:9200',
log: 'error'
});
}
Foo.prototype.close = function() {
this.client.close();
}
Foo.prototype.createIndex = function (indexName) {
var d = deferred();
this.client.indices.create({
index: indexName,
body: {
"mappings" : {
"myType" : {
"_id" : {
"path" : "id"
}
}
}
}
}).done(function(body) {
d.resolve(body);
}, function(error) {
d.reject(new Error('Unable to create user index. Error: ' + JSON.stringify(error)));
});
return d.promise;
}
Foo.prototype.deleteIndex = function (indexName) {
var d = deferred();
this.client.indices.delete({index:indexName}).done(function(body) {
d.resolve(body);
}, function(error) {
d.reject(new Error('Unable to delete user index. Error: ' + JSON.stringify(error)));
});
return d.promise;
}
Foo.prototype.doesIndexExist = function (indexName) {
var d = deferred();
this.client.indices.exists({index:indexName}).done(function(body) {
d.resolve(body);
}, function(error) {
d.reject(new Error('Unable to check existence of user index. Error: ' + JSON.stringify(error)));
});
return d.promise;
}
Foo.prototype.resetIndex = function (indexName) {
var d = deferred();
var _this = this;
this.doesIndexExist(indexName).then(function(exists) {
if(exists) {
_this.deleteIndex(indexName).then(function(body) {
return _this.createIndex(indexName);
}).done(function(body) {
d.resolve(true);
}, function(error) {
d.reject(error);
});
} else {
_this.createIndex(indexName).then(function(body) {
d.resolve(true);
}, function(error) {
d.reject(error);
});
}
}, function (error) {
d.reject(error);
});
return d.promise;
}
var a = new Foo();
// doesn't terminate if indices already exist
a.resetIndex("test").then(function(b) {
return a.resetIndex("test1");
}, function(e) {
console.error(e);
}).done(function(b) {
console.log(b);
a.close();
}, function(e) {
console.error(e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment