Skip to content

Instantly share code, notes, and snippets.

@MikeyBurkman
Created June 10, 2016 22:04
Show Gist options
  • Save MikeyBurkman/64f16059402df7616fad43975f348bef to your computer and use it in GitHub Desktop.
Save MikeyBurkman/64f16059402df7616fad43975f348bef to your computer and use it in GitHub Desktop.
Clear Elasticsearch Index
#! /usr/bin/env cecil
// Usage:
// ./esclear.js http://your-elasticsearch-host.com
var elasticsearch = include('elasticsearch', '^11.0.0');
var index = 'testindex';
var host = process.argv[2];
if (!host) {
console.log('Host needs to be specified as first argument');
process.exit(0);
}
var client = new elasticsearch.Client({
host: host,
log: 'info'
});
clear()
.then(createIndex)
.then(function() {
console.log('Finished!');
});
/////////
function clear() {
return client.indices.delete({
index: index
})
.catch(function(err) {
if (err.message.indexOf('IndexMissingException') === -1) {
throw err;
}
});
}
function createIndex() {
return client.indices.create({
index: index,
body: {
mappings: {
'testType': {
properties: {
name: { type: 'string', index: 'not_analyzed' },
env: { type: 'string', index: 'not_analyzed' },
time: { type: 'integer' },
created: { type: 'date'}
}
}
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment