Skip to content

Commit

Permalink
Merge pull request #2 from lemonde/index-destroy
Browse files Browse the repository at this point in the history
Add index.destroy() method
  • Loading branch information
gregberge committed Apr 3, 2014
2 parents f65483f + 98ab3cc commit 375d874
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
47 changes: 47 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
88 changes: 88 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 375d874

Please sign in to comment.