Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow document to be overwritten in onDocument iteratee of bulk helper #263

Merged
merged 1 commit into from
Jul 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/Helpers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ export interface BulkStats {
aborted: boolean;
}

interface IndexAction {
interface IndexActionOperation {
index: {
_index: string;
[key: string]: any;
};
}

interface CreateAction {
interface CreateActionOperation {
create: {
_index: string;
[key: string]: any;
Expand All @@ -110,6 +110,8 @@ interface DeleteAction {
};
}

type CreateAction = CreateActionOperation | [CreateActionOperation, unknown];
type IndexAction = IndexActionOperation | [IndexActionOperation, unknown];
type UpdateAction = [UpdateActionOperation, Record<string, any>];
type Action = IndexAction | CreateAction | UpdateAction | DeleteAction;
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
Expand Down
13 changes: 6 additions & 7 deletions lib/Helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,21 +532,20 @@ class Helpers {
for await (const chunk of datasource) {
if (shouldAbort === true) break;
timeoutRef.refresh();
const action = onDocument(chunk);
const operation = Array.isArray(action)
? Object.keys(action[0])[0]
: Object.keys(action)[0];
const result = onDocument(chunk);
const [action, payload] = Array.isArray(result) ? result : [result, chunk];
const operation = Object.keys(action)[0];
if (operation === 'index' || operation === 'create') {
actionBody = serializer.serialize(action);
payloadBody = typeof chunk === 'string' ? chunk : serializer.serialize(chunk);
payloadBody = typeof payload === 'string' ? payload : serializer.serialize(payload);
chunkBytes += Buffer.byteLength(actionBody) + Buffer.byteLength(payloadBody);
bulkBody.push(actionBody, payloadBody);
} else if (operation === 'update') {
actionBody = serializer.serialize(action[0]);
actionBody = serializer.serialize(action);
payloadBody =
typeof chunk === 'string'
? `{"doc":${chunk}}`
: serializer.serialize({ doc: chunk, ...action[1] });
: serializer.serialize({ doc: chunk, ...payload });
chunkBytes += Buffer.byteLength(actionBody) + Buffer.byteLength(payloadBody);
bulkBody.push(actionBody, payloadBody);
} else if (operation === 'delete') {
Expand Down
101 changes: 101 additions & 0 deletions test/unit/helpers/bulk.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,56 @@ test('bulk index', (t) => {
});
});

t.test('Should use payload returned by `onDocument`', async (t) => {
let count = 0;
const updatedAt = '1970-01-01T12:00:00.000Z';
const MockConnection = connection.buildMockConnection({
onRequest(params) {
t.equal(params.path, '/_bulk');
t.match(params.headers, {
'content-type': 'application/x-ndjson',
});
const [action, payload] = params.body.split('\n');
t.same(JSON.parse(action), { index: { _index: 'test' } });
t.same(JSON.parse(payload), { ...dataset[count++], updatedAt });
return { body: { errors: false, items: [{}] } };
},
});

const client = new Client({
node: 'http://localhost:9200',
Connection: MockConnection,
});
const result = await client.helpers.bulk({
datasource: dataset.slice(),
flushBytes: 1,
concurrency: 1,
onDocument(doc) {
return [
{
index: {
_index: 'test',
},
},
{ ...doc, updatedAt },
];
},
onDrop(doc) {
t.fail('This should never be called');
},
});

t.type(result.time, 'number');
t.type(result.bytes, 'number');
t.match(result, {
total: 3,
successful: 3,
retry: 0,
failed: 0,
aborted: false,
});
});

t.end();
});

Expand Down Expand Up @@ -844,6 +894,57 @@ test('bulk create', (t) => {
aborted: false,
});
});

t.test('Should perform a bulk request', async (t) => {
let count = 0;
const updatedAt = '1970-01-01T12:00:00.000Z';
const MockConnection = connection.buildMockConnection({
onRequest(params) {
t.equal(params.path, '/_bulk');
t.match(params.headers, { 'content-type': 'application/x-ndjson' });
const [action, payload] = params.body.split('\n');
t.same(JSON.parse(action), { create: { _index: 'test', _id: count } });
t.same(JSON.parse(payload), { ...dataset[count++], updatedAt });
return { body: { errors: false, items: [{}] } };
},
});

const client = new Client({
node: 'http://localhost:9200',
Connection: MockConnection,
});
let id = 0;
const result = await client.helpers.bulk({
datasource: dataset.slice(),
flushBytes: 1,
concurrency: 1,
onDocument(doc) {
return [
{
create: {
_index: 'test',
_id: id++,
},
},
{ ...doc, updatedAt },
];
},
onDrop(doc) {
t.fail('This should never be called');
},
});

t.type(result.time, 'number');
t.type(result.bytes, 'number');
t.match(result, {
total: 3,
successful: 3,
retry: 0,
failed: 0,
aborted: false,
});
});

t.end();
});

Expand Down