From 98ab3cc1f1e2236c58022c7556ed4d35f4c41f5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Berg=C3=A9?= Date: Thu, 3 Apr 2014 11:09:32 +0200 Subject: [PATCH] Add index.destroy function. --- README.md | 19 +++++++++++ lib/index.js | 47 +++++++++++++++++++++++++++ test/index.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 154 insertions(+) diff --git a/README.md b/README.md index 0fa08ed..dde17e0 100644 --- a/README.md +++ b/README.md @@ -192,6 +192,25 @@ index.create([ ], { lang: 'FRENCH' }, function (err) { ... }); ``` +## index.destroy(values, [options], callback) + +Destroy a documents in the index. + +```js +index.destroy(['182', '85'], function (err) { ... }); +``` + +Some options are avalaibles: + +#### field + +Type: `String` + +The field used to match values, default to "id". + +```js +index.destroy(['bob', 'tom'], { field: 'name' }, function (err) { ... }); +``` ### index.search(query, [options], callback) diff --git a/lib/index.js b/lib/index.js index 0272ef6..20e5baf 100644 --- a/lib/index.js +++ b/lib/index.js @@ -106,6 +106,53 @@ Index.prototype.create = function create(documents, options, callback) { } }; +/** + * Destroy documents in index. + * + * @param {String|String[]} values + * @param {Function} callback + */ + +Index.prototype.destroy = function destroy(values, options, callback) { + var index = this; + + // search(query, callback) + if (! callback) { + callback = options; + options = {}; + } + + // Default field to id. + options.field = options.field || 'id'; + + // Accept single value. + values = _.isArray(values) ? values : [values]; + + // Destroy documents in each clients. + async.each(this.indexers, destroyInClient, callback); + + /** + * Create document in OSS client. + * + * @param {OssClient} client + * @param {Function} callback + */ + + function destroyInClient(client, callback) { + var opts = { + field: options.field, + values: values + }; + + client.documents.destroy(index.name, opts, function (err) { + // Errors are not blocking indexation, we just emit an event. + if (err) index.emit('error', err, client, index.name, opts); + else index.emit('destroy', client, index.name, opts); + callback(); + }); + } +}; + /** * Search in the index. * diff --git a/test/index.js b/test/index.js index f4d23af..a353e23 100644 --- a/test/index.js +++ b/test/index.js @@ -125,6 +125,94 @@ describe('Index', function () { }); }); + describe('#destroy', function () { + var indexer1, indexer2, index, values; + + beforeEach(function () { + indexer1 = createOssClient(); + indexer2 = createOssClient(); + + index = new Index({ + name: 'my_index', + indexers: [indexer1, indexer2] + }); + + values = [34928, 81238]; + + function createOssClient() { + var client = oss.createClient(); + sinon.stub(client.documents, 'destroy').yields(); + return client; + } + }); + + it('should destroy documents on each indexer', function (done) { + index.destroy(values, function (err) { + if (err) return done(err); + expect(indexer1.documents.destroy).to.be.calledWith('my_index', { + field: 'id', + values: values + }); + expect(indexer2.documents.destroy).to.be.calledWith('my_index', { + field: 'id', + values: values + }); + done(); + }); + }); + + it('should emit "destroy" event', function (done) { + var spy = sinon.spy(); + index.on('destroy', spy); + + index.destroy(values, function (err) { + if (err) return done(err); + expect(spy).to.be.calledWith(indexer1, 'my_index', { + field: 'id', + values: values + }); + expect(spy).to.be.calledWith(indexer2, 'my_index', { + field: 'id', + values: values + }); + done(); + }); + }); + + it('should emit an "error" event', function (done) { + var spy = sinon.spy(); + index.on('error', spy); + + var indexError = new Error('Indexing error.'); + indexer1.documents.destroy.restore(); + sinon.stub(indexer1.documents, 'destroy').yields(indexError); + + index.destroy(values, function (err) { + if (err) return done(err); + expect(spy).to.be.calledWith(indexError, indexer1, 'my_index', { + field: 'id', + values: values + }); + done(); + }); + }); + + it('should be possible to add options', function (done) { + index.destroy(values, { field: 'id_test' }, function (err) { + if (err) return done(err); + expect(indexer1.documents.destroy).to.be.calledWith('my_index', { + field: 'id_test', + values: values + }); + expect(indexer2.documents.destroy).to.be.calledWith('my_index', { + field: 'id_test', + values: values + }); + done(); + }); + }); + }); + describe('#search', function () { var index, searcher, searchResult;