Skip to content

Instantly share code, notes, and snippets.

@saltukalakus
Last active October 6, 2015 17:08
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 saltukalakus/699d349dcee11118b8f0 to your computer and use it in GitHub Desktop.
Save saltukalakus/699d349dcee11118b8f0 to your computer and use it in GitHub Desktop.
Elastic search tests for API:1.7
var elasticsearch = require('elasticsearch');
var client = elasticsearch.Client({
hosts: [
'localhost:9200'
]
});
var tk = {
_bulkArray: function(idx, type, data) {
var bulk_formed = [];
for (var i = 0, len = data.length; i < len; i++) {
bulk_formed.push({index: {_index:idx, _type: type}});
bulk_formed.push(data[i]);
}
return bulk_formed;
},
bulkInsert: function(idx, type, data) {
var bulk_body = this._bulkArray(idx, type, data);
client.bulk({
body: bulk_body
}, function (err, resp) {
if (err)
console.log("ERR:" + err);
if (resp)
console.log(resp);
})
}
};
(function test_bulk() {
var test_array= [{foo:1, bar:2, baz: "John", "@timestamp": new Date().toISOString()},
{foo:2, bar:4, baz: "Dough", "@timestamp": new Date().toISOString()},
{foo:0, bar:5, baz: "Jane", "@timestamp": new Date().toISOString()}];
tk.bulkInsert("myindex", "mytype", test_array);
})();
var elasticsearch = require('elasticsearch');
var Esq = require('esq');
var client = elasticsearch.Client({
hosts: [
'localhost:9200'
]
});
var tk = {
// Number of items in a given index
countIdx: function(idx, cb) {
client.count({
index: idx
}, cb );
},
// Number of items for a matching query in a given index
countIdxQ: function(idx, key, val, cb) {
var esq = new Esq();
if (val instanceof Array)
esq.query('query', 'filtered', 'filter', 'terms', key, val);
else
esq.query('query', 'filtered', 'filter', 'terms', key, [val]);
var query = esq.getQuery();
client.count({
index: idx,
body: query
}, cb);
}
};
(function test_count() {
function count_idx_cb(err, resp) {
if (err)
console.log(err);
else
console.log(resp.count);
}
tk.countIdx("myindex", count_idx_cb);
tk.countIdxQ("myindex", "foo", 2, count_idx_cb);
tk.countIdxQ("myindex", "foo", [1,2], count_idx_cb);
})();
var elasticsearch = require('elasticsearch');
var Esq = require('esq');
var client = elasticsearch.Client({
hosts: [
'localhost:9200'
]
});
var tk = {
deleteAll: function(idx, type, cb) {
var esq = new Esq();
esq.query("query", "filtered", "query", "match_all", "", "");
var query = esq.getQuery();
console.log(JSON.stringify(query));
client.deleteByQuery({
index: idx,
type: type,
body: query
}, cb );
}
};
(function test_delete() {
function delete_all_cb(err, resp) {
if (err)
console.log(err);
else
console.log(resp.count);
}
tk.deleteAll("myindex", "mytype", delete_all_cb);
})();
var elasticsearch = require('elasticsearch');
var Esq = require('esq');
var client = elasticsearch.Client({
hosts: [
'localhost:9200'
]
});
client = Etk(client, {"index": "myindex", "type": "mytype"});
function Etk(client, opt) {
this.client = client;
this.client.tk = this.client.tk || {
// Quick search of items
searchQ: function (param, cb) {
this.client.search({
index: this.index,
type: this.type,
q: param},
cb);
},
search: function (key, value, cb) {
var esq = new Esq();
esq.query("query", "filtered", "query", "match", key, value);
var query = esq.getQuery();
this.client.search({
body: query},
cb);
},
searchLastDays: function (key, value, days, cb) {
var esq = new Esq();
var search_days = "now-" + days.toString() + "d/d";
esq.query("query", "filtered", "query", "match", key, value);
esq.query("query", "filtered", "filter", "range", this.time_field, "gte", search_days);
var query = esq.getQuery();
this.client.search({
body: query},
cb);
},
searchBetween: function (key, value, start, end, cb) {
var json_data = {"query": {
"filtered": {
"query": {
"match": {
"foo": 1
}
},
"filter": {
"numeric_range": {
"@timestamp": {
"lt": "2015-06-30",
"gte": "2015-06-01"
}}}
}
}};
this.client.search({
body: json_data},
cb);
}
};
this.client.tk.client = this.client;
this.client.tk.time_field = opt.time_field || "@timestamp"; // Default time field is Logstash compatible
this.client.tk.index = opt.index || "*";
this.client.tk.type = opt.type || "*";
return this.client;
}
(function test_search() {
function search_cb(err, resp) {
console.log("cb fire...");
if (err)
console.log(err);
else
console.log(JSON.stringify(resp));
}
client.tk.search("foo", 1, search_cb);
client.tk.searchLastDays("foo", 1, 1, search_cb);
client.tk.searchBetween("foo", 1, "2015-06-01", "2015-09-30", search_cb);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment