Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
index document when update previously fail in 404 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdemonte committed May 7, 2016
1 parent 36aab67 commit 96b48e4
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 54 deletions.
94 changes: 47 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,18 +389,36 @@ function indexDoc(update, callback) {
body[field] = null;
});
}
esOptions.client[update ? 'update' : 'index'](
{
index: esOptions.index,
type: esOptions.type,
id: self._id.toString(),
body: update ? {doc: body}: body
},
function (err, result) {
_indexDoc(self._id, body, esOptions, resolve, reject, update);
});
}

/**
* Update or Index a document, when updating, retry as index when getting a 404 error
* @param {ObjectId|String} id
* @param {Object} body
* @param {Object} esOptions
* @param {Function} resolve
* @param {Function} reject
* @param {Boolean} [update] default false
* @private
*/
function _indexDoc(id, body, esOptions, resolve, reject, update) {
esOptions.client[update ? 'update' : 'index'](
{
index: esOptions.index,
type: esOptions.type,
id: id.toString(),
body: update ? {doc: body}: body
},
function (err, result) {
if (update && err && err.status === 404) {
_indexDoc(id, body, esOptions, resolve, reject);
} else {
return err ? reject(err) : resolve(result);
}
);
});
}
);
}

/**
Expand Down Expand Up @@ -456,46 +474,21 @@ function removeDoc(callback) {
var self = this;
return utils.run(callback, function (resolve, reject) {
var esOptions = self.esOptions();
deleteByMongoId(
esOptions,
self,
function (err, result) {
return err ? reject(err) : resolve(result);
esOptions.client.delete(
{
index: esOptions.index,
type: esOptions.type,
id: self._id.toString()
},
3
);
});
}

/**
* Delete one document on ElasticSearch
* Internal
* @param {Object} options
* @param {Object} document
* @param {Function} callback
* @param {Number} retry
*/
function deleteByMongoId(options, document, callback, retry) {
options.client.delete(
{
index: options.index,
type: options.type,
id: document._id.toString()
},
function (err) {
if (err && err.message.indexOf('404') > -1) {
if (retry && retry > 0) {
setTimeout(function () {
deleteByMongoId(options, document, callback, retry - 1);
}, 500);
function (err) {
if (err) {
reject(err);
} else {
callback(err);
resolve();
}
} else {
callback(err);
}
}
);
);
});
}

/**
Expand Down Expand Up @@ -541,7 +534,14 @@ function postSave(doc) {
doc.constructor.emit('es-indexed', err);
});
} else {
postRemove(doc);
doc.emit('es-filtered');
doc.constructor.emit('es-filtered');
if (!data.wasNew) {
doc.esRemove(function (err, res) {
doc.emit('es-removed', err, res);
doc.constructor.emit('es-removed', err, res);
});
}
}
}
}
Expand Down
82 changes: 75 additions & 7 deletions test/document-hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,9 @@ describe("document-hook", function () {
})
.then(function () {
return new utils.Promise(function (resolve, reject) {
john.on('es-removed', function (err) {
if (err) {
resolve();
} else {
reject(new Error('should not have been found'));
}
});
john.on('es-filtered', function () {
resolve();
});
john.save();
});
})
Expand Down Expand Up @@ -725,4 +721,76 @@ describe("document-hook", function () {
});
});

it('should save a previously filtered entry', function (done) {

var UserSchema = new mongoose.Schema({
name: String,
age: Number
});

UserSchema.plugin(plugin, {filter: function (doc) {
return doc.age < 80;
}});

var UserModel = mongoose.model('User', UserSchema);

var henry = new UserModel({name: 'Henry', age: 85});

utils.deleteModelIndexes(UserModel)
.then(function () {
return UserModel.esCreateMapping();
})
.then(function () {
return henry.save();
})
.then(function () {
return UserModel.esRefresh();
})
.then(function () {
return new utils.Promise(function (resolve) {
var options = UserModel.esOptions();
var client = options.client;
client.search({index: options.index, type: options.type, body: {query: {match_all: {}}}}, function (err, resp) {
expect(resp.hits.total).to.eql(0);
resolve();
});
});
})
.then(function () {
return new utils.Promise(function (resolve, reject) {
henry.age = 35;
henry.on('es-indexed', function (err) {
if (err) {
reject(err);
} else {
resolve();
}
});
henry.save();
});
})
.then(function () {
return UserModel.esRefresh();
})
.then(function () {
return new utils.Promise(function (resolve) {
var options = UserModel.esOptions();
var client = options.client;
client.search({index: options.index, type: options.type, body: {query: {match_all: {}}}}, function (err, resp) {
expect(resp.hits.total).to.eql(1);
var hit = resp.hits.hits[0];
expect(hit._id).to.eql(henry._id.toString());
expect(hit._source).to.eql({name: 'Henry', age: 35});
resolve();
});
});
})
.then(function () {
done();
})
.catch(function (err) {
done(err);
});
});

});

0 comments on commit 96b48e4

Please sign in to comment.