Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit a646e911a1d1bd8d565f64a780b77ccc4a030455 @pedrosnk committed Aug 31, 2012
Showing with 242 additions and 0 deletions.
  1. +13 −0 README.md
  2. +2 −0 index.js
  3. +146 −0 lib/elastico.js
  4. +15 −0 package.json
  5. +66 −0 test/elastico_test.js
@@ -0,0 +1,13 @@
+ELASTICO
+========
+
+Elastico is an API to easialy connect a node application with
+elastic search.
+
+
+License
+-------
+
+Elastico is released under the MIT license:
+
+http://www.opensource.org/licenses/MIT
@@ -0,0 +1,2 @@
+
+exports.Elastico = require('./lib/elastico').Elastico;
@@ -0,0 +1,146 @@
+/*
+ * Elastico is a library created to make connections with the elastic search
+ */
+
+var http = require('http');
+
+Elastico = function(){
+ this.options = {
+ host: 'localhost'
+ ,port: 9200
+ ,protocol: "http"
+ };
+};
+
+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 = []
+ res.on('data',function(chucked){
+ jsonData.push(chucked);
+ });
+ res.on('end', function(){
+ jsonData = jsonData.join('');
+ callback(JSON.parse(jsonData));
+ });
+ });
+ 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 = []
+ res.on('data',function(chucked){
+ jsonData.push(chucked);
+ });
+ res.on('end', function(){
+ jsonData = jsonData.join('');
+ callback(JSON.parse(jsonData));
+ });
+ });
+ req.method = 'GET';
+ req.end();
+ },
+
+ /*
+ * TODO: return the id of the object
+ */
+ insert: function (index, type, value, callback) {
+ var options = {
+ host: this.options.host,
+ port: this.options.port,
+ path: "/" + index + "/" + type + "?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(value);
+ req.end();
+ },
+
+ insertWithId: function (index, type, id, value, callback) {
+ var options = {
+ host: this.options.host,
+ port: this.options.port,
+ path: "/" + index + "/" + type + "/" + id + "?refresh=true",
+ method: 'PUT',
+ 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(value);
+ req.end();
+ },
+
+ remove: function(index, type, id, callback) {
+ var options = {
+ host: this.options.host,
+ port: this.options.port,
+ path: "/" + index + "/" + type + "/" + id,
+ method: 'DELETE',
+ 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.end();
+ },
+
+ createIndex: function(index, callback){
+ var req = http.request(this.options.protocol + "://" + this.options.host + ":" +
+ this.options.port + "/" + index + "?refresh=true", function(res){
+ if (callback != undefined){
+ callback(res.statusCode < 300 && res.statusCode >= 200);
+ }
+ });
+ req.method = "POST";
+ req.end();
+ },
+
+ removeIndex: function(index, callback){
+ var req = http.request(this.options.protocol + "://" + this.options.host + ":" +
+ this.options.port + "/" + index + "?refresh=true", function(res){
+ if (callback != undefined){
+ if (res.statusCode < 300 && res.statusCode >= 200){
+ callback(true);
+ } else{
+ callback(false);
+ }
+ }
+ });
+ req.method = "DELETE";
+ req.end();
+ }
+};
+
+module.exports.Elastico = Elastico;
@@ -0,0 +1,15 @@
+{
+ "name" : "redis",
+ "version" : "0.0.1",
+ "description" : "Elastic Search Client Library",
+ "author": "Pedro Medeiros",
+ "main": "./index.js",
+ "devDependencies": {
+ "mocha": "1.3.2"
+ ,"should": "1.1.0"
+ },
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/pedrosnk/elastico.git"
+ }
+}
@@ -0,0 +1,66 @@
+var should = require('should')
+ , Elastico = require('../lib/elastico').Elastico;
+
+describe('Elastico', function(){
+ var elastic_search = new Elastico();
+ before(function(done){
+ elastic_search.removeIndex('test',function(){
+ elastic_search.removeIndex('indice',function(){
+ done();
+ });
+ });
+
+ });
+
+ it('should get an object of the elastic search', function(done){
+ elastic_search.insertWithId("test","ids","lol",'{"lol": "zomg"}', function(){
+ elastic_search.getIndex("test","ids","lol", function(json){
+ json.should.be.ok;
+ done();
+ });
+ });
+ });
+
+ it('should get all the index objects of the elastic search', function(done){
+ elastic_search.insert("test","ids",'{"quelquechose": "je parle quelquechose"}', function(){
+ elastic_search.getAll("test","ids",function(json){
+ json.should.be.ok;
+ done();
+ });
+ });
+ });
+
+ it('should insert an data into the elastic search', function(done){
+ elastic_search.insert("test","ids", '{"key": "value"}', function(created){
+ created.should.be.true;
+ done();
+ });
+ });
+
+ it('should insert an data into the elastic search with an pre existing id', function(done){
+ elastic_search.insertWithId("test","ids", "zomgid" , '{"key", "value"}', function(created){
+ created.should.be.true;
+ done();
+ });
+ });
+
+ it('should remove a data of the elastic search by an id', function(done){
+ elastic_search.insertWithId("test","ids", "zomgid" , '{"key": "value"}', function(created){
+ elastic_search.remove("test","ids", "zomgid", function(removed){
+ removed.should.be.true;
+ done();
+ });
+ });
+ });
+
+ it('should create an index and remove it',function(done) {
+ elastic_search.createIndex('create_index',function(created){
+ created.should.be.true;
+ elastic_search.removeIndex('create_index',function(removed){
+ removed.should.be.true;
+ done();
+ });
+ });
+ });
+
+});

0 comments on commit a646e91

Please sign in to comment.