Skip to content

Commit

Permalink
fix issue #45
Browse files Browse the repository at this point in the history
use https://github.com/arielfr/elastic-deletebyquery to bulk delete for destroyAll when working with v2.x of ES
  • Loading branch information
pulkitsinghal committed Aug 11, 2016
1 parent 3203754 commit a6e4a46
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/server/datasources.sample-es-plain-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
'index': 'shakespeare',
'hosts': [
{
'host': 'es',
'host': 'localhost',
'port': 9201
}
],
Expand Down
2 changes: 1 addition & 1 deletion examples/server/datasources.sample-es-ssl-1.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
'hosts': [
{
'protocol': 'https',
'host': 'es', //
'host': 'localhost',
'port': 9243, // TODO: think about defaulting it to 9243+1=9244 for an example run against v1.x of ES
'auth': 'username:password'
}
Expand Down
24 changes: 15 additions & 9 deletions lib/esConnector.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var Promise = require('bluebird');
var log = require('debug')('loopback:connector:elasticsearch');

var elasticsearch = require('elasticsearch');
var deleteByQuery;
var Connector = require('loopback-connector').Connector;

/**
Expand Down Expand Up @@ -101,6 +102,12 @@ ESConnector.prototype.connect = function (callback) {
}
else {
self.db = new elasticsearch.Client(self.getClientConfig());
if (self.settings.apiVersion.indexOf('2') === 0) {
log('injecting deleteByQuery');
deleteByQuery = require('elastic-deletebyquery');
deleteByQuery(self.db);
self.db.deleteByQuery = Promise.promisify(self.db.deleteByQuery);
}
// NOTE: we take the liberty of setting up indices even if dev doesn't call automigrate
if(self.settings.mappings) {
self.setupIndex(function(err) {
Expand Down Expand Up @@ -795,17 +802,16 @@ ESConnector.prototype.destroyAll = function destroyAll(modelName, whereClause, c
};

var defaults = self.addDefaults(modelName);
self.db.deleteByQuery(_.defaults({ body: body }, defaults)).then(
function (response) {
var options = _.defaults({ body: body }, defaults);
log('ESConnector.prototype.destroyAll', 'options:', JSON.stringify(options,null,2));
self.db.deleteByQuery(options)
.then(function(response){
cb(null, response);
},
function (err) {
})
.catch(function(err) {
console.trace(err.message);
if (err) {
return cb(err, null);
}
}
);
return cb(err, null);
});
};

/**
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"async": "^0.9.0",
"bluebird": "^2.9.14",
"debug": "^2.1.3",
"elastic-deletebyquery": "1.0.7",
"elasticsearch": "^11.0.1",
"lodash": "^3.5.0",
"loopback-connector": "^1.1.1"
Expand Down
18 changes: 10 additions & 8 deletions test/02.basic-querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,19 +825,21 @@ describe('basic-querying', function () {
before(seed);

it('should only delete instances that satisfy the where condition', function (done) {
this.timeout(4000);
this.timeout(6000);
// NOTE: ES indexing then searching isn't real-time ... its near-real-time
setTimeout(function () {
User.destroyAll({name: 'John Lennon'}, function () {
User.find({where: {name: 'John Lennon'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(0);
User.find({where: {name: 'Paul McCartney'}}, function (err, data) {
setTimeout(function () {
User.find({where: {name: 'John Lennon'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(1);
done();
data.length.should.equal(0);
User.find({where: {name: 'Paul McCartney'}}, function (err, data) {
should.not.exist(err);
data.length.should.equal(1);
done();
});
});
});
}, 2000);
});
}, 2000);
});
Expand Down

0 comments on commit a6e4a46

Please sign in to comment.