Skip to content

Commit

Permalink
Introducing real-time-get support as suggested in lbdremy#88
Browse files Browse the repository at this point in the history
First stab at it, including the test suggested in https://wiki.apache.org/solr/RealTimeGet
  • Loading branch information
marc-portier committed Jul 20, 2014
1 parent b92afb1 commit cd24531
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lib/solr.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function Client(options){
this.UPDATE_HANDLER = 'update';
this.SELECT_HANDLER = 'select';
this.ADMIN_PING_HANDLER = 'admin/ping';
this.GET_HANDLER = 'get';
}

/**
Expand Down Expand Up @@ -129,6 +130,37 @@ Client.prototype.add = function(docs,options,callback){
return this.update(docs,options,callback);
}

/**
/**
* Get a document by id or a list of documents using the Real-time-get feature in SOLR4 (https://wiki.apache.org/solr/RealTimeGet)
*
* @param {String|Array} ids - id or list of ids that identify the documents to get
* @param {Object} [options] -
* @param {Function} callback(err,obj) - a function executed when the Solr server responds or an error occurs
* @param {Error} callback().err
* @param {Object} callback().obj - JSON response sent by the Solr server deserialized
*
* @return {http.ClientRequest}
* @api public
*/

Client.prototype.get = function(ids, options, callback){
if(typeof(options) === 'function'){
callback = options;
options = {};
}
ids = Array.isArray(ids) ? ids : [ids];
options.ids=ids.join(',');

var qs = querystring.stringify(options);
this.options.fullPath = [this.options.path,this.options.core, this.GET_HANDLER + '?' + qs ]
.filter(function(element){
return element;
})
.join('/');
return queryRequest(this.options,callback);
}

/**
* Add the remote resource located at the given path `options.path` into the Solr database.
*
Expand Down
73 changes: 73 additions & 0 deletions test/rt-get-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Testing support for Real-Time GET -- https://wiki.apache.org/solr/RealTimeGet
/**
* Modules dependencies
*/

var mocha = require('mocha'),
figc = require('figc'),
assert = require('chai').assert,
libPath = process.env['SOLR_CLIENT_COV'] ? '../lib-cov' : '../lib',
solr = require( libPath + '/solr'),
sassert = require('./sassert');

// Test suite
var config = figc(__dirname + '/config.json');
var client = solr.createClient(config.client);
var basePath = [config.client.path, config.client.core].join('/').replace(/\/$/,"");

describe('Client',function(){
describe('Real-time-get functionality',function(){
var id = "RandomId-" + Math.floor(Math.random() * 1000000);
var title = "the title for " + id;
var doc = { id : id, title_t : title };

it('should add one document with a long period before committing',function(done){
var options = {
commitWithin : 10000000 //extremely long, giving us plenty of time to test
};
client.add(doc, options, function(err,data){
sassert.ok(err,data);
done();
});
});

it('should not find that document in the index yet' ,function(done){
var query = client.createQuery();
query.matchFilter('id', id);
client.search(query,function(err,data){
sassert.ok(err,data);
assert.equal(data.response.numFound, 0, "Added document should not be index and thus not found.");
done();
});
});

it('should be able to get that specific document',function(done){
// note that by default the /get handler will have omitHeader=true configured on the server!
client.get(id,{omitHeader: false}, function(err,data){
sassert.ok(err,data);
assert.equal(data.response.numFound, 1, "Added document should be retrieved in real-time get.");
var retrieved = data.response.docs[0];
console.log("\nretrieved ==> %j\n", retrieved);
assert.equal(retrieved.id, id, "Didn't retrieve the expected document.");
assert.equal(retrieved.title_t, title, "Didn't retrieve the expected document.");
done();
});
});

it('should be able to delete it',function(done){
client.deleteByID(id,function(err,data){
sassert.ok(err,data);
done();
});
});

it('should no longer be able to get that specific document',function(done){
client.get(id,{omitHeader: false}, function(err,data){
sassert.ok(err,data);
assert.equal(data.response.numFound, 0, "Deleted document should no longer be retrievable in real-time get.");
done();
});
});

});
});

0 comments on commit cd24531

Please sign in to comment.