mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
Consistent error messages between esdocs and essql (#26399)
* Added catch promise to esdocs * Extracted query to elasticsearch into a lib function
This commit is contained in:
parent
04a37c4654
commit
abeafa1ae1
3 changed files with 55 additions and 72 deletions
|
@ -5,10 +5,7 @@
|
|||
*/
|
||||
|
||||
import squel from 'squel';
|
||||
import { map, zipObject } from 'lodash';
|
||||
import { buildBoolArray } from '../../../../server/lib/build_bool_array';
|
||||
import { normalizeType } from '../../../../server/lib/normalize_type';
|
||||
import { sanitizeName } from '../../../../server/lib/sanitize_name';
|
||||
import { queryEsSQL } from '../../../../server/lib/query_es_sql';
|
||||
|
||||
export const esdocs = () => ({
|
||||
name: 'esdocs',
|
||||
|
@ -76,31 +73,10 @@ export const esdocs = () => ({
|
|||
if (sortField) query.order(`"${sortField}"`, sortOrder.toLowerCase() === 'asc');
|
||||
}
|
||||
|
||||
return handlers
|
||||
.elasticsearchClient('transport.request', {
|
||||
path: '/_xpack/sql?format=json',
|
||||
method: 'POST',
|
||||
body: {
|
||||
fetch_size: args.count,
|
||||
query: query.toString(),
|
||||
filter: {
|
||||
bool: {
|
||||
must: [{ match_all: {} }, ...buildBoolArray(context.and)],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
.then(res => {
|
||||
const columns = res.columns.map(({ name, type }) => {
|
||||
return { name: sanitizeName(name), type: normalizeType(type) };
|
||||
});
|
||||
const columnNames = map(columns, 'name');
|
||||
const rows = res.rows.map(row => zipObject(columnNames, row));
|
||||
return {
|
||||
type: 'datatable',
|
||||
columns,
|
||||
rows,
|
||||
};
|
||||
});
|
||||
return queryEsSQL(handlers.elasticsearchClient, {
|
||||
count: args.count,
|
||||
query,
|
||||
filter: context.and,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
|
|
@ -4,10 +4,7 @@
|
|||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { map, zipObject } from 'lodash';
|
||||
import { normalizeType } from '../../../../server/lib/normalize_type';
|
||||
import { buildBoolArray } from '../../../../server/lib/build_bool_array';
|
||||
import { sanitizeName } from '../../../../server/lib/sanitize_name';
|
||||
import { queryEsSQL } from '../../../../server/lib/query_es_sql';
|
||||
|
||||
export const essql = () => ({
|
||||
name: 'essql',
|
||||
|
@ -27,42 +24,6 @@ export const essql = () => ({
|
|||
default: 1000,
|
||||
},
|
||||
},
|
||||
fn(context, args, helpers) {
|
||||
return helpers
|
||||
.elasticsearchClient('transport.request', {
|
||||
path: '/_xpack/sql?format=json',
|
||||
method: 'POST',
|
||||
body: {
|
||||
fetch_size: args.count,
|
||||
query: args.query,
|
||||
filter: {
|
||||
bool: {
|
||||
must: [{ match_all: {} }, ...buildBoolArray(context.and)],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
.then(res => {
|
||||
const columns = res.columns.map(({ name, type }) => {
|
||||
return { name: sanitizeName(name), type: normalizeType(type) };
|
||||
});
|
||||
const columnNames = map(columns, 'name');
|
||||
const rows = res.rows.map(row => zipObject(columnNames, row));
|
||||
return {
|
||||
type: 'datatable',
|
||||
columns,
|
||||
rows,
|
||||
};
|
||||
})
|
||||
.catch(e => {
|
||||
if (e.message.indexOf('parsing_exception') > -1) {
|
||||
throw new Error(
|
||||
`Couldn't parse Elasticsearch SQL query. You may need to add double quotes to names containing special characters. Check your query and try again. Error: ${
|
||||
e.message
|
||||
}`
|
||||
);
|
||||
}
|
||||
throw new Error(`Unexpected error from Elasticsearch: ${e.message}`);
|
||||
});
|
||||
},
|
||||
fn: (context, args, handlers) =>
|
||||
queryEsSQL(handlers.elasticsearchClient, { ...args, filter: context.and }),
|
||||
});
|
||||
|
|
46
x-pack/plugins/canvas/server/lib/query_es_sql.js
Normal file
46
x-pack/plugins/canvas/server/lib/query_es_sql.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
import { map, zipObject } from 'lodash';
|
||||
import { buildBoolArray } from './build_bool_array';
|
||||
import { sanitizeName } from './sanitize_name';
|
||||
import { normalizeType } from './normalize_type';
|
||||
|
||||
export const queryEsSQL = (elasticsearchClient, { count, query, filter }) =>
|
||||
elasticsearchClient('transport.request', {
|
||||
path: '/_xpack/sql?format=json',
|
||||
method: 'POST',
|
||||
body: {
|
||||
fetch_size: count,
|
||||
query: query,
|
||||
filter: {
|
||||
bool: {
|
||||
must: [{ match_all: {} }, ...buildBoolArray(filter)],
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
.then(res => {
|
||||
const columns = res.columns.map(({ name, type }) => {
|
||||
return { name: sanitizeName(name), type: normalizeType(type) };
|
||||
});
|
||||
const columnNames = map(columns, 'name');
|
||||
const rows = res.rows.map(row => zipObject(columnNames, row));
|
||||
return {
|
||||
type: 'datatable',
|
||||
columns,
|
||||
rows,
|
||||
};
|
||||
})
|
||||
.catch(e => {
|
||||
if (e.message.indexOf('parsing_exception') > -1) {
|
||||
throw new Error(
|
||||
`Couldn't parse Elasticsearch SQL query. You may need to add double quotes to names containing special characters. Check your query and try again. Error: ${
|
||||
e.message
|
||||
}`
|
||||
);
|
||||
}
|
||||
throw new Error(`Unexpected error from Elasticsearch: ${e.message}`);
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue