From 1b3689d13ce2165900770b8255aac14e90d0f47a Mon Sep 17 00:00:00 2001 From: Viduni Wickramarachchi Date: Wed, 21 May 2025 17:18:06 -0400 Subject: [PATCH] [Obs AI Assistant] Check for documents before starting semantic text migration (#221152) Closes https://github.com/elastic/kibana/issues/221157 ## Summary We run a semantic text migration at startup to add the semantic text field to documents that were created before 8.17. Before multilingual KB was introduced: - We created index assets for KB when the AI Assistant flyout opens. - Even if the user does not set up the KB, they will have a component template pointing to the custom inference endpoint. With the introduction of multilingual KB: - We moved some of the index creation to when setting up the KB. - We try to do the semantic_text migration at startup. During this migration, for users who didn't set up the KB but had the index assets created at startup, the custom inference endpoint will be unavailable. - But since the migration uses the inference endpoint from the write index, we try to access an endpoint that's not available. This is the reason for this error to be logged. ``` Inference endpoint "obs_ai_assistant_kb_inference" not found or unavailable: resource_not_found_exception Root causes: resource_not_found_exception: Inference endpoint not found [obs_ai_assistant_kb_inference] ``` There is no customer impact from this, just that the error that gets logged is creating a lot of noise. ## Solution This PR checks whether there are documents without semantic_text before starting the migration. And also reduced the log level to warn because we hit the `/status` endpoint once when a user opens the AI Assistant. ### Checklist - [x] The PR description includes the appropriate Release Notes section, and the correct `release_note:*` label is applied per the [guidelines](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process) (cherry picked from commit b1e7012477af991632a0ad2fdfccad743bcac3c6) --- .../server/service/inference_endpoint.ts | 2 +- .../populate_missing_semantic_text_fields.ts | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/inference_endpoint.ts b/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/inference_endpoint.ts index e26e4248d28ce..422d70fe8cc5d 100644 --- a/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/inference_endpoint.ts +++ b/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/inference_endpoint.ts @@ -154,7 +154,7 @@ export async function getKbModelStatus({ if (!isInferenceEndpointMissingOrUnavailable(error)) { throw error; } - logger.error(`Inference endpoint "${inferenceId}" not found or unavailable: ${error.message}`); + logger.warn(`Inference endpoint "${inferenceId}" not found or unavailable: ${error.message}`); return { enabled, diff --git a/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/startup_migrations/populate_missing_semantic_text_fields.ts b/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/startup_migrations/populate_missing_semantic_text_fields.ts index 50792fe424796..9135cf2a857b5 100644 --- a/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/startup_migrations/populate_missing_semantic_text_fields.ts +++ b/x-pack/platform/plugins/shared/observability_ai_assistant/server/service/startup_migrations/populate_missing_semantic_text_fields.ts @@ -47,6 +47,23 @@ async function populateMissingSemanticTextField({ }) { logger.debug('Initalizing semantic text migration for knowledge base entries...'); + const { count } = await esClient.asInternalUser.count({ + index: resourceNames.writeIndexAlias.kb, + query: { + bool: { + must_not: { + exists: { field: 'semantic_text' }, + }, + }, + }, + }); + logger.info(`Documents missing 'semantic_text' before migration: ${count}`); + + if (count === 0) { + logger.debug('No documents missing semantic_text field, skipping migration.'); + return; + } + await pRetry( async () => { const inferenceId = await getInferenceIdFromWriteIndex(esClient);