Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idlist #95

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: node_js
node_js:
- 0.12
- iojs
- 4.2.3
services:
- elasticsearch
22 changes: 11 additions & 11 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@ apt-get update

apt-get -y install g++ git git-core nodejs

# nodejs - most people running esta will already have node.js installed.
# apt-get -y install g++ git git-core nodejs npm
# use https://github.com/visionmedia/n to get latest node+npm
# npm install n -g
# n stable
# node -v
# npm install nodemon -g

# see: https://gist.github.com/wingdspur/2026107
sudo apt-get install openjdk-7-jre-headless -y

### Check http://www.elasticsearch.org/download/ for latest version of ElasticSearch and replace wget link below

wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.7.1.deb
sudo dpkg -i elasticsearch-1.7.1.deb
wget https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.7.2.deb
sudo dpkg -i elasticsearch-1.7.2.deb

curl -L http://github.com/elasticsearch/elasticsearch-servicewrapper/tarball/master | tar -xz
sudo mkdir -p /usr/local/share/elasticsearch/bin/
Expand All @@ -34,7 +26,15 @@ sudo /usr/local/share/elasticsearch/bin/service/elasticsearch install
sudo ln -s `readlink -f /usr/local/share/elasticsearch/bin/service/elasticsearch` /usr/local/bin/rcelasticsearch

sudo service elasticsearch start
# curl http://localhost:9200
# wait for ElasticSearch to boot
sleep 10
# Test that the sever is working
curl http://localhost:9200

# OPTIONALLY if you want to install Node:
# https://nodejs.org/en/download/package-manager/#debian-and-ubuntu-based-linux-distributions
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs

SCRIPT

Expand Down
28 changes: 28 additions & 0 deletions lib/list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var OPTIONS = require('./options');
var REQUEST = require('./http_request');

/**
* list constructs a search query to list ALL the ids in the index!
* @param {Object} query - the query we want to look for
* required fields text (the text you want to search for)
* and field (the fields in which we want to search)
* try using curl: curl 127.0.0.1:9200/_all/tweet/_search?q=text:amazing
*/
module.exports = function list(query, callback) {
query.index = query.index || '_all';
query.type = query.type || 'tweet';
query.text = query.text || 'hello';
query.field = query.field || 'text';
// An object of options to indicate where to post to
var options = OPTIONS(query, 'GET');
options.body = {
"query" : {
"match_all" : {}
},
"fields": ["_id"]
}
var str = encodeURIComponent(query.text);
options.path = '/' + query.index + '/' + query.type
+'/_search?scrollq=1m&pretty'; // see:
return REQUEST(options, callback);
}
5 changes: 3 additions & 2 deletions lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ module.exports = function options(record, method) {
}
};
// example url: https://myuser:password@dogwood-1234.eu-west-1.searchly.com
if(process.env.SEARCHBOX_SSL_URL || process.env.BONSAI_URL) {
var u = url.parse(process.env.SEARCHBOX_SSL_URL || process.env.BONSAI_URL);
if(process.env.SEARCHBOX_SSL_URL) { // || process.env.BONSAI_URL) {
var u = url.parse(process.env.SEARCHBOX_SSL_URL); // || process.env.BONSAI_URL);
var unpw = u.auth.split(':'); // auth is everything between // and @
var username = unpw[0];
var password = unpw[1];
Expand All @@ -34,5 +34,6 @@ module.exports = function options(record, method) {
o.host = u.host; // everthing after the @ symbol
o.port = 443; // always TLS/SSL
}
o.body = record.body; // if its set ... is this a good idea...?
return o; // the full object to be used in the http request
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"url": "https://github.com/dwyl/esta.git"
},
"engines": {
"node": ">= 0.10"
"node": ">= 4.2.3"
},
"keywords": [
"ElasticSearch",
Expand Down
5 changes: 4 additions & 1 deletion test/_connection.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
var test = require('tape');
var chalk = require('chalk');

process.env.SEARCHBOX = process.env.SEARCHBOX_SSL_URL;
delete process.env.SEARCHBOX_SSL_URL; // ensure we load http (NOT https)!
delete process.env.BONSAI_URL; // don't use Bonsai ES for bulk tests
var ES_URL = '127.0.0.1:9200';
var ES = require('../lib/index');

test(chalk.cyan('CONNECT to ES on ' +ES_URL), function (t) {
// console.log('process.env.SEARCHBOX_SSL_URL: '+process.env.SEARCHBOX_SSL_URL)
ES.CONNECT('twitter', function (res) {

console.log(' - - - - - - - - - - - - - - - - - - - - - res:');
console.log(res);
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - ');
t.equal(res.status, 200, chalk.green("✓ Status 200 - OK"));
t.end();
});
Expand Down
83 changes: 45 additions & 38 deletions test/heroku.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
var test = require('tape');
var chalk = require('chalk');
var uncache = require('./uncache').uncache;
// console.log(' - - - - - - - - - - - - - - - - - - - - - - - - process.env.SEARCHBOX_SSL_URL:');
// console.log(process.env.SEARCHBOX +' || ' + process.env.SEARCHBOX_SSL_URL);
process.env.SEARCHBOX = process.env.SEARCHBOX || process.env.SEARCHBOX_SSL_URL;
// console.log(process.env.SEARCHBOX +' || ' + process.env.SEARCHBOX_SSL_URL);

test(chalk.cyan('Force HTTP Error with bad UN:PW'), function (t) {
// please don't spam this SearchBox ElasticSearch account with Records!
// Its JUST for Testing this module! thanks! :-)
// process.env.SEARCHBOX = process.env.SEARCHBOX_SSL_URL;
process.env.SEARCHBOX_SSL_URL = 'https://un:pw@kili-eu-west-1.searchly.com'
var ES_URL = process.env.SEARCHBOX_SSL_URL || '127.0.0.1:9200';
// console.log(' - - - - - - - - - - - - - - - - - - - - - res:');
// console.log(process.env.SEARCHBOX_SSL_URL);
// console.log(' - - - - - - - - - - - - - - - - - - - - - - - - ');
// var ES_URL = process.env.SEARCHBOX_SSL_URL || '127.0.0.1:9200';
// https://nodejs.org/docs/latest/api/globals.html#globals_require_cache
uncache('../lib/index'); // reload http_request sans SSL! (localhost)
var ES = require('../lib/index');
ES.CONNECT(function (res) {
console.log(res);
t.equal(res.status, 'error', chalk.green("✓ Error forced. (we wanted this!)"));

t.end();
});
});
Expand All @@ -36,44 +43,43 @@ test(chalk.cyan('Exercise http_request req.on("error") handler'), function (t) {
});


test(chalk.cyan('CONNECT to Bonsai on HEROKU!'), function (t) {
// please don't spam this Bonsai ElasticSearch account with Records!
// Its JUST for Testing this module! thanks! :-)
delete process.env.SEARCHBOX_SSL_URL; // unset SearchBox so we can test Bonsai
process.env.BONSAI_URL = 'https://8py6wr37:ehq7m0yasuz446rd@ginkgo-5930963.eu-west-1.bonsai.io'
// https://nodejs.org/docs/latest/api/globals.html#globals_require_cache
uncache('../lib/index'); // reload http_request sans SSL! (localhost)
var ES = require('../lib/index');
// don't specify the index
ES.CONNECT(function (res) {
console.log(res);
t.equal(res.status, 200, chalk.green("✓ Status 200 - HEROKU Bonsai works like a charm"));
t.end();
});
});

test(chalk.cyan('CREATE a record on HEROKU/Bonsai'), function (t) {
process.env.BONSAI_URL = 'https://8py6wr37:ehq7m0yasuz446rd@ginkgo-5930963.eu-west-1.bonsai.io'
var ES = require('../lib/index');
var record = require('./fake_record.js')(); // fake record
ES.CREATE(record, function (res) {
console.log(res)
t.equal(res.created, true, chalk.green("✓ Record Created on Heroku/Bonsai"));
t.end();
});
});
// test(chalk.cyan('CONNECT to Bonsai on HEROKU!'), function (t) {
// // please don't spam this Bonsai ElasticSearch account with Records!
// // Its JUST for Testing this module! thanks! :-)
// delete process.env.SEARCHBOX_SSL_URL; // unset SearchBox so we can test Bonsai
// process.env.BONSAI_URL = 'https://8py6wr37:ehq7m0yasuz446rd@ginkgo-5930963.eu-west-1.bonsai.io'
// // https://nodejs.org/docs/latest/api/globals.html#globals_require_cache
// uncache('../lib/index'); // reload http_request sans SSL! (localhost)
// var ES = require('../lib/index');
// // don't specify the index
// ES.CONNECT(function (res) {
// console.log(res);
// t.equal(res.status, 200, chalk.green("✓ Status 200 - HEROKU Bonsai works like a charm"));
// t.end();
// });
// });
//
// test(chalk.cyan('CREATE a record on HEROKU/Bonsai'), function (t) {
// process.env.BONSAI_URL = 'https://8py6wr37:ehq7m0yasuz446rd@ginkgo-5930963.eu-west-1.bonsai.io'
// var ES = require('../lib/index');
// var record = require('./fake_record.js')(); // fake record
// ES.CREATE(record, function (res) {
// console.log(res)
// t.equal(res.created, true, chalk.green("✓ Record Created on Heroku/Bonsai"));
// t.end();
// });
// });

// var searchbox_index = 'bonsai'+new Date().getTime();
test(chalk.cyan('CONNECT to SearchBox on HEROKU!'), function (t) {
// please don't spam this SearchBox ElasticSearch account with Records!
// Its JUST for Testing this module! thanks! :-)
delete process.env.BONSAI_URL; // so we can test SearchBox now
process.env.SEARCHBOX_SSL_URL = 'https://paas:177117314d80d98671aaffd7fb9a314b@kili-eu-west-1.searchly.com'
// https://nodejs.org/docs/latest/api/globals.html#globals_require_cache
uncache('../lib/index'); // reload http_request sans SSL! (localhost)
process.env.SEARCHBOX_SSL_URL = process.env.SEARCHBOX || process.env.SEARCHBOX_SSL_URL; // see _connection.js
// console.log(process.env.SEARCHBOX_SSL_URL);
uncache('../lib/index'); // see: https://github.com/dwyl/decache
var ES = require('../lib/index');
ES.CONNECT('twitter', function (res) {
console.log(' - - - - - - - - - - - - - - - - - - - - - res:');
console.log(res);
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - ');
t.equal(res.status, 200, chalk.green("✓ Status 200 - OK"));
t.end();
});
Expand All @@ -84,7 +90,9 @@ test(chalk.cyan('CREATE a record on HEROKU/SearchBox'), function (t) {
var ES = require('../lib/index');
var record = require('./fake_record.js')(); // fake record
ES.CREATE(record, function (res) {
console.log(res)
console.log(' - - - - - - - - - - - - - - - - - - - - - res:');
console.log(res);
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - ');
t.equal(res.created, true, chalk.green("✓ Record Created "+res._id));
t.end();
});
Expand All @@ -93,7 +101,6 @@ test(chalk.cyan('CREATE a record on HEROKU/SearchBox'), function (t) {
test(chalk.cyan('Ensure that we clear node require cache to resume testing on localhost'), function(t){
uncache('../lib/index');
delete process.env.SEARCHBOX_SSL_URL; // unset SearchBox URL so we can resume testing locally
delete process.env.BONSAI_URL; // same for BONSAI
t.equal(process.env.BONSAI_URL, undefined, chalk.green("✓ Continue testing Local ES."));
t.equal(process.env.SEARCHBOX_SSL_URL, undefined, chalk.green("✓ Continue testing Local ES."));
t.end();
});
File renamed without changes.
32 changes: 32 additions & 0 deletions test/x_list.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// LIST operation needs to be done after "performance" to ensure we have some records!
var test = require('tape');
var chalk = require('chalk');
var fs = require('fs');
var LIST = require('../lib/list');

test(chalk.cyan("LIST all _ids of records in index"), function(t) {
var query = {
type: 'tweet',
index: 'twitter'
}
LIST(query, function(res){
console.log(' - - - - - - - - - - - - - - - - - - - - - res:');
// console.log(JSON.stringify(res, null, 2));
console.log(res.hits.hits.length)
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - ');
t.ok(res.hits.hits.length > 9, "List returned "+res.hits.hits.length + " hits.")
t.end();
});
});

test(chalk.cyan("LIST all _ids of records in _all indecies"), function(t) {
var query = {};
LIST(query, function(res){
console.log(' - - - - - - - - - - - - - - - - - - - - - res:');
// console.log(JSON.stringify(res, null, 2));
console.log(res.hits.hits.length)
console.log(' - - - - - - - - - - - - - - - - - - - - - - - - ');
t.ok(res.hits.hits.length > 9, "List returned "+res.hits.hits.length + " hits.")
t.end();
});
});
2 changes: 1 addition & 1 deletion test/z_teardown.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var record = { // fake record
index: 'twitter',
id: 1
}

// equivalent to: curl -XDELETE 'http://localhost:9200/_all'
test( chalk.yellow.bgRed.bold(' - DROP ALL INDEXes so ES is Clean for Next Time - '), function (t) {
DROP(record, function (res) {
STATS(function (res) {
Expand Down