From 3ed5264cc307964ced5f90402264232724956602 Mon Sep 17 00:00:00 2001 From: friol Date: Fri, 27 Dec 2019 04:09:40 +0100 Subject: [PATCH] Possibility to filter when testing scripted fields (#35379) (#44220) * Possibility to filter when testing scripted fields * Possibility to filter when testing scripted fields * Now the i18n test should pass * use SearchBar instead of EuiFieldText input * clean up * test script design improvement * Fixed SearchBar reference and updated the help_flyout tests Co-authored-by: Elastic Machine Co-authored-by: Nathan Reese Co-authored-by: Elizabet Oliveira --- src/legacy/ui/public/field_editor/_index.scss | 1 + .../__snapshots__/help_flyout.test.js.snap | 1 - .../scripting_help/_test_script.scss | 5 ++ .../scripting_help/help_flyout.test.js | 6 +++ .../components/scripting_help/test_script.js | 54 +++++++++++++++---- .../field_editor/lib/validate_script.js | 5 ++ 6 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 src/legacy/ui/public/field_editor/components/scripting_help/_test_script.scss diff --git a/src/legacy/ui/public/field_editor/_index.scss b/src/legacy/ui/public/field_editor/_index.scss index 9c94861eb74aa..39f69c013d428 100644 --- a/src/legacy/ui/public/field_editor/_index.scss +++ b/src/legacy/ui/public/field_editor/_index.scss @@ -1 +1,2 @@ @import './components/field_format_editor/samples/index'; +@import './components/scripting_help/test_script'; diff --git a/src/legacy/ui/public/field_editor/components/scripting_help/__snapshots__/help_flyout.test.js.snap b/src/legacy/ui/public/field_editor/components/scripting_help/__snapshots__/help_flyout.test.js.snap index b2ee13e865a9a..ca252b6d0147b 100644 --- a/src/legacy/ui/public/field_editor/components/scripting_help/__snapshots__/help_flyout.test.js.snap +++ b/src/legacy/ui/public/field_editor/components/scripting_help/__snapshots__/help_flyout.test.js.snap @@ -34,7 +34,6 @@ exports[`ScriptingHelpFlyout should render normally 1`] = ` executeScript={[Function]} indexPattern={Object {}} lang="painless" - name="myScriptedField" />, "data-test-subj": "testTab", "id": "test", diff --git a/src/legacy/ui/public/field_editor/components/scripting_help/_test_script.scss b/src/legacy/ui/public/field_editor/components/scripting_help/_test_script.scss new file mode 100644 index 0000000000000..34e8a60d07074 --- /dev/null +++ b/src/legacy/ui/public/field_editor/components/scripting_help/_test_script.scss @@ -0,0 +1,5 @@ +.testScript__searchBar { + .globalQueryBar { + padding: $euiSize 0 0; + } +} diff --git a/src/legacy/ui/public/field_editor/components/scripting_help/help_flyout.test.js b/src/legacy/ui/public/field_editor/components/scripting_help/help_flyout.test.js index 69e6f9430b4e1..2fac8c7641ddb 100644 --- a/src/legacy/ui/public/field_editor/components/scripting_help/help_flyout.test.js +++ b/src/legacy/ui/public/field_editor/components/scripting_help/help_flyout.test.js @@ -26,6 +26,12 @@ jest.mock('ui/documentation_links', () => ({ getDocLink: doc => `(docLink for ${doc})`, })); +jest.mock('./test_script', () => ({ + TestScript: () => { + return `
mockTestScript
`; + }, +})); + const indexPatternMock = {}; describe('ScriptingHelpFlyout', () => { diff --git a/src/legacy/ui/public/field_editor/components/scripting_help/test_script.js b/src/legacy/ui/public/field_editor/components/scripting_help/test_script.js index 08d22af66e451..942f39fc98dec 100644 --- a/src/legacy/ui/public/field_editor/components/scripting_help/test_script.js +++ b/src/legacy/ui/public/field_editor/components/scripting_help/test_script.js @@ -31,6 +31,13 @@ import { EuiCallOut, } from '@elastic/eui'; +import { npStart } from 'ui/new_platform'; +const { SearchBar } = npStart.plugins.data.ui; + +const { uiSettings } = npStart.core; + +import { esQuery } from '../../../../../../plugins/data/public'; + export class TestScript extends Component { state = { isLoading: false, @@ -43,7 +50,7 @@ export class TestScript extends Component { } } - previewScript = async () => { + previewScript = async searchContext => { const { indexPattern, lang, name, script, executeScript } = this.props; if (!script || script.length === 0) { @@ -54,11 +61,23 @@ export class TestScript extends Component { isLoading: true, }); + let query; + if (searchContext) { + const esQueryConfigs = esQuery.getEsQueryConfig(uiSettings); + query = esQuery.buildEsQuery( + this.props.indexPattern, + searchContext.query, + null, + esQueryConfigs + ); + } + const scriptResponse = await executeScript({ name, lang, script, indexPatternTitle: indexPattern.title, + query, additionalFields: this.state.additionalFields.map(option => { return option.value; }), @@ -161,24 +180,36 @@ export class TestScript extends Component { return ( - + - - Run script - +
+ + Run script + + } + /> +
); } @@ -191,7 +222,8 @@ export class TestScript extends Component {

Preview results

Run your script to preview the first 10 results. You can also select some additional - fields to include in your results to gain more context. + fields to include in your results to gain more context or add a query to filter on + specific documents.

diff --git a/src/legacy/ui/public/field_editor/lib/validate_script.js b/src/legacy/ui/public/field_editor/lib/validate_script.js index ee51fe8858bc0..47e2091565c30 100644 --- a/src/legacy/ui/public/field_editor/lib/validate_script.js +++ b/src/legacy/ui/public/field_editor/lib/validate_script.js @@ -24,6 +24,7 @@ export const executeScript = async ({ lang, script, indexPatternTitle, + query, additionalFields = [], }) => { // Using _msearch because _search with index name in path dorks everything up @@ -52,6 +53,10 @@ export const executeScript = async ({ search._source = additionalFields; } + if (query) { + search.query = query; + } + const body = `${JSON.stringify(header)}\n${JSON.stringify(search)}\n`; const esResp = await kfetch({ method: 'POST', pathname: '/elasticsearch/_msearch', body }); // unwrap _msearch response