44 * you may not use this file except in compliance with the Elastic License.
55 */
66
7+ import { errors } from '@elastic/elasticsearch' ;
78import { IRouter } from 'kibana/server' ;
89import { schema } from '@kbn/config-schema' ;
910import Boom from '@hapi/boom' ;
10- import { get } from 'lodash' ;
1111import { LicenseState , verifyApiAccess } from '../lib/license_state' ;
1212
13+ interface ErrorResponse {
14+ error ?: {
15+ root_cause ?: Array < { type : string ; reason : string } > ;
16+ } ;
17+ }
18+
1319export function registerExploreRoute ( {
1420 router,
1521 licenseState,
@@ -31,11 +37,7 @@ export function registerExploreRoute({
3137 async (
3238 {
3339 core : {
34- elasticsearch : {
35- legacy : {
36- client : { callAsCurrentUser : callCluster } ,
37- } ,
38- } ,
40+ elasticsearch : { client : esClient } ,
3941 } ,
4042 } ,
4143 request ,
@@ -46,32 +48,31 @@ export function registerExploreRoute({
4648 try {
4749 return response . ok ( {
4850 body : {
49- resp : await callCluster ( 'transport.request' , {
50- path : '/' + encodeURIComponent ( request . body . index ) + '/_graph/explore' ,
51- body : request . body . query ,
52- method : 'POST' ,
53- query : { } ,
54- } ) ,
51+ resp : (
52+ await esClient . asCurrentUser . transport . request ( {
53+ path : '/' + encodeURIComponent ( request . body . index ) + '/_graph/explore' ,
54+ body : request . body . query ,
55+ method : 'POST' ,
56+ } )
57+ ) . body ,
5558 } ,
5659 } ) ;
5760 } catch ( error ) {
58- // Extract known reasons for bad choice of field
59- const relevantCause = get (
60- error ,
61- 'body.error.root_cause' ,
62- [ ] as Array < { type : string ; reason : string } >
63- ) . find ( ( cause : { type : string ; reason : string } ) => {
64- return (
65- cause . reason . includes ( 'Fielddata is disabled on text fields' ) ||
66- cause . reason . includes ( 'No support for examining floating point' ) ||
67- cause . reason . includes ( 'Sample diversifying key must be a single valued-field' ) ||
68- cause . reason . includes ( 'Failed to parse query' ) ||
69- cause . type === 'parsing_exception'
70- ) ;
71- } ) ;
61+ if ( error instanceof errors . ResponseError ) {
62+ const errorBody : ErrorResponse = error . body ;
63+ const relevantCause = ( errorBody . error ?. root_cause ?? [ ] ) . find ( ( cause ) => {
64+ return (
65+ cause . reason . includes ( 'Fielddata is disabled on text fields' ) ||
66+ cause . reason . includes ( 'No support for examining floating point' ) ||
67+ cause . reason . includes ( 'Sample diversifying key must be a single valued-field' ) ||
68+ cause . reason . includes ( 'Failed to parse query' ) ||
69+ cause . type === 'parsing_exception'
70+ ) ;
71+ } ) ;
7272
73- if ( relevantCause ) {
74- throw Boom . badRequest ( relevantCause . reason ) ;
73+ if ( relevantCause ) {
74+ throw Boom . badRequest ( relevantCause . reason ) ;
75+ }
7576 }
7677
7778 return response . internalError ( {
0 commit comments