Permalink
Browse files

add bulk e some options to order query

  • Loading branch information...
1 parent 5dc96ea commit 8f7d9bf8151420d91979d803d2e7dc3aae114576 Pedro Medeiros committed Sep 10, 2012
Showing with 45 additions and 8 deletions.
  1. +45 −8 lib/elastico.js
View
53 lib/elastico.js
@@ -13,10 +13,11 @@ Elastico = function(){
};
Elastico.prototype = {
+
getIndex: function (index, type, key, callback){
var req = http.request(this.options.protocol + "://" + this.options.host + ":" +
this.options.port + "/" + index + "/" + type + "/" + key, function(res) {
- var jsonData = []
+ var jsonData = [];
res.on('data',function(chucked){
jsonData.push(chucked);
});
@@ -28,11 +29,20 @@ Elastico.prototype = {
req.method = 'GET';
req.end();
},
-
- getAll: function (index, type, callback){
- var req = http.request(this.options.protocol + "://" + this.options.host + ":" +
- this.options.port + "/" + index + "/" + type + "/_search?size=9001", function(res) {
- var jsonData = []
+
+ getAll: function (index, type, callback, extras){
+ var body = JSON.stringify(extras);
+ var options = {
+ host: this.options.host,
+ port: this.options.port,
+ path: index + "/" + type + "/_search",
+ method: 'GET',
+ headers: {
+ 'Content-Type': 'application/json',
+ }
+ };
+ var req = http.request(options, function(res) {
+ var jsonData = [];
res.on('data',function(chucked){
jsonData.push(chucked);
});
@@ -41,7 +51,10 @@ Elastico.prototype = {
callback(JSON.parse(jsonData));
});
});
- req.method = 'GET';
+ if(body !== undefined){
+ req.setHeader('Content-Length', body.length.toString() );
+ req.write(JSON.stringify(extras));
+ }
req.end();
},
@@ -57,7 +70,7 @@ Elastico.prototype = {
headers: {
'Content-Type': 'application/json'
}
- };
+ }
var req = http.request(options, function(res){
if (callback != undefined){
if (res.statusCode < 300 && res.statusCode >= 200){
@@ -140,7 +153,31 @@ Elastico.prototype = {
});
req.method = "DELETE";
req.end();
+ },
+
+ bulk: function(index, type, values, callback){
+ var options = {
+ host: this.options.host,
+ port: this.options.port,
+ path: "/" + index + "/" + type + "/_bulk?refresh=true",
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ };
+ var req = http.request(options, function(res){
+ if (callback != undefined){
+ if (res.statusCode < 300 && res.statusCode >= 200){
+ callback(true);
+ } else{
+ callback(false);
+ }
+ }
+ });
+ req.write(values);
+ req.end();
}
+
};
module.exports.Elastico = Elastico;

0 comments on commit 8f7d9bf

Please sign in to comment.