import { OpenSearchQuerySortValue , IndexPattern } from '../../../../opensearch_dashboards_services' ; import { SortOrder } from '../components/table_header/helpers' ; import { getSort } from './get_sort' ; import { getDefaultSort } from './get_default_sort' ; /**
 * Prepares sort for search source, that's sending the request to OpenSearch
 * - Adds default sort if necessary
 * - Handles the special case when there's sorting by date_nanos typed fields
 *   the addon of the numeric_type guarantees the right sort order
 *   when there are indices with date and indices with date_nanos field
 */ export function getSortForSearchSource ( sort ? , indexPattern ? , defaultDirection = 'desc' ) { if ( ! sort || ! indexPattern ) { return [ ] ; } else if ( Array . isArray ( sort ) && sort . length === 0 ) { sort = getDefaultSort ( indexPattern , defaultDirection ) ; } const { timeFieldName } = indexPattern ; return getSort ( sort , indexPattern ) . map ( ( sortPair ) => { if ( indexPattern . isTimeNanosBased ( ) && timeFieldName && sortPair [ timeFieldName ] ) { return { [ timeFieldName ] : { order : sortPair [ timeFieldName ] , numeric_type : 'date_nanos' , } , } as OpenSearchQuerySortValue ; } return sortPair as OpenSearchQuerySortValue ; } ) ; }