From 85ac08da4349a5c607f8c11c9ddf97d2ef922d07 Mon Sep 17 00:00:00 2001 From: Gianfranco Paoloni Date: Wed, 11 Sep 2024 18:17:52 -0300 Subject: [PATCH 1/2] chore: resources index lambda point to shared ES instance --- resources-domain/lambdas/search-index/index.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/resources-domain/lambdas/search-index/index.ts b/resources-domain/lambdas/search-index/index.ts index 98283d5db..44f5a3337 100644 --- a/resources-domain/lambdas/search-index/index.ts +++ b/resources-domain/lambdas/search-index/index.ts @@ -83,7 +83,11 @@ export const executeBulk = async ( Object.keys(documentsByAccountSid).map(async accountSid => { const documents = documentsByAccountSid[accountSid]; const client = ( - await getClient({ accountSid, indexType: RESOURCE_INDEX_TYPE }) + await getClient({ + accountSid, + indexType: RESOURCE_INDEX_TYPE, + ssmConfigParameter: process.env.SSM_PARAM_ELASTICSEARCH_CONFIG, + }) ).indexClient(resourceIndexConfiguration); try { const indexResp = await client.executeBulk({ documents }); From 60a08178a5bc1defb83fb745e49ef1e6307b5de6 Mon Sep 17 00:00:00 2001 From: Gianfranco Paoloni Date: Wed, 11 Sep 2024 18:29:52 -0300 Subject: [PATCH 2/2] chore: add support for autocreate to resources index --- packages/elasticsearch-client/src/executeBulk.ts | 9 +++++++++ resources-domain/lambdas/search-index/index.ts | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/elasticsearch-client/src/executeBulk.ts b/packages/elasticsearch-client/src/executeBulk.ts index 3e3f2b579..ee9ae3514 100644 --- a/packages/elasticsearch-client/src/executeBulk.ts +++ b/packages/elasticsearch-client/src/executeBulk.ts @@ -15,6 +15,7 @@ */ import { BulkRequest, BulkResponse } from '@elastic/elasticsearch/lib/api/types'; import { PassThroughConfig } from './client'; +import createIndex from './createIndex'; export type BulkOperation = { id: string; @@ -32,6 +33,7 @@ export type BulkOperations = BulkOperation[]; export type ExecuteBulkExtraParams = { documents: BulkOperations; + autocreate?: boolean; }; export type ExecuteBulkParams = PassThroughConfig & ExecuteBulkExtraParams; @@ -43,7 +45,14 @@ export const executeBulk = async ({ index, indexConfig, documents, + autocreate = false, }: ExecuteBulkParams): Promise => { + 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') { diff --git a/resources-domain/lambdas/search-index/index.ts b/resources-domain/lambdas/search-index/index.ts index 44f5a3337..7583b3b17 100644 --- a/resources-domain/lambdas/search-index/index.ts +++ b/resources-domain/lambdas/search-index/index.ts @@ -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);