Comparing version 0.0.6 to 0.0.7
92
index.js
@@ -7,2 +7,26 @@ | ||
/** | ||
* @class Etk | ||
* Etk client. Appended to Elastic Search instance with "tk" namespace. | ||
* | ||
* @example | ||
* elastic = require('elasticsearch'); | ||
* 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) { | ||
* ... | ||
* }); | ||
* | ||
* @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>raw_response</strong>: {bool} Returns elasticsearch response object without any modification. Default is <strong>false</strong>. </p> | ||
* <strong>raw_error</strong>: {bool} Returns elasticsearch error object without any modification. Default is <strong>false</strong>. </p> | ||
* <strong>insert_time</strong>: {bool} Inserts time field for every stored object to elasticsearch. Default is <strong>false</strong>. </p> | ||
* <strong>time_field</strong>: {string} Timestamp name which is auto inserted to all stored fields. | ||
* Default is <strong>@timestamp</strong> which is Logstash compatible. </p> | ||
*/ | ||
function Etk(client, opt) { | ||
@@ -12,2 +36,9 @@ this.client = client; | ||
this.client.tk = this.client.tk || { | ||
_query : function (query_body, opt) { | ||
var query = {index: this.index, type: this.type, body: query_body}; | ||
if (opt) { | ||
_.merge(query, opt); | ||
} | ||
return query; | ||
}, | ||
@@ -30,13 +61,10 @@ /** | ||
* @param cb {function} Callback function of signature (err, resp) | ||
* @param [opt] {JSON} Additional options for search like "sort" for sorted results. | ||
* @param [opt] {json} Additional options for search like "sort" for sorted results. | ||
* Pass options as documented in elasticsearch. | ||
*/ | ||
search: function (key, value, cb) { | ||
search: function (key, value, cb, opt) { | ||
var esq = new Esq(); | ||
esq.query("query", "filtered", "query", "match", key, value); | ||
var query_body = esq.getQuery(); | ||
var query = {index: this.index, type: this.type, body: query_body}; | ||
if (opt) { | ||
_.merge(query, opt); | ||
} | ||
var query = this._query(query_body, opt); | ||
this.client.search(query, cb); | ||
@@ -62,3 +90,3 @@ }, | ||
* @param cb {function} Callback function of signature (err, resp) | ||
* @param [opt] {JSON} Additional options for search like "sort" for sorted results. | ||
* @param [opt] {json} Additional options for search like "sort" for sorted results. | ||
* Pass options as documented in elasticsearch. | ||
@@ -72,6 +100,3 @@ */ | ||
var query_body = esq.getQuery(); | ||
var query = {index: this.index, type: this.type, body: query_body}; | ||
if (opt) { | ||
_.merge(query, opt); | ||
} | ||
var query = this._query(query_body, opt); | ||
this.client.search(query, cb); | ||
@@ -129,12 +154,24 @@ }, | ||
*/ | ||
deleteAll: function(cb) { | ||
deleteAll: function(cb, opt) { | ||
var esq = new Esq(); | ||
esq.query("query", "filtered", "query", "match_all", "", ""); | ||
var query = esq.getQuery(); | ||
this.client.deleteByQuery({ | ||
index: this.index, | ||
type: this.type, | ||
body: query | ||
}, cb ); | ||
var query_body = esq.getQuery(); | ||
var query = this._query(query_body, opt); | ||
this.client.deleteByQuery(query, this._deleteAllCb(cb, this)); | ||
}, | ||
_deleteAllCb: function (cb, self) { | ||
var self = self; | ||
return function(err, resp) { | ||
if (err && !self.raw_error) { | ||
if (err["status"] == "404") { | ||
// If index is not found deleting sould not return error. | ||
cb(false, {}); | ||
} else { | ||
cb(err, resp); | ||
} | ||
} else { | ||
cb(err, resp); | ||
} | ||
} | ||
}, | ||
/** | ||
@@ -154,3 +191,3 @@ * Get all items of Etk client | ||
* @param cb {function} Callback function of signature (err, resp) | ||
* @param [opt] {JSON} Additional options for search like "sort" for sorted results. | ||
* @param [opt] {json} Additional options for search like "sort" for sorted results. | ||
* Pass options as documented in elasticsearch. | ||
@@ -162,6 +199,3 @@ */ | ||
var query_body = esq.getQuery(); | ||
var query = {index: this.index, type: this.type, body: query_body}; | ||
if (opt) { | ||
_.merge(query, opt); | ||
} | ||
var query = this._query(query_body, opt); | ||
this.client.search(query, cb ); | ||
@@ -173,10 +207,14 @@ } | ||
this.client.tk.client = this.client; | ||
// 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; | ||
// Default time field is Logstash compatible | ||
this.client.tk.time_field = opt.time_field || "@timestamp"; | ||
// Default searches "all" available index | ||
this.client.tk.index = opt.index || "*"; | ||
// Default searches "all" available types | ||
this.client.tk.type = opt.type || "*"; | ||
// Return the extended object | ||
return this.client; | ||
} |
{ | ||
"name": "etk", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Elastic search tool kit.", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -11,3 +11,3 @@ var test = require('tape'); | ||
var client_1 = new etk(client, {index: "myindex", type: "mytype"}); | ||
var client_1 = new etk(client, {index: "myindex", type: "mytype", raw_error: true}); | ||
@@ -14,0 +14,0 @@ /* |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3170364
50
122818