Skip to content

Commit

Permalink
Merge pull request techmatters#742 from techmatters/gian_CHI-2827
Browse files Browse the repository at this point in the history
chore: Migrate resource search to shared ES instance [CHI-2827]
  • Loading branch information
GPaoloni authored Sep 12, 2024
2 parents 31cb92a + 60a0817 commit f066614
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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
8 changes: 6 additions & 2 deletions resources-domain/lambdas/search-index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,14 @@ 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 });
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 f066614

Please sign in to comment.