Skip to content

Commit

Permalink
chore: add support for autocreate to resources index
Browse files Browse the repository at this point in the history
  • Loading branch information
GPaoloni committed Sep 11, 2024
1 parent 85ac08d commit 60a0817
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/elasticsearch-client/src/executeBulk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
import { BulkRequest, BulkResponse } from '@elastic/elasticsearch/lib/api/types';
import { PassThroughConfig } from './client';
import createIndex from './createIndex';

export type BulkOperation<T> = {
id: string;
Expand All @@ -32,6 +33,7 @@ export type BulkOperations<T> = BulkOperation<T>[];

export type ExecuteBulkExtraParams<T> = {
documents: BulkOperations<T>;
autocreate?: boolean;
};

export type ExecuteBulkParams<T> = PassThroughConfig<T> & ExecuteBulkExtraParams<T>;
Expand All @@ -43,7 +45,14 @@ export const executeBulk = async <T>({
index,
indexConfig,
documents,
autocreate = false,
}: ExecuteBulkParams<T>): Promise<ExecuteBulkResponse> => {
if (autocreate) {
// const exists = await client.indices.exists({ index });
// NOTE: above check is already performed in createIndex
await createIndex({ client, index, indexConfig });
}

const body: BulkRequest['operations'] = documents.flatMap(
(documentItem): BulkRequest['operations'] => {
if (documentItem.action === 'delete') {
Expand Down
2 changes: 1 addition & 1 deletion resources-domain/lambdas/search-index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export const executeBulk = async (
})
).indexClient(resourceIndexConfiguration);
try {
const indexResp = await client.executeBulk({ documents });
const indexResp = await client.executeBulk({ documents, autocreate: true });
await handleErrors(indexResp, addDocumentIdToFailures);
} catch (err) {
console.error(new ResourceIndexProcessorError('Error calling executeBulk'), err);
Expand Down

0 comments on commit 60a0817

Please sign in to comment.