import type { IndexPatternField } from 'src/plugins/data/public' ; import { FIELD_ORIGIN } from '../../../common/constants' ; import { ESTooltipProperty } from '../tooltips/es_tooltip_property' ; import { ITooltipProperty , TooltipProperty } from '../tooltips/tooltip_property' ; import { indexPatterns } from '../../../../../../src/plugins/data/public' ; import { IField , AbstractField } from './field' ; import { IESSource } from '../sources/es_source' ; import { IVectorSource } from '../sources/vector_source' ; export class ESDocField extends AbstractField implements IField { private readonly _source : IESSource ; constructor ( { fieldName , source , origin , } ) { super ( { fieldName , origin } ) ; this . _source = source ; } supportsFieldMetaFromEs ( ) { return true ; } supportsFieldMetaFromLocalData ( ) { return ! this . getSource ( ) . isMvt ( ) ; } canValueBeFormatted ( ) { return true ; } getSource ( ) { return this . _source ; } async _getIndexPatternField ( ) { const indexPattern = await this . _source . getIndexPattern ( ) ; const indexPatternField = indexPattern . fields . getByName ( this . getName ( ) ) ; return indexPatternField && indexPatterns . isNestedField ( indexPatternField ) ? undefined : indexPatternField ; } async createTooltipProperty ( value ) { const indexPattern = await this . _source . getIndexPattern ( ) ; const tooltipProperty = new TooltipProperty ( this . getName ( ) , await this . getLabel ( ) , value ) ; return new ESTooltipProperty ( tooltipProperty , indexPattern , this as IField , this . _source . getApplyGlobalQuery ( ) ) ; } async getDataType ( ) { const indexPatternField = await this . _getIndexPatternField ( ) ; return indexPatternField ? indexPatternField . type : '' ; } async getLabel ( ) { const indexPatternField = await this . _getIndexPatternField ( ) ; return indexPatternField && indexPatternField . displayName ? indexPatternField . displayName : super . getLabel ( ) ; } async getExtendedStatsFieldMetaRequest ( ) { const indexPatternField = await this . _getIndexPatternField ( ) ; if ( ! indexPatternField || ( indexPatternField . type !== 'number' && indexPatternField . type !== 'date' ) ) { return null ; } const metricAggConfig = { } ; if ( indexPatternField . scripted ) { metricAggConfig . script = { source : indexPatternField . script , lang : indexPatternField . lang , } ; } else { metricAggConfig . field = this . getName ( ) ; } return { [ `${ this . getName ( ) }_range` ] : { extended_stats : metricAggConfig , } , } ; } async getPercentilesFieldMetaRequest ( percentiles ) { const indexPatternField = await this . _getIndexPatternField ( ) ; if ( ! indexPatternField || indexPatternField . type !== 'number' ) { return null ; } const metricAggConfig = { percents : [ 0 , ... percentiles ] , } ; if ( indexPatternField . scripted ) { metricAggConfig . script = { source : indexPatternField . script , lang : indexPatternField . lang , } ; } else { metricAggConfig . field = this . getName ( ) ; } return { [ `${ this . getName ( ) }_percentiles` ] : { percentiles : metricAggConfig , } , } ; } async getCategoricalFieldMetaRequest ( size ) { const indexPatternField = await this . _getIndexPatternField ( ) ; if ( ! indexPatternField || size <= 0 ) { return null ; } const topTerms = { size : size - 1 , } ; if ( indexPatternField . scripted ) { topTerms . script = { source : indexPatternField . script , lang : indexPatternField . lang , } ; } else { topTerms . field = this . getName ( ) ; } return { [ `${ this . getName ( ) }_terms` ] : { terms : topTerms , } , } ; } }