Skip to content

Commit 775a72a

Browse files
author
Tim Roes
committed
Use new elasticsearch client
1 parent 6822643 commit 775a72a

2 files changed

Lines changed: 39 additions & 39 deletions

File tree

x-pack/plugins/graph/server/routes/explore.ts

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,18 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { errors } from '@elastic/elasticsearch';
78
import { IRouter } from 'kibana/server';
89
import { schema } from '@kbn/config-schema';
910
import Boom from '@hapi/boom';
10-
import { get } from 'lodash';
1111
import { LicenseState, verifyApiAccess } from '../lib/license_state';
1212

13+
interface ErrorResponse {
14+
error?: {
15+
root_cause?: Array<{ type: string; reason: string }>;
16+
};
17+
}
18+
1319
export 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({

x-pack/plugins/graph/server/routes/search.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7+
import { errors } from '@elastic/elasticsearch';
78
import { IRouter } from 'kibana/server';
89
import { schema } from '@kbn/config-schema';
910
import { LicenseState, verifyApiAccess } from '../lib/license_state';
@@ -31,11 +32,7 @@ export function registerSearchRoute({
3132
{
3233
core: {
3334
uiSettings: { client: uiSettings },
34-
elasticsearch: {
35-
legacy: {
36-
client: { callAsCurrentUser: callCluster },
37-
},
38-
},
35+
elasticsearch: { client: esClient },
3936
},
4037
},
4138
request,
@@ -47,12 +44,14 @@ export function registerSearchRoute({
4744
try {
4845
return response.ok({
4946
body: {
50-
resp: await callCluster('search', {
51-
index: request.body.index,
52-
body: request.body.body,
53-
rest_total_hits_as_int: true,
54-
ignore_throttled: !includeFrozen,
55-
}),
47+
resp: (
48+
await esClient.asCurrentUser.search({
49+
index: request.body.index,
50+
body: request.body.body,
51+
rest_total_hits_as_int: true,
52+
ignore_throttled: !includeFrozen,
53+
})
54+
).body,
5655
},
5756
});
5857
} catch (error) {

0 commit comments

Comments
 (0)