diff --git a/src/ui/public/filter_manager/__tests__/filter_manager.js b/src/ui/public/filter_manager/__tests__/filter_manager.js index 9b13b1575350e..33725381f36de 100644 --- a/src/ui/public/filter_manager/__tests__/filter_manager.js +++ b/src/ui/public/filter_manager/__tests__/filter_manager.js @@ -5,6 +5,7 @@ import expect from 'expect.js'; import ngMock from 'ng_mock'; import FilterManagerProvider from 'ui/filter_manager'; import FilterBarQueryFilterProvider from 'ui/filter_bar/query_filter'; +import { buildInlineScriptForPhraseFilter } from '../lib/phrase'; let $rootScope; let queryFilter; let filterManager; @@ -120,8 +121,7 @@ describe('Filter Manager', function () { meta: {index: 'myIndex', negate: false, field: 'scriptedField'}, script: { script: { - inline: `boolean compare(Supplier s, def v) {return s.get() == v;} - compare(() -> { ${scriptedField.script} }, params.value);`, + inline: buildInlineScriptForPhraseFilter(scriptedField), lang: scriptedField.lang, params: {value: 1} } diff --git a/src/ui/public/filter_manager/filter_manager.js b/src/ui/public/filter_manager/filter_manager.js index 51b0ed0231e50..3bf8a22051264 100644 --- a/src/ui/public/filter_manager/filter_manager.js +++ b/src/ui/public/filter_manager/filter_manager.js @@ -1,5 +1,7 @@ import _ from 'lodash'; import FilterBarQueryFilterProvider from 'ui/filter_bar/query_filter'; +import { buildInlineScriptForPhraseFilter } from './lib/phrase'; + // Adds a filter to a passed state export default function (Private) { let queryFilter = Private(FilterBarQueryFilterProvider); @@ -54,17 +56,11 @@ export default function (Private) { break; default: if (field.scripted) { - // We must wrap painless scripts in a lambda in case they're more than a simple expression - let script = `(${field.script}) == value`; - if (field.lang === 'painless') { - script = `boolean compare(Supplier s, def v) {return s.get() == v;} - compare(() -> { ${field.script} }, params.value);`; - } filter = { meta: { negate: negate, index: index, field: fieldName }, script: { script: { - inline: script, + inline: buildInlineScriptForPhraseFilter(field), lang: field.lang, params: { value: value diff --git a/src/ui/public/filter_manager/lib/phrase.js b/src/ui/public/filter_manager/lib/phrase.js index 2a98a9bec0fba..ea1343159045f 100644 --- a/src/ui/public/filter_manager/lib/phrase.js +++ b/src/ui/public/filter_manager/lib/phrase.js @@ -14,12 +14,7 @@ export default function buildPhraseFilter(field, value, indexPattern) { convertedValue = value === 'true' ? true : false; } - // We must wrap painless scripts in a lambda in case they're more than a simple expression - let script = `(${field.script}) == value`; - if (field.lang === 'painless') { - script = `boolean compare(Supplier s, def v) {return s.get() == v;} - compare(() -> { ${field.script} }, params.value);`; - } + const script = buildInlineScriptForPhraseFilter(field); _.set(filter, 'script.script', { inline: script, @@ -38,3 +33,23 @@ export default function buildPhraseFilter(field, value, indexPattern) { } return filter; }; + + +/** + * Takes a scripted field and returns an inline script appropriate for use in a script query. + * Handles lucene expression and Painless scripts. Other langs aren't guaranteed to generate valid + * scripts. + * + * @param {object} scriptedField A Field object representing a scripted field + * @returns {string} The inline script string + */ +export function buildInlineScriptForPhraseFilter(scriptedField) { + // We must wrap painless scripts in a lambda in case they're more than a simple expression + if (scriptedField.lang === 'painless') { + return `boolean compare(Supplier s, def v) {return s.get() == v;} + compare(() -> { ${scriptedField.script} }, params.value);`; + } + else { + return `(${scriptedField.script}) == value`; + } +}