diff --git a/README.md b/README.md index b0d9bc8..c7c76b9 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ The records written to the stream has to have the following format: index: 'name-of-index', type: 'recordType', id: 'recordId', + parent: 'parentRecordType', //optional body: { name: 'Foo Bar' } diff --git a/index.js b/index.js index 11b8937..3c1f6d0 100644 --- a/index.js +++ b/index.js @@ -20,12 +20,18 @@ function transformRecords(records) { assert(record.type, 'type is required'); assert(record.body, 'body is required'); + var index = { + _index: record.index, + _type: record.type, + _id: record.id + }; + + if (record.parent) { + index._parent = record.parent; + } + bulkOperations.push({ - index: { - _index: record.index, - _type: record.type, - _id: record.id - } + index: index }); bulkOperations.push(record.body); diff --git a/test/elasticsearch-bulk-stream.js b/test/elasticsearch-bulk-stream.js index afc1cb6..0ecc17e 100644 --- a/test/elasticsearch-bulk-stream.js +++ b/test/elasticsearch-bulk-stream.js @@ -11,7 +11,9 @@ chai.use(sinonChai); var expect = chai.expect; var recordFixture = require('./fixture/record.json'); +var recordParentFixture = require('./fixture/parentrecord.json'); var successResponseFixture = require('./fixture/success-response.json'); +var successParentResponseFixture = require('./fixture/success-parent-response.json'); var errorResponseFixture = require('./fixture/error-response.json'); describe('ElastisearchBulkIndexWritable', function() { @@ -201,4 +203,43 @@ describe('ElastisearchBulkIndexWritable', function() { }.bind(this)); }); }); + + describe('parent type', function() { + beforeEach(function() { + this.client = { + bulk: this.sinon.stub() + }; + + this.stream = new ElasticsearchBulkIndexWritable(this.client, { + highWaterMark: 1, + flushTimeout: 10 + }); + + this.client.bulk.yields(null, successParentResponseFixture); + this.clock = sinon.useFakeTimers(); + }); + + it('should include parent type in record if present', function() { + this.stream.write(recordParentFixture); + + var expectedArgument = { + body: [ + { + index: { + _index: 'indexName', + _type: 'recordType', + _id: 'recordId', + _parent: 'parentRecordType' + } + }, + { + foo: 'bar' + } + ] + }; + + expect(this.client.bulk).to.have.callCount(1); + expect(this.client.bulk).to.have.been.calledWith(expectedArgument); + }); + }); }); diff --git a/test/fixture/parentrecord.json b/test/fixture/parentrecord.json new file mode 100644 index 0000000..97fbe81 --- /dev/null +++ b/test/fixture/parentrecord.json @@ -0,0 +1,9 @@ +{ + "index": "indexName", + "id": "recordId", + "type": "recordType", + "parent": "parentRecordType", + "body": { + "foo": "bar" + } +} \ No newline at end of file diff --git a/test/fixture/success-parent-response.json b/test/fixture/success-parent-response.json new file mode 100644 index 0000000..8cfbd16 --- /dev/null +++ b/test/fixture/success-parent-response.json @@ -0,0 +1,16 @@ +{ + "took": 30, + "errors": false, + "items": [ + { + "index": { + "_index": "indexName", + "_id": "recordId", + "_type": "recordType", + "_parent": "parentRecordType", + "_version": 5, + "status": 200 + } + } + ] +} \ No newline at end of file