diff --git a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_overall_stats.ts b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_overall_stats.ts index 599839f3aa78e..cb72592742d44 100644 --- a/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_overall_stats.ts +++ b/x-pack/plugins/data_visualizer/public/application/index_data_visualizer/hooks/use_overall_stats.ts @@ -19,6 +19,7 @@ import { getProcessedFields } from '@kbn/ml-data-grid'; import { buildBaseFilterCriteria } from '@kbn/ml-query-utils'; import { isDefined } from '@kbn/ml-is-defined'; import type { FieldSpec } from '@kbn/data-views-plugin/common'; +import type { MappingRuntimeFields } from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; import { useDataVisualizerKibana } from '../../kibana_context'; import type { AggregatableFieldOverallStats, @@ -51,6 +52,18 @@ import { fetchDataWithTimeout, rateLimitingForkJoin, } from '../search_strategy/requests/fetch_utils'; + +const getPopulatedFieldsInIndex = ( + populatedFieldsInIndexWithoutRuntimeFields: Set | undefined | null, + runtimeFieldMap: MappingRuntimeFields | undefined +): Set | undefined | null => { + if (!populatedFieldsInIndexWithoutRuntimeFields) return undefined; + const runtimeFields = runtimeFieldMap ? Object.keys(runtimeFieldMap) : undefined; + return runtimeFields && runtimeFields?.length > 0 + ? new Set([...Array.from(populatedFieldsInIndexWithoutRuntimeFields), ...runtimeFields]) + : populatedFieldsInIndexWithoutRuntimeFields; +}; + export function useOverallStats( esql = false, searchStrategyParams: TParams | undefined, @@ -68,7 +81,7 @@ export function useOverallStats(getDefaultPageState().overallStats); - const [populatedFieldsInIndex, setPopulatedFieldsInIndex] = useState< + const [populatedFieldsInIndexWithoutRuntimeFields, setPopulatedFieldsInIndex] = useState< | Set // request to fields caps has not been made yet | undefined @@ -92,10 +105,9 @@ export function useOverallStats { populatedFieldsAbortCtrl.current.abort(); @@ -131,15 +143,7 @@ export function useOverallStats field.name), - // Field caps API don't know about runtime fields - // so by default we expect runtime fields to be populated - // so we can later check as needed - ...Object.keys(runtimeFieldMap ?? {}), - ]) - ); + setPopulatedFieldsInIndex(new Set([...nonEmptyFields.map((field) => field.name)])); } else { setPopulatedFieldsInIndex(null); } @@ -154,25 +158,33 @@ export function useOverallStats { try { searchSubscription$.current?.unsubscribe(); abortCtrl.current.abort(); abortCtrl.current = new AbortController(); - if (!searchStrategyParams || lastRefresh === 0 || populatedFieldsInIndex === undefined) { + if ( + !searchStrategyParams || + lastRefresh === 0 || + populatedFieldsInIndexWithoutRuntimeFields === undefined + ) { return; } + const populatedFieldsInIndex = getPopulatedFieldsInIndex( + populatedFieldsInIndexWithoutRuntimeFields, + searchStrategyParams.runtimeFieldMap + ); + setFetchState({ ...getInitialProgress(), isRunning: true, @@ -391,7 +403,14 @@ export function useOverallStats { searchSubscription$.current?.unsubscribe();