From 648fcc12b4806f8dbbc5805990250a6748cbbb3b Mon Sep 17 00:00:00 2001 From: friol Date: Sat, 24 Aug 2019 22:26:27 +0200 Subject: [PATCH 1/7] Possibility to filter when testing scripted fields --- .../components/scripting_help/test_script.js | 23 ++++++++++++++++++- .../field_editor/lib/validate_script.js | 6 ++++- 2 files changed, 27 insertions(+), 2 deletions(-) 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 04a457c118e55..9b20d93ed1908 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 @@ -29,12 +29,16 @@ import { EuiSpacer, EuiTitle, EuiCallOut, + EuiFieldText, } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; + export class TestScript extends Component { state = { isLoading: false, additionalFields: [], + searchQuery: '', } componentDidMount() { @@ -65,6 +69,7 @@ export class TestScript extends Component { lang, script, indexPatternTitle: indexPattern.title, + additionalQuery: this.state.searchQuery, additionalFields: this.state.additionalFields.map(option => { return option.value; }) @@ -181,6 +186,22 @@ export class TestScript extends Component { /> + + + { + this.setState({ + searchQuery: e.target.value, + }); + }} + /> + + + 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. + additional 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 f8940447abc68..3f01d42e5f6af 100644 --- a/src/legacy/ui/public/field_editor/lib/validate_script.js +++ b/src/legacy/ui/public/field_editor/lib/validate_script.js @@ -19,7 +19,7 @@ import { kfetch } from 'ui/kfetch'; -export const executeScript = async ({ name, lang, script, indexPatternTitle, additionalFields = [] }) => { +export const executeScript = async ({ name, lang, script, indexPatternTitle, additionalQuery = '', additionalFields = [] }) => { // Using _msearch because _search with index name in path dorks everything up const header = { index: indexPatternTitle, @@ -46,6 +46,10 @@ export const executeScript = async ({ name, lang, script, indexPatternTitle, add search._source = additionalFields; } + if (additionalQuery !== '') { + search.query = { query_string: { query: additionalQuery } }; + } + const body = `${JSON.stringify(header)}\n${JSON.stringify(search)}\n`; const esResp = await kfetch({ method: 'POST', pathname: '/elasticsearch/_msearch', body }); // unwrap _msearch response From be05f0b16cb018a1e0f15f51cfe749bbbcb6ddec Mon Sep 17 00:00:00 2001 From: friol Date: Sat, 24 Aug 2019 22:26:27 +0200 Subject: [PATCH 2/7] Possibility to filter when testing scripted fields --- .../components/scripting_help/test_script.js | 23 ++++++++++++++++++- .../field_editor/lib/validate_script.js | 6 ++++- 2 files changed, 27 insertions(+), 2 deletions(-) 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 04a457c118e55..9b20d93ed1908 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 @@ -29,12 +29,16 @@ import { EuiSpacer, EuiTitle, EuiCallOut, + EuiFieldText, } from '@elastic/eui'; +import { i18n } from '@kbn/i18n'; + export class TestScript extends Component { state = { isLoading: false, additionalFields: [], + searchQuery: '', } componentDidMount() { @@ -65,6 +69,7 @@ export class TestScript extends Component { lang, script, indexPatternTitle: indexPattern.title, + additionalQuery: this.state.searchQuery, additionalFields: this.state.additionalFields.map(option => { return option.value; }) @@ -181,6 +186,22 @@ export class TestScript extends Component { /> + + + { + this.setState({ + searchQuery: e.target.value, + }); + }} + /> + + + 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. + additional 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 f8940447abc68..3f01d42e5f6af 100644 --- a/src/legacy/ui/public/field_editor/lib/validate_script.js +++ b/src/legacy/ui/public/field_editor/lib/validate_script.js @@ -19,7 +19,7 @@ import { kfetch } from 'ui/kfetch'; -export const executeScript = async ({ name, lang, script, indexPatternTitle, additionalFields = [] }) => { +export const executeScript = async ({ name, lang, script, indexPatternTitle, additionalQuery = '', additionalFields = [] }) => { // Using _msearch because _search with index name in path dorks everything up const header = { index: indexPatternTitle, @@ -46,6 +46,10 @@ export const executeScript = async ({ name, lang, script, indexPatternTitle, add search._source = additionalFields; } + if (additionalQuery !== '') { + search.query = { query_string: { query: additionalQuery } }; + } + const body = `${JSON.stringify(header)}\n${JSON.stringify(search)}\n`; const esResp = await kfetch({ method: 'POST', pathname: '/elasticsearch/_msearch', body }); // unwrap _msearch response From 6f2305519b8dff5522bff92e99e7486e95c53027 Mon Sep 17 00:00:00 2001 From: friol Date: Wed, 28 Aug 2019 22:23:40 +0200 Subject: [PATCH 3/7] Now the i18n test should pass --- .../field_editor/components/scripting_help/test_script.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 9b20d93ed1908..1462e74953f40 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 @@ -190,7 +190,7 @@ export class TestScript extends Component { { From 8cd4cbd6dfe614be2ed1a2b7fb7e434233dbcebc Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 4 Dec 2019 07:42:00 -0700 Subject: [PATCH 4/7] use SearchBar instead of EuiFieldText input --- src/legacy/ui/public/field_editor/_index.scss | 1 + .../scripting_help/_test_script.scss | 5 ++ .../components/scripting_help/test_script.js | 59 ++++++++++--------- .../field_editor/lib/validate_script.js | 6 +- .../maps/public/layers/sources/es_source.js | 1 + 5 files changed, 42 insertions(+), 30 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/_test_script.scss b/src/legacy/ui/public/field_editor/components/scripting_help/_test_script.scss new file mode 100644 index 0000000000000..6d2db99adabfc --- /dev/null +++ b/src/legacy/ui/public/field_editor/components/scripting_help/_test_script.scss @@ -0,0 +1,5 @@ +.testScriptSearchBar { + .globalQueryBar { + padding: 8px 0px 0px 0px; + } +} 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 1462e74953f40..07bd78d6ec259 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 @@ -29,16 +29,19 @@ import { EuiSpacer, EuiTitle, EuiCallOut, - EuiFieldText, } from '@elastic/eui'; -import { i18n } from '@kbn/i18n'; +import { start as data } from '../../../../../core_plugins/data/public/legacy'; +const { SearchBar } = data.ui; +import { npStart } from 'ui/new_platform'; +const { uiSettings } = npStart.core; + +import { esQuery } from '../../../../../../plugins/data/public'; export class TestScript extends Component { state = { isLoading: false, additionalFields: [], - searchQuery: '', } componentDidMount() { @@ -47,7 +50,7 @@ export class TestScript extends Component { } } - previewScript = async () => { + previewScript = async (searchContext) => { const { indexPattern, lang, @@ -64,12 +67,18 @@ 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, - additionalQuery: this.state.searchQuery, + query, additionalFields: this.state.additionalFields.map(option => { return option.value; }) @@ -186,30 +195,26 @@ export class TestScript extends Component { /> - - - { - this.setState({ - searchQuery: e.target.value, - }); - }} +
+ + Run script + + } /> +
-
- - - Run script - ); } 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 3f01d42e5f6af..22e896e9b961d 100644 --- a/src/legacy/ui/public/field_editor/lib/validate_script.js +++ b/src/legacy/ui/public/field_editor/lib/validate_script.js @@ -19,7 +19,7 @@ import { kfetch } from 'ui/kfetch'; -export const executeScript = async ({ name, lang, script, indexPatternTitle, additionalQuery = '', additionalFields = [] }) => { +export const executeScript = async ({ name, lang, script, indexPatternTitle, query, additionalFields = [] }) => { // Using _msearch because _search with index name in path dorks everything up const header = { index: indexPatternTitle, @@ -46,8 +46,8 @@ export const executeScript = async ({ name, lang, script, indexPatternTitle, add search._source = additionalFields; } - if (additionalQuery !== '') { - search.query = { query_string: { query: additionalQuery } }; + if (query) { + search.query = query; } const body = `${JSON.stringify(header)}\n${JSON.stringify(search)}\n`; diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js index c2f4f7e755288..1462e96e3dc68 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js @@ -120,6 +120,7 @@ export class AbstractESSource extends AbstractVectorSource { searchSource.setField('query', searchFilters.query); } + console.log(searchFilters.sourceQuery); if (searchFilters.sourceQuery) { const layerSearchSource = new SearchSource(); layerSearchSource.setField('index', indexPattern); From 1784b3101f90fb975e2916c2f63165d06ae6df1c Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Wed, 4 Dec 2019 07:48:51 -0700 Subject: [PATCH 5/7] clean up --- x-pack/legacy/plugins/maps/public/layers/sources/es_source.js | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js b/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js index 1462e96e3dc68..c2f4f7e755288 100644 --- a/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js +++ b/x-pack/legacy/plugins/maps/public/layers/sources/es_source.js @@ -120,7 +120,6 @@ export class AbstractESSource extends AbstractVectorSource { searchSource.setField('query', searchFilters.query); } - console.log(searchFilters.sourceQuery); if (searchFilters.sourceQuery) { const layerSearchSource = new SearchSource(); layerSearchSource.setField('index', indexPattern); From ae1c08008d491f5a133cf81b2b8eaeb5f4bdb73c Mon Sep 17 00:00:00 2001 From: miukimiu Date: Wed, 11 Dec 2019 18:17:22 +0000 Subject: [PATCH 6/7] test script design improvement --- .../components/scripting_help/_test_script.scss | 6 +++--- .../field_editor/components/scripting_help/test_script.js | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) 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 index 6d2db99adabfc..34e8a60d07074 100644 --- 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 @@ -1,5 +1,5 @@ -.testScriptSearchBar { - .globalQueryBar { - padding: 8px 0px 0px 0px; +.testScript__searchBar { + .globalQueryBar { + padding: $euiSize 0 0; } } 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 07bd78d6ec259..f978460ddbf1c 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 @@ -185,6 +185,7 @@ export class TestScript extends Component { -
+
Date: Thu, 26 Dec 2019 20:10:55 +0100 Subject: [PATCH 7/7] Fixed SearchBar reference and updated the help_flyout tests --- .../scripting_help/__snapshots__/help_flyout.test.js.snap | 1 - .../components/scripting_help/help_flyout.test.js | 6 ++++++ .../field_editor/components/scripting_help/test_script.js | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) 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/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 ab36baab95c1b..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,9 +31,9 @@ import { EuiCallOut, } from '@elastic/eui'; -import { start as data } from '../../../../../core_plugins/data/public/legacy'; -const { SearchBar } = data.ui; import { npStart } from 'ui/new_platform'; +const { SearchBar } = npStart.plugins.data.ui; + const { uiSettings } = npStart.core; import { esQuery } from '../../../../../../plugins/data/public';