55 */
66
77import Boom from '@hapi/boom' ;
8+ import { errors } from '@elastic/elasticsearch' ;
89import DateMath from '@elastic/datemath' ;
910import { schema } from '@kbn/config-schema' ;
1011import { CoreSetup } from 'src/core/server' ;
@@ -47,7 +48,7 @@ export async function initFieldsRoute(setup: CoreSetup<PluginStartContract>) {
4748 } ,
4849 } ,
4950 async ( context , req , res ) => {
50- const requestClient = context . core . elasticsearch . legacy . client ;
51+ const requestClient = context . core . elasticsearch . client . asCurrentUser ;
5152 const { fromDate, toDate, timeFieldName, field, dslQuery } = req . body ;
5253
5354 try {
@@ -71,18 +72,18 @@ export async function initFieldsRoute(setup: CoreSetup<PluginStartContract>) {
7172 } ,
7273 } ;
7374
74- const search = ( aggs : unknown ) =>
75- requestClient . callAsCurrentUser ( ' search' , {
75+ const search = async ( aggs : unknown ) => {
76+ const { body : result } = await requestClient . search ( {
7677 index : req . params . indexPatternTitle ,
78+ track_total_hits : true ,
7779 body : {
7880 query,
7981 aggs,
8082 } ,
81- // The hits total changed in 7.0 from number to object, unless this flag is set
82- // this is a workaround for elasticsearch response types that are from 6.x
83- restTotalHitsAsInt : true ,
8483 size : 0 ,
8584 } ) ;
85+ return result ;
86+ } ;
8687
8788 if ( field . type === 'number' ) {
8889 return res . ok ( {
@@ -98,7 +99,7 @@ export async function initFieldsRoute(setup: CoreSetup<PluginStartContract>) {
9899 body : await getStringSamples ( search , field ) ,
99100 } ) ;
100101 } catch ( e ) {
101- if ( e . status === 404 ) {
102+ if ( e instanceof errors . ResponseError && e . statusCode === 404 ) {
102103 return res . notFound ( ) ;
103104 }
104105 if ( e . isBoom ) {
@@ -142,8 +143,7 @@ export async function getNumberHistogram(
142143
143144 const minMaxResult = ( await aggSearchWithBody ( searchBody ) ) as ESSearchResponse <
144145 unknown ,
145- { body : { aggs : typeof searchBody } } ,
146- { restTotalHitsAsInt : true }
146+ { body : { aggs : typeof searchBody } }
147147 > ;
148148
149149 const minValue = minMaxResult . aggregations ! . sample . min_value . value ;
@@ -164,7 +164,7 @@ export async function getNumberHistogram(
164164
165165 if ( histogramInterval === 0 ) {
166166 return {
167- totalDocuments : minMaxResult . hits . total ,
167+ totalDocuments : minMaxResult . hits . total . value ,
168168 sampledValues : minMaxResult . aggregations ! . sample . sample_count . value ! ,
169169 sampledDocuments : minMaxResult . aggregations ! . sample . doc_count ,
170170 topValues : topValuesBuckets ,
@@ -187,12 +187,11 @@ export async function getNumberHistogram(
187187 } ;
188188 const histogramResult = ( await aggSearchWithBody ( histogramBody ) ) as ESSearchResponse <
189189 unknown ,
190- { body : { aggs : typeof histogramBody } } ,
191- { restTotalHitsAsInt : true }
190+ { body : { aggs : typeof histogramBody } }
192191 > ;
193192
194193 return {
195- totalDocuments : minMaxResult . hits . total ,
194+ totalDocuments : minMaxResult . hits . total . value ,
196195 sampledDocuments : minMaxResult . aggregations ! . sample . doc_count ,
197196 sampledValues : minMaxResult . aggregations ! . sample . sample_count . value ! ,
198197 histogram : {
@@ -227,12 +226,11 @@ export async function getStringSamples(
227226 } ;
228227 const topValuesResult = ( await aggSearchWithBody ( topValuesBody ) ) as ESSearchResponse <
229228 unknown ,
230- { body : { aggs : typeof topValuesBody } } ,
231- { restTotalHitsAsInt : true }
229+ { body : { aggs : typeof topValuesBody } }
232230 > ;
233231
234232 return {
235- totalDocuments : topValuesResult . hits . total ,
233+ totalDocuments : topValuesResult . hits . total . value ,
236234 sampledDocuments : topValuesResult . aggregations ! . sample . doc_count ,
237235 sampledValues : topValuesResult . aggregations ! . sample . sample_count . value ! ,
238236 topValues : {
@@ -275,12 +273,11 @@ export async function getDateHistogram(
275273 } ;
276274 const results = ( await aggSearchWithBody ( histogramBody ) ) as ESSearchResponse <
277275 unknown ,
278- { body : { aggs : typeof histogramBody } } ,
279- { restTotalHitsAsInt : true }
276+ { body : { aggs : typeof histogramBody } }
280277 > ;
281278
282279 return {
283- totalDocuments : results . hits . total ,
280+ totalDocuments : results . hits . total . value ,
284281 histogram : {
285282 buckets : results . aggregations ! . histo . buckets . map ( ( bucket ) => ( {
286283 count : bucket . doc_count ,
0 commit comments