@@ -23,7 +23,7 @@ import { i18n } from '@kbn/i18n';
2323import moment from 'moment' ;
2424import { isEqual , memoize } from 'lodash' ;
2525import { Global , css } from '@emotion/react' ;
26- import { getESQLQueryColumns } from '@kbn/esql-utils' ;
26+ import { getESQLQueryColumns , getIndexPatternFromESQLQuery } from '@kbn/esql-utils' ;
2727import type { CodeEditorProps } from '@kbn/code-editor' ;
2828import { CodeEditor } from '@kbn/code-editor' ;
2929import type { CoreStart } from '@kbn/core/public' ;
@@ -547,9 +547,20 @@ const ESQLEditorInternal = function ESQLEditor({
547547
548548 const canCreateLookupIndex = useCanCreateLookupIndex ( ) ;
549549
550+ // Extract source command and build minimal query with cluster prefixes
551+ const minimalQuery = useMemo ( ( ) => {
552+ const prefix = code . match ( / \b ( F R O M | T S ) \b / i) ?. [ 1 ] ?. toUpperCase ( ) ;
553+ const indexPattern = getIndexPatternFromESQLQuery ( code ) ;
554+
555+ return prefix && indexPattern ? `${ prefix } ${ indexPattern } ` : '' ;
556+ } , [ code ] ) ;
557+
558+ const minimalQueryRef = useRef ( minimalQuery ) ;
559+ minimalQueryRef . current = minimalQuery ;
560+
550561 const getJoinIndices = useCallback < Required < ESQLCallbacks > [ 'getJoinIndices' ] > (
551562 async ( cacheOptions ) => {
552- const remoteClusters = getRemoteClustersFromESQLQuery ( code ) ;
563+ const remoteClusters = getRemoteClustersFromESQLQuery ( minimalQueryRef . current ) ;
553564 let result : IndicesAutocompleteResult = { indices : [ ] } ;
554565 if ( kibana . services ?. esql ?. getJoinIndicesAutocomplete ) {
555566 result = await kibana . services . esql . getJoinIndicesAutocomplete . call (
@@ -559,7 +570,7 @@ const ESQLEditorInternal = function ESQLEditor({
559570 }
560571 return result ;
561572 } ,
562- [ code , kibana ?. services ?. esql ?. getJoinIndicesAutocomplete ]
573+ [ kibana ?. services ?. esql ?. getJoinIndicesAutocomplete ]
563574 ) ;
564575
565576 const telemetryCallbacks = useMemo < ESQLTelemetryCallbacks > (
@@ -583,7 +594,7 @@ const ESQLEditorInternal = function ESQLEditor({
583594 const esqlCallbacks = useMemo < ESQLCallbacks > ( ( ) => {
584595 const callbacks : ESQLCallbacks = {
585596 getSources : async ( ) => {
586- clearCacheWhenOld ( dataSourcesCache , fixedQuery ) ;
597+ clearCacheWhenOld ( dataSourcesCache , minimalQueryRef . current ) ;
587598 const getLicense = kibana . services ?. esql ?. getLicense ;
588599 const sources = await memoizedSources ( core , getLicense ) . result ;
589600 return sources ;
@@ -688,7 +699,6 @@ const ESQLEditorInternal = function ESQLEditor({
688699 favoritesClient ,
689700 kibana . services ?. esql ,
690701 dataSourcesCache ,
691- fixedQuery ,
692702 memoizedSources ,
693703 core ,
694704 esqlFieldsCache ,
0 commit comments