diff --git a/x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/elasticsearch_assets/entity_index.ts b/x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/elasticsearch_assets/entity_index.ts index 7bc139ea08adf..6fb5935618dfc 100644 --- a/x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/elasticsearch_assets/entity_index.ts +++ b/x-pack/plugins/security_solution/server/lib/entity_analytics/entity_store/elasticsearch_assets/entity_index.ts @@ -8,6 +8,7 @@ import type { ElasticsearchClient, Logger } from '@kbn/core/server'; import type { EntityType } from '../../../../../common/api/entity_analytics'; import { getEntitiesIndexName } from '../utils'; +import { createOrUpdateIndex } from '../../utils/create_or_update_index'; interface Options { entityType: EntityType; @@ -17,18 +18,13 @@ interface Options { } export const createEntityIndex = async ({ entityType, esClient, namespace, logger }: Options) => { - try { - await esClient.indices.create({ + await createOrUpdateIndex({ + esClient, + logger, + options: { index: getEntitiesIndexName(entityType, namespace), - body: {}, - }); - } catch (e) { - if (e.meta.body.error.type === 'resource_already_exists_exception') { - logger.debug(`Index for ${entityType} already exists, skipping creation.`); - } else { - throw e; - } - } + }, + }); }; export const deleteEntityIndex = ({ entityType, esClient, namespace }: Options) => diff --git a/x-pack/plugins/security_solution/server/lib/entity_analytics/utils/create_or_update_index.ts b/x-pack/plugins/security_solution/server/lib/entity_analytics/utils/create_or_update_index.ts index f9525e14ac6c4..b6e49017c95a7 100644 --- a/x-pack/plugins/security_solution/server/lib/entity_analytics/utils/create_or_update_index.ts +++ b/x-pack/plugins/security_solution/server/lib/entity_analytics/utils/create_or_update_index.ts @@ -51,7 +51,16 @@ export const createOrUpdateIndex = async ({ ); } } else { - return esClient.indices.create(options); + try { + await esClient.indices.create(options); + } catch (err) { + // If the index already exists, we can ignore the error + if (err?.meta?.body?.error?.type === 'resource_already_exists_exception') { + logger.info(`${options.index} already exists`); + } else { + throw err; + } + } } } catch (err) { const error = transformError(err);