Skip to content

Commit

Permalink
DRY it up
Browse files Browse the repository at this point in the history
  • Loading branch information
Bargs committed Nov 22, 2016
1 parent b2a86bb commit 927de50
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/ui/public/filter_manager/__tests__/filter_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}
}
Expand Down
10 changes: 3 additions & 7 deletions src/ui/public/filter_manager/filter_manager.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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
Expand Down
27 changes: 21 additions & 6 deletions src/ui/public/filter_manager/lib/phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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`;
}
}

0 comments on commit 927de50

Please sign in to comment.