Skip to content

Commit

Permalink
make index, type and id optional when creating records. #73
Browse files Browse the repository at this point in the history
  • Loading branch information
nelsonic committed Apr 15, 2015
1 parent 48443f0 commit 6439b9f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 27 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ npm install esta --save
<a name="connect"/>
#### CONNECT to ElasticSearch Cluster using `ES.CONNECT(calback(response))`

If you need to check the connection status to the ElasticSearch Instance/Cluster
If you need to *check* the connection status to the ElasticSearch Instance/Cluster
we expose the handy `ES.CONNECT` method:

```js
Expand All @@ -72,8 +72,6 @@ example `ES.CONNECT` [response](https://travis-ci.org/nelsonic/esta/jobs/5353361
lucene_version: '4.10.2' },
tagline: 'You Know, for Search' }
```
***Note***: **Esta** *expects* you to have environment variables set up for
**ES_HOST** and **ES_PORT** (see below)

<br />

Expand All @@ -88,9 +86,7 @@ Creating a new record is *easy*:
```js
// define the record you want to store:
var record = {
index: 'twitter',
type: 'tweet',
id: Math.floor(Math.random() * (100000)), // or what ever GUID you want
date: new Date().toISOString(),
message: 'Your amazing message goes here'
};
ES.CREATE(record, function(response) {
Expand All @@ -99,14 +95,14 @@ ES.CREATE(record, function(response) {
```
A typical *successful* `ES.CREATE` response:
```js
{ _index: 'twitter',
_type: 'tweet',
{ _index: 'index',
_type: 'type',
_id: '112669114721',
_version: 1,
created: true }
```

##### *Required Fields* for a *New Record*:
##### *Optional Fields* for a *New Record*:

- `index` can be compared to a ***Database*** in **SQL**
see: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/glossary.html#glossary-index
Expand Down
7 changes: 4 additions & 3 deletions lib/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ var OPTIONS = require('./options');
var REQUEST = require('./http_request');

module.exports = function create(record, callback) {

record.index = record.index || 'index';
// fallbacks for type and id ... do we need these?
// see discussion on:
record.type = record.type || 'type';
record.id = record.id || Math.floor(Math.random() * (1000000));
// if the person does not set the record.id we give it a pseudo-random-number:
record.id = record.id || Math.floor(Math.random() * (1000000000))+new Date().getTime();
// An object of options to indicate where to post to
var options = OPTIONS(record, 'POST');

Expand Down
18 changes: 7 additions & 11 deletions lib/options.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = function options(record, method) {
// record.index = record.index || process.env.ES_INDEX || 'index';
record.index = record.index || process.env.ES_INDEX || 'index';
var o = {
host: '127.0.0.1',
port: 9200,
Expand All @@ -9,20 +9,16 @@ module.exports = function options(record, method) {
'Content-Type': 'application/json'
}
};
// example url: https://un:pw@dogwood-1234.eu-west-1.searchly.com
if(process.env.SEARCHBOX_SSL_URL) {
var url = process.env.SEARCHBOX_SSL_URL;
// e.g: "https://un:[email protected]"
var url = process.env.SEARCHBOX_SSL_URL;
var unpw = url.split('://')[1].split(':');
var un = unpw[0];
var pw = unpw[1].split('@')[0];
console.log(un +':'+pw);
var un = unpw[0];
var pw = unpw[1].split('@')[0];
var auth = (new Buffer(un + ':' + pw, 'utf8')).toString('base64');
o.headers['Authorization'] = 'Basic ' + auth;
o.host = url.split('@')[1];
o.port = 443;
o.host = url.split('@')[1];
o.port = 443;
}
// console.log(' - - - - - - - - - - - - - - options:')
// console.log(o);
// console.log(' - - - - - - - - - - - - - - - - - - -')
return o;
}
42 changes: 39 additions & 3 deletions test/create.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var test = require('tape');
var chalk = require('chalk');
var record = require('./fake_record.js')(); // fake record

var uncache = require('./uncache').uncache; // so we can re-load lib/options.js
var CREATE = require('../lib/create.js');

test(chalk.cyan('CREATE a record'), function (t) {
Expand All @@ -12,12 +12,48 @@ test(chalk.cyan('CREATE a record'), function (t) {
});
});

test(chalk.cyan('CREATE a record without specifying index, type or id!'), function (t) {
delete record.id;
test(chalk.cyan('CREATE a record without specifying index, type or id (fallback)'), function (t) {
delete record.index;
delete record.type;
delete record.id;

uncache('../lib/options');
uncache('../lib/create.js');
uncache('../lib/http_request');
var CREATE = require('../lib/create.js'); // re-require it

CREATE(record, function (res) {
console.log(res);
t.equal(res.created, true, chalk.green("✓ Record Created"));
t.equal(res._index, 'index', chalk.green("✓ Fallback index is: "+res._index));
t.equal(res._type, 'type', chalk.green("✓ Fallback type is: "+res._type));

t.end();
});
});

test(chalk.cyan('CREATE a record with process.env.ES_INDEX'), function (t) {
var record = require('./fake_record.js')();
delete record.index;
process.env.ES_INDEX = 'testindex';


uncache('../lib/options');
uncache('../lib/create.js');
uncache('../lib/http_request');
var CREATE = require('../lib/create.js'); // re-require it

// var OPTIONS = require('../lib/options');
// var opts = OPTIONS(record, 'POST');
// console.log(" - - - - - - - - ")
// console.log(opts);
// console.log(" - - - - - - - - ")

CREATE(record, function (res) {
console.log(res);
t.equal(res.created, true, chalk.green("✓ Record Created using ES_INDEX"));
t.equal(res._index, process.env.ES_INDEX, chalk.green("✓ index: "+process.env.ES_INDEX));
delete process.env.ES_INDEX;
t.end();
});
});
10 changes: 9 additions & 1 deletion test/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ test(chalk.cyan('Query ES for string: "amazing"'), function (t) {
chalk.green("✓ Search results found: "+ res.hits.total));
t.end();
});
},800)
},1200)
});

test(chalk.cyan('Query ES for string that is NOT in the index'), function (t) {
Expand Down Expand Up @@ -80,3 +80,11 @@ test(chalk.cyan('Simulate actual usage: search for "Thanks"'), function (t) {
t.end();
});
});

test(chalk.cyan('Simulate actual usage: search for "Thanks"'), function (t) {
SEARCH({"text":"thanks"}, function(res) {
t.equal(res.hits.total > 0, true,
chalk.green("✓ Search results found: "+ res.hits.total ));
t.end();
});
});

0 comments on commit 6439b9f

Please sign in to comment.