Skip to content

Commit

Permalink
Possibility to filter when testing scripted fields
Browse files Browse the repository at this point in the history
  • Loading branch information
friol committed Aug 28, 2019
1 parent 9bc0ec0 commit be05f0b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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;
})
Expand Down Expand Up @@ -181,6 +186,22 @@ export class TestScript extends Component {
/>
</EuiFormRow>

<EuiFormRow label="Search query">

<EuiFieldText
placeholder={i18n.translate('common.ui.savedObjects.finder.searchPlaceholder', {
defaultMessage: 'Search query…',
})}
value={this.state.searchQuery}
onChange={e => {
this.setState({
searchQuery: e.target.value,
});
}}
/>

</EuiFormRow>

<EuiButton
onClick={this.previewScript}
disabled={this.props.script ? false : true}
Expand All @@ -201,7 +222,7 @@ export class TestScript extends Component {
<h3>Preview results</h3>
<p>
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.
</p>
</EuiText>
<EuiSpacer />
Expand Down
6 changes: 5 additions & 1 deletion src/legacy/ui/public/field_editor/lib/validate_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit be05f0b

Please sign in to comment.