Skip to content

Commit

Permalink
Merge pull request #44 from juzerali/deploy-9
Browse files Browse the repository at this point in the history
Fixes #9, stop interpreting 'id' field of documents as unique identifier...
  • Loading branch information
phillro committed Feb 6, 2013
2 parents b746dcf + ead177e commit b065ee1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 71 deletions.
32 changes: 16 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,22 @@ var qryObj = {
}

elasticSearchClient.search('my_index_name', 'my_type_name', qryObj)
.on('data', function(data) {
console.log(JSON.parse(data))
})
.on('done', function(){
//always returns 0 right now
})
.on('error', function(error){
console.log(error)
})
.exec()

elasticSearchClient.index('my_index_name', 'my_type_name', {'name':'name', id:"1111"})
.on('data', function(data) {
console.log(data)
})
.exec()
.on('data', function(data) {
console.log(JSON.parse(data))
})
.on('done', function(){
//always returns 0 right now
})
.on('error', function(error){
console.log(error)
})
.exec()

elasticSearchClient.index('my_index_name', 'my_type_name', /* _id (optional) */'1111', {'name':'name'})
.on('data', function(data) {
console.log(data)
})
.exec()

//Bulk index example
var commands = []
Expand Down
32 changes: 13 additions & 19 deletions lib/elasticsearchclient/calls/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@
var querystring = require('querystring'),
ElasticSearchClient = require('../elasticSearchClient')

ElasticSearchClient.prototype.index = function(indexName, typeName, document, options) {
ElasticSearchClient.prototype.index = function(indexName, typeName, document, id, options) {
var path = '/' + indexName + '/' + typeName;
var qs = '';
if (options) {
qs = querystring.stringify(options)
var method = 'POST'

if(typeof id === 'object'){
options = id;
id = undefined;
}

var method = 'POST'
//If the document has an id field, its an update.
if (document.id) {
path += "/" + document.id
if (id) {
path += "/" + id
method = 'PUT'
delete document.id
}

if (options) {
qs = querystring.stringify(options)
}

if (qs.length > 0) {
path += "?" + qs;
}
Expand Down Expand Up @@ -220,20 +225,9 @@ ElasticSearchClient.prototype.moreLikeThis = function(indexName, typeName, docum
}

ElasticSearchClient.prototype.update = function(indexName, typeName, documentId, document, options) {

if(typeof documentId === 'object'){
options = document;
document = documentId;
documentId = document.id;
delete document.id;
}

document = document || {};
document = {"doc": document};

if(!documentId)
throw "id should be provided for update: " + JSON.stringify(document);

var path = '/' + indexName + '/' + typeName + '/'+documentId+'/_update';
var qs = '';
if (options) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Phillip Rosen <phill.rosen@gmail.com>",
"name": "elasticsearchclient",
"description": "A client for Elastic Search",
"version": "0.2.2",
"version": "0.3.0",
"repository": {
"url": ""
},
Expand Down
37 changes: 2 additions & 35 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("ElasticSearchClient Core api", function(){
/*
* To allow running tests individually `mocha --grep search`
*/
elasticSearchClient.index(indexName, objName, {'name':'sushi', id: 'sushi'})
elasticSearchClient.index(indexName, objName, {'name':'sushi'}, "sushi")
.on('data', function(){
done();
})
Expand All @@ -41,7 +41,7 @@ describe("ElasticSearchClient Core api", function(){
})

it("should index an object with given id under the same id", function(done){
elasticSearchClient.index(indexName, objName, {'name':'name', id:"1111"})
elasticSearchClient.index(indexName, objName, {'name':'name', id:"9999"}, "1111")
.on('data', function(data) {
data = JSON.parse(data);
data._id.should.equal("1111");
Expand Down Expand Up @@ -78,39 +78,6 @@ describe("ElasticSearchClient Core api", function(){
.exec()
});

it("should update the existing doc even if only an object with id is passed", function(done){
elasticSearchClient.update(indexName, objName, {id: "sushi", age: 23})
.on('data', function(data) {
data = JSON.parse(data);
data.should.be.ok;
data._id.should.equal("sushi");
done();
})
.exec()
});

it("should throw error when no id is passed", function(){
(function() {
elasticSearchClient.update(indexName, objName, {age: 23});
})
.should.throw;
});

it("should not throw even when no data is provided", function(){
(function() {
elasticSearchClient.update(indexName, objName, {age: 25})
.on('data', function(data) {
data = JSON.parse(data);
data.should.be.ok;
data.error.should.equal.be.ok;
data.status.should.equal(500);
done();
})
.exec()
})
.should.not.throw;
});

});

describe("#search", function(){
Expand Down

0 comments on commit b065ee1

Please sign in to comment.