diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/util/get_access_query.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/util/get_access_query.ts index c01fb4422cc66..b517273630f56 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/util/get_access_query.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/server/service/util/get_access_query.ts @@ -17,22 +17,7 @@ export function getAccessQuery({ filter: [ { bool: { - should: [ - { term: { public: true } }, - ...(user - ? user.id - ? [ - { term: { 'user.id': user.id } }, - { - bool: { - must_not: { exists: { field: 'user.id' } }, - must: { term: { 'user.name': user.name } }, - }, - }, - ] - : [{ term: { 'user.name': user.name } }] - : []), - ], + should: [{ term: { public: true } }, ...getUserAccessFilters(user)], minimum_should_match: 1, }, }, @@ -61,3 +46,23 @@ export function getAccessQuery({ }, ]; } + +function getUserAccessFilters(user?: { name: string; id?: string }) { + if (!user) { + return []; + } + + if (user.id) { + return [ + { term: { 'user.id': user.id } }, + { + bool: { + must_not: { exists: { field: 'user.id' } }, + must: { term: { 'user.name': user.name } }, + }, + }, + ]; + } + + return [{ term: { 'user.name': user.name } }]; +}