Skip to content

Commit

Permalink
Backport PR elastic#8642
Browse files Browse the repository at this point in the history
---------

**Commit 1:**
Fix our request to ES for filtering on scripted fields

Need to use params.value instead of value.

Fixes elastic#8404

Add params prefix in another spot for painless scripted fields

Fix date histogram with scripted fields

Remove format: epoch_millis so the script compiles.  I am not 100%
confident of the side affect from this (it’s used for non-scripted
fields, but I’m not sure where I would put it for scripted fields, or
if it’s needed).  At any rate, it appears that formatting settings for
scripted fields is still being honored, even after removing it from
params.

* Original sha: 7cdb74d
* Authored by Stacey Gammon <[email protected]> on 2016-10-12T14:54:23Z
  • Loading branch information
elastic-jasper committed Oct 13, 2016
1 parent 04e80c1 commit 4b54dc6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 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 @@ -114,13 +114,13 @@ describe('Filter Manager', function () {
checkAddFilters(0, null, 3);
expect(appState.filters).to.have.length(2);

let scriptedField = {name: 'scriptedField', scripted: true, script: 1};
let scriptedField = {name: 'scriptedField', scripted: true, script: 1, lang: 'painless'};
filterManager.add(scriptedField, 1, '+', 'myIndex');
checkAddFilters(1, [{
meta: {index: 'myIndex', negate: false, field: 'scriptedField'},
script: {
script: {
inline: '(' + scriptedField.script + ') == value',
inline: '(' + scriptedField.script + ') == params.value',
lang: scriptedField.lang,
params: {value: 1}
}
Expand Down
5 changes: 4 additions & 1 deletion src/ui/public/filter_manager/filter_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ export default function (Private) {
break;
default:
if (field.scripted) {
// painless expects params.value while groovy and expression languages expect value.
const valueClause = field.lang === 'painless' ? 'params.value' : 'value';
filter = {
meta: { negate: negate, index: index, field: fieldName },
script: {
script: {
inline: '(' + field.script + ') == value',
inline: '(' + field.script + ') == ' + valueClause,
lang: field.lang,
params: {
value: value
Expand All @@ -82,3 +84,4 @@ export default function (Private) {

return filterManager;
};

4 changes: 3 additions & 1 deletion src/ui/public/filter_manager/lib/phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ export default function buildPhraseFilter(field, value, indexPattern) {
let filter = { meta: { index: indexPattern.id} };

if (field.scripted) {
// painless expects params.value while groovy and expression languages expect value.
const valueClause = field.lang === 'painless' ? 'params.value' : 'value';
_.set(filter, 'script.script', {
inline: '(' + field.script + ') == value',
inline: '(' + field.script + ') == ' + valueClause,
lang: field.lang,
params: {
value: value
Expand Down
11 changes: 7 additions & 4 deletions src/ui/public/filter_manager/lib/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,18 @@ export default function buildRangeFilter(field, params, indexPattern, formattedV
lt: '<',
};

const script = _.map(params, function (val, key) {
return '(' + field.script + ')' + operators[key] + key;
const knownParams = _.pick(params, (val, key) => { return key in operators; });
const script = _.map(knownParams, function (val, key) {
// painless expects params.[key] while groovy and expression languages expect [key] only.
const valuePrefix = field.lang === 'painless' ? 'params.' : '';
return '(' + field.script + ')' + operators[key] + valuePrefix + key;
}).join(' && ');

const value = _.map(params, function (val, key) {
const value = _.map(knownParams, function (val, key) {
return operators[key] + field.format.convert(val);
}).join(' ');

_.set(filter, 'script.script', { inline: script, params: params, lang: field.lang });
_.set(filter, 'script.script', { inline: script, params: knownParams, lang: field.lang });
filter.script.script.params.value = value;
filter.meta.field = field.name;
} else {
Expand Down

0 comments on commit 4b54dc6

Please sign in to comment.