import { overwrite } from '../../helpers' ; import { validateField } from '../../../../../common/fields_utils' ; import type { AnnotationsRequestProcessorsFunction } from './types' ; export const topHits = ( { panel , annotation , annotationIndex , } ) => { return ( next ) => ( doc ) => { const fields = ( annotation . fields && annotation . fields . split ( /[,\s]+/ ) ) || [ ] ; const timeField = annotation . time_field || annotationIndex . indexPattern ?. timeFieldName || '' ; if ( panel . use_kibana_indexes ) { validateField ( timeField , annotationIndex ) ; } overwrite ( doc , `aggs.${ annotation . id }.aggs.hits.top_hits` , { sort : [ { [ timeField ] : { order : 'desc' } , } , ] , fields : [ ... fields , timeField ] , size : 5 , } ) ; return next ( doc ) ; } ; } ;