Comparing version 0.0.8 to 0.1.0
@@ -37,2 +37,3 @@ var fs = require('fs'); | ||
gulp.task('api_document', shell.task([ | ||
'rm -Rf ./out', | ||
'cp ./confs/jsdoc/jsdoc-conf.json ./node_modules/jsdoc/conf.json', | ||
@@ -77,4 +78,22 @@ /*jshint multistr: true */ | ||
// Document class web site patches | ||
gulp.task('patch_class_document', ['patch_index_html_document'], function(){ | ||
gulp.src(['out/Etk.html']) | ||
.pipe(replace("</body>\n</html>", | ||
"<style> .type-signature { font-size:60px;} .signature {color: orange;} .page-title {display: none;}</style>" + | ||
"<script type='text/javascript' src='scripts/jquery-watch-element.js'></script>" + | ||
"<script>$( document ).ready(function(){$('.toc-h1').waitUntilExists(" + | ||
"function(){$('.toc-h1').hide(); " + | ||
"$('.toc-h2').hide()}, " + | ||
"false, true);});" + | ||
"</script>" + | ||
'<a href="https://github.com/saltukalakus/etk"><img style="position: fixed; top: 0; left: 0; border: 0; z-index: 999999" src="https://camo.githubusercontent.com/121cd7cbdc3e4855075ea8b558508b91ac463ac2/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_green_007200.png"></a>' + | ||
"</body>\n</html>")) | ||
.pipe(replace("Global", "API")) | ||
.pipe(gulp.dest('out')); | ||
}); | ||
// Document api web site patches | ||
gulp.task('patch_api_document', ['patch_index_html_document'], function(){ | ||
gulp.task('patch_api_document', ['patch_class_document'], function(){ | ||
gulp.src(['out/global.html']) | ||
@@ -81,0 +100,0 @@ .pipe(replace("</body>\n</html>", |
85
index.js
@@ -9,18 +9,19 @@ | ||
* @class Etk | ||
* Etk client. Appended to Elastic Search instance with "tk" namespace. | ||
* | ||
* @example | ||
* elastic = require('elasticsearch'); | ||
* Etk = require('etk'); | ||
* Etk = require('etk'); | ||
* | ||
* var client = elastic.Client({hosts: ['localhost:9200']}); | ||
* client = Etk(client, {index: "myindex", type: "mytype"}); | ||
* client.tk.search("foo", "bar", function (err, resp) { | ||
* ... | ||
* }); | ||
* var tk = new Etk(client, {index: "myindex", type: "mytype"}); | ||
* | ||
* tk.search("foo", "bar", function (err, resp) { | ||
* ... | ||
* }); | ||
* | ||
* @param client {object} elasticsearch instance | ||
* @param [opt] {json} Configuration options for Etk instance. </p> | ||
* | ||
* <strong>index</strong> {string} Index which Etk operates on. Default is <strong>*</strong> </p> | ||
* <strong>type</strong>: {string} Type which Etk operated on. Default is <strong>*</strong> </p> | ||
* <strong>index</strong> {string} Index which Etk operates on. Default is <strong>search all</strong> </p> | ||
* <strong>type</strong>: {string} Type which Etk operated on. Default is <strong>search all</strong> </p> | ||
* <strong>raw_response</strong>: {bool} Returns elasticsearch response object without any modification. Default is <strong>false</strong>. </p> | ||
@@ -33,5 +34,6 @@ * <strong>raw_error</strong>: {bool} Returns elasticsearch error object without any modification. Default is <strong>false</strong>. </p> | ||
function Etk(client, opt) { | ||
this.client = client; | ||
// Add tk namespace to elastic search object. | ||
this.client.tk = this.client.tk || { | ||
// Insert elasticsearch with "e" namespace | ||
this.e = client; | ||
// Insert Etk API with "tk" namespace | ||
this.tk = { | ||
_query : function (query_body, opt) { | ||
@@ -46,3 +48,5 @@ var query = {index: this.index, type: this.type, body: query_body}; | ||
/** | ||
* Searches the key-value pair of Etk client. Returns result to the callback | ||
* @method | ||
* @name search | ||
* @description Searches the key-value pair of Etk client. Returns result to the callback | ||
* function. | ||
@@ -52,6 +56,6 @@ * | ||
* elastic = require('elasticsearch'); | ||
* Etk = require('etk'); | ||
* Etk = require('etk'); | ||
* var client = elastic.Client({hosts: ['localhost:9200']}); | ||
* client = Etk(client, {index: "myindex", type: "mytype"}); | ||
* client.tk.search("foo", "bar", function (err, resp) { | ||
* var tk = new Etk(client, {index: "myindex", type: "mytype"}); | ||
* tk.search("foo", "bar", function (err, resp) { | ||
* ... | ||
@@ -75,3 +79,5 @@ * }, {"sort":"FIELD_NAME"}); | ||
/** | ||
* Searches the key-value pair for the last number of days of Etk client | ||
* @method | ||
* @name searchLastDays | ||
* @description Searches the key-value pair for the last number of days of Etk client | ||
* Returns result to the callback function. | ||
@@ -83,4 +89,4 @@ * | ||
* var client = elastic.Client({hosts: ['localhost:9200']}); | ||
* client = Etk(client, {index: "myindex", type: "mytype"}); | ||
* client.tk.searchLastDays("foo", "bar", 10, function (err, resp) { | ||
* var tk = new Etk(client, {index: "myindex", type: "mytype"}); | ||
* tk.searchLastDays("foo", "bar", 10, function (err, resp) { | ||
* ... | ||
@@ -115,3 +121,5 @@ * }, {"sort":"FIELD_NAME"}); | ||
/** | ||
* Inserts Json arrays in bulk mode to the Etk client | ||
* @method | ||
* @name bulkInsert | ||
* @description Inserts Json arrays in bulk mode to the Etk client | ||
* | ||
@@ -122,3 +130,3 @@ * @example | ||
* var client = elastic.Client({hosts: ['localhost:9200']}); | ||
* client = Etk(client, {index: "myindex", type: "mytype"}); | ||
* var tk = new Etk(client, {index: "myindex", type: "mytype"}); | ||
* | ||
@@ -129,7 +137,7 @@ * var test_array= [{foo:1, bar:2, baz: "John", "@timestamp": new Date().toISOString()}, | ||
* | ||
* client.tk.bulkInsert(test_array, function (err, resp) { | ||
* tk.bulkInsert(test_array, function (err, resp) { | ||
* ... | ||
* }); | ||
* | ||
* @param data {json} Bulk arbitrary json data | ||
* @param data {array} Array of bulk arbitrary json data | ||
* @param cb {function} Callback function of signature (err, resp) | ||
@@ -144,3 +152,5 @@ */ | ||
/** | ||
* Delete all items of Etk client | ||
* @method | ||
* @name deleteAll | ||
* @description Delete all items of Etk client | ||
* | ||
@@ -151,5 +161,5 @@ * @example | ||
* var client = elastic.Client({hosts: ['localhost:9200']}); | ||
* client = Etk(client, {index: "myindex", type: "mytype"}); | ||
* var tk = Etk(client, {index: "myindex", type: "mytype"}); | ||
* | ||
* client.tk.deleteAll(function (err, resp) { | ||
* tk.deleteAll(function (err, resp) { | ||
* ... | ||
@@ -183,3 +193,5 @@ * }); | ||
/** | ||
* Get all items of Etk client | ||
* @method | ||
* @name listAll | ||
* @description Get all items of Etk client. | ||
* | ||
@@ -190,5 +202,5 @@ * @example | ||
* var client = elastic.Client({hosts: ['localhost:9200']}); | ||
* client = Etk(client, {index: "myindex", type: "mytype"}); | ||
* var tk = new Etk(client, {index: "myindex", type: "mytype"}); | ||
* | ||
* client.tk.listAll(function (err, resp) { | ||
* tk.listAll(function (err, resp) { | ||
* ... | ||
@@ -230,15 +242,14 @@ * }, {"sort":"FIELD_NAME"}); | ||
// Store elastic search client for etk use | ||
this.client.tk.client = this.client; | ||
this.tk.client = this.e; | ||
// Etk options | ||
this.client.tk.index = opt.index || "*"; | ||
this.client.tk.type = opt.type || "*"; | ||
this.client.tk.raw_response = opt.raw_response || false; | ||
this.client.tk.raw_error = opt.raw_error || false; | ||
this.client.tk.insert_time = this.client.tk.insert_time || false; | ||
this.tk.index = opt.index || "*"; | ||
this.tk.type = opt.type || "*"; | ||
this.tk.raw_response = opt.raw_response || false; | ||
this.tk.raw_error = opt.raw_error || false; | ||
this.tk.insert_time = opt.insert_time || false; | ||
// Default time field is Logstash compatible | ||
this.client.tk.time_field = opt.time_field || "@timestamp"; | ||
this.tk.time_field = opt.time_field || "@timestamp"; | ||
// Return the extended object | ||
return this.client; | ||
return this.tk; | ||
} |
{ | ||
"name": "etk", | ||
"version": "0.0.8", | ||
"version": "0.1.0", | ||
"description": "Elastic search tool kit.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -12,11 +12,22 @@ ## ElasticSearch Tool Kit | ||
// Elasticsearch instance is initialized with some common options | ||
var client = elastic.Client({hosts: ['localhost:9200']}); | ||
client_1 = Etk(client, {index: "my_index", type: "my_type"}); | ||
client_1.tk.search("foo", "bar", function (err, resp) { | ||
// First Etk sample instance (tk_1) makes Elasticsearch calls against | ||
// index = my_index and type = my_type using elasticsearch instance | ||
var tk_1 = Etk(client, {index: "my_index", type: "my_type"}); | ||
tk_1.search("foo", "bar", function (err, resp) { | ||
... | ||
}); | ||
client_2 = Etk(client, {index: "another_index", type: "another_type"}); | ||
client_2.tk.search("baz", "bar", function (err, resp) { | ||
// Second Etk sample instance passes elasticsearch error and response | ||
// messages untouched to application. See class documentation for full | ||
// list of Etk options you can configure | ||
var tk_2 = Etk(client, {index: "another_index", | ||
type: "another_type", | ||
raw_response: true, | ||
raw_error: true}); | ||
tk_2.search("baz", "bar", function (err, resp) { | ||
// Application should handle raw error and response messages | ||
// received from elasticsearch directly. | ||
... | ||
@@ -30,10 +41,9 @@ }); | ||
* Extents the official [elasticsearch](https://github.com/elastic/elasticsearch-js) project with easy to use, well documented function calls. | ||
* Parses elasticsearch error and response messages in useful ways. Yet you have the option to receive them as they are with configuration options. | ||
* Etk library is inserted into elasticsearch with "tk" namespace. | ||
* Uses same elasticsearch instance in multiple Etk clients. | ||
* All API calls are tested against the latest elasticsearch release. | ||
* elasticsearch can be used alongside without any change. | ||
* Parses [elasticsearch](https://github.com/elastic/elasticsearch-js) error and response messages in useful ways. Yet you have the option to receive them as they are with configuration options. | ||
* Uses same [elasticsearch](https://github.com/elastic/elasticsearch-js) instance in multiple Etk clients. | ||
* All API calls are tested against the latest [elasticsearch](https://github.com/elastic/elasticsearch-js) release. | ||
* [Elasticsearch](https://github.com/elastic/elasticsearch-js) can be used alongside without any change. | ||
## API Documentation with Examples | ||
[Site-Link](http://saltukalakus.github.io/etk) | ||
[Site Link](http://saltukalakus.github.io/etk) | ||
@@ -40,0 +50,0 @@ ## People |
@@ -11,39 +11,48 @@ var test = require('tape'); | ||
var client_1 = new Etk(client, {index: "myindex", type: "mytype"}); | ||
// First test data set is stored in myindex-mytype index/type store | ||
var tk_1 = new Etk(client, {index: "myindex", type: "mytype"}); | ||
/* | ||
//var client_2 = new etk(client, {index: "myin3dex2", type: "myty3pe2"}); | ||
// Second test data set is stored in myuser-myname index/type store | ||
// In this set etk instance passes elasticsearch error and response messages | ||
// untouched | ||
var tk_2 = new Etk(client, {index: "another_index", | ||
type: "another_type", | ||
raw_response: true, | ||
raw_error: false}); | ||
client_1.tk.searchLastDays("foo", 1, 20, function (err, resp) { | ||
console.log("1"); | ||
console.log(JSON.stringify(resp)); | ||
}); | ||
var test_array_1= [{foo:1, bar:2, baz: "John", "@timestamp": new Date().toISOString(), "id" : 1}, | ||
{foo:2, bar:4, baz: "Dough", "@timestamp": new Date().toISOString(), "id": 2}, | ||
{foo:0, bar:5, baz: "Jane", "@timestamp": new Date().toISOString(), "id": 3}]; | ||
client_1.tk.search("foo", 1, function (err, resp) { | ||
console.log("2"); | ||
console.log(JSON.stringify(resp)); | ||
}); | ||
var test_array_2= [{woman:"Mary", man:"John", "id" : 1}, | ||
{woman:"Curry", man:"Tesla", "id" : 2}, | ||
{woman:"Jane", man:"Bob", "id" : 3}]; | ||
client_2.tk.searchLastDays("foo", 1, 20, function (err, resp) { | ||
console.log("3"); | ||
console.log(JSON.stringify(resp)); | ||
test("Delete data set - 1", function(t) { | ||
function cb (err, resp) { | ||
if (err) | ||
t.fail("ERR: " + JSON.stringify(err)); | ||
} | ||
tk_1.deleteAll(function(err, resp){ | ||
if (err) { | ||
t.fail("Data set could not be cleared. ERR: " + JSON.stringify(err)); | ||
} | ||
}); | ||
t.end(); | ||
}); | ||
client_2.tk.search("foo", 1, function (err, resp) { | ||
console.log("4"); | ||
console.log(JSON.stringify(resp)); | ||
test("Delete data set - 2", function(t) { | ||
function cb (err, resp) { | ||
if (err) | ||
t.fail("ERR: " + JSON.stringify(err)); | ||
} | ||
tk_2.deleteAll(function(err, resp){ | ||
if (err) { | ||
t.fail("Data set could not be cleared. ERR: " + JSON.stringify(err)); | ||
} | ||
}); | ||
t.end(); | ||
}); | ||
client_1.tk.search("foo", 1, function (err, resp) { | ||
console.log("5"); | ||
console.log(JSON.stringify(resp)); | ||
}); | ||
*/ | ||
var test_array= [{foo:1, bar:2, baz: "John", "@timestamp": new Date().toISOString(), "id" : 1}, | ||
{foo:2, bar:4, baz: "Dough", "@timestamp": new Date().toISOString(), "id": 2}, | ||
{foo:0, bar:5, baz: "Jane", "@timestamp": new Date().toISOString(), "id": 3}]; | ||
test("Delete data set", function(t) { | ||
test("Populate data set - 1", function(t) { | ||
function cb (err, resp) { | ||
@@ -53,6 +62,8 @@ if (err) | ||
} | ||
client_1.tk.deleteAll(function(err, resp){ | ||
tk_1.deleteAll(function(err, resp){ | ||
if (err) { | ||
t.fail("Data set could not be cleared. ERR: " + JSON.stringify(err)); | ||
} | ||
console.log("Now bulk insert"); | ||
tk_1.bulkInsert(test_array_1, cb); | ||
}); | ||
@@ -62,3 +73,3 @@ t.end(); | ||
test("Populate the data set", function(t) { | ||
test("Populate data set - 2", function(t) { | ||
function cb (err, resp) { | ||
@@ -68,3 +79,3 @@ if (err) | ||
} | ||
client_1.tk.deleteAll(function(err, resp){ | ||
tk_2.deleteAll(function(err, resp){ | ||
if (err) { | ||
@@ -74,3 +85,3 @@ t.fail("Data set could not be cleared. ERR: " + JSON.stringify(err)); | ||
console.log("Now bulk insert"); | ||
client_1.tk.bulkInsert(test_array, cb); | ||
tk_2.bulkInsert(test_array_2, cb); | ||
}); | ||
@@ -80,3 +91,3 @@ t.end(); | ||
test("Verify if the data set is successfully stored", function(t) { | ||
test("Verify if the data set is successfully stored for sample set - 1", function(t) { | ||
t.plan(3); | ||
@@ -89,6 +100,4 @@ | ||
//for (var item in resp['hits']['hits']) { | ||
for (var item in resp) { | ||
//t.equal(JSON.stringify(test_array[item]), JSON.stringify(resp['hits']['hits'][item]['_source'])); | ||
t.equal(JSON.stringify(test_array[item]), JSON.stringify(resp[item])); | ||
t.equal(JSON.stringify(test_array_1[item]), JSON.stringify(resp[item])); | ||
} | ||
@@ -98,9 +107,8 @@ } | ||
setTimeout(function() { | ||
// Give elastic search some time to index | ||
client_1.tk.listAll(cb, {"sort": "id"}); | ||
// Give elastic search some time to index newly stored data set | ||
tk_1.listAll(cb, {"sort": "id"}); | ||
}, 3000); | ||
}); | ||
/* | ||
test("Verify in raw response mode, if the data set is successfully stored", function(t) { | ||
test("Verify if the data set is successfully stored for sample set - 2", function(t) { | ||
t.plan(3); | ||
@@ -113,4 +121,5 @@ | ||
// Response is in raw form. So application should handle elasticsearch's crappy json format. | ||
for (var item in resp['hits']['hits']) { | ||
t.equal(JSON.stringify(test_array[item]), JSON.stringify(resp['hits']['hits'][item]['_source'])); | ||
t.equal(JSON.stringify(test_array_2[item]), JSON.stringify(resp['hits']['hits'][item]['_source'])); | ||
} | ||
@@ -120,9 +129,9 @@ } | ||
setTimeout(function() { | ||
// Give elastic search some time to index | ||
client_1.tk.listAll(cb, {"sort": "id", "raw_response": true}); | ||
// Give elastic search some time to index newly stored data set | ||
tk_2.listAll(cb, {"sort": "id"}); | ||
}, 3000); | ||
}); | ||
*/ | ||
test("Search the data set with success", function(t){ | ||
test("Search the data set with success for set - 1 ", function(t){ | ||
function cb(err, resp) { | ||
@@ -135,3 +144,3 @@ if (err) { | ||
setTimeout(function() { | ||
//client_1.tk.search(cb, ) | ||
//tk_1.search(cb, ) | ||
}, 3000); | ||
@@ -144,1 +153,30 @@ t.end(); | ||
}); | ||
/* | ||
tk_1.searchLastDays("foo", 1, 20, function (err, resp) { | ||
console.log("1"); | ||
console.log(JSON.stringify(resp)); | ||
}); | ||
tk_1.search("foo", 1, function (err, resp) { | ||
console.log("2"); | ||
console.log(JSON.stringify(resp)); | ||
}); | ||
tk_2.searchLastDays("foo", 1, 20, function (err, resp) { | ||
console.log("3"); | ||
console.log(JSON.stringify(resp)); | ||
}); | ||
tk_2.search("foo", 1, function (err, resp) { | ||
console.log("4"); | ||
console.log(JSON.stringify(resp)); | ||
}); | ||
tk_1.search("foo", 1, function (err, resp) { | ||
console.log("5"); | ||
console.log(JSON.stringify(resp)); | ||
}); | ||
*/ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3180164
122934
64