Skip to content

Commit

Permalink
Update script syntax for scripted fields
Browse files Browse the repository at this point in the history
Elasticsearch has removed the abbreviated script syntax that we were
using in Kibana. This change updates all of Kibana's queries and
filters to use the newer object syntax wherever scripted fields are
valid.

This commit also includes a change to pass the lang param when sorting
by scripted field which fixes an error that was being thrown.

Fixes: elastic#7788
Fixes: elastic#5676
Related: elastic/elasticsearch#19387
  • Loading branch information
Bargs committed Jul 21, 2016
1 parent 0724220 commit ebb9cd2
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 36 deletions.
2 changes: 1 addition & 1 deletion src/ui/public/agg_types/param_types/field.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default function FieldAggParamFactory(Private) {

if (field.scripted) {
output.params.script = {
script: field.script,
inline: field.script,
lang: field.lang,
};
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ describe('SearchSource#normalizeSortRequest', function () {
sortState[fieldName] = direction;
normalizedSort = {
_script: {
script: indexField.script,
script: {
inline: indexField.script,
lang: indexField.lang
},
type: indexField.type,
order: direction
}
Expand Down
5 changes: 4 additions & 1 deletion src/ui/public/courier/data_source/_normalize_sort_request.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ export default function normalizeSortRequest(config) {

sortField = '_script';
sortValue = {
script: indexField.script,
script: {
inline: indexField.script,
lang: indexField.lang
},
type: indexField.type,
order: direction
};
Expand Down
12 changes: 7 additions & 5 deletions src/ui/public/filter_bar/lib/__tests__/map_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Filter Bar Directive', function () {
it('should return the key and value for matching filters', function (done) {
let filter = {
meta: { index: 'logstash-*', field: 'script number' },
script: { script: 'doc["script number"].value * 5', params: { value: 35}}
script: {script: { inline: 'doc["script number"].value * 5', params: { value: 35}}}
};
mapScript(filter).then(function (result) {
expect(result).to.have.property('key', 'script number');
Expand All @@ -51,10 +51,12 @@ describe('Filter Bar Directive', function () {
field: 'script number'
},
script: {
params: {
gte: 1000,
lt: 2000,
value: '>=1,000.00 <2,000.00'
script: {
params: {
gte: 1000,
lt: 2000,
value: '>=1,000.00 <2,000.00'
}
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/filter_bar/lib/map_script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ define(function () {
if (filter.meta.formattedValue) {
value = filter.meta.formattedValue;
} else {
value = filter.script.params.value;
value = filter.script.script.params.value;
value = field.format.convert(value);
}

Expand Down
8 changes: 5 additions & 3 deletions src/ui/public/filter_manager/__tests__/filter_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ describe('Filter Manager', function () {
checkAddFilters(1, [{
meta: {index: 'myIndex', negate: false, field: 'scriptedField'},
script: {
script: '(' + scriptedField.script + ') == value',
lang: scriptedField.lang,
params: {value: 1}
script: {
inline: '(' + scriptedField.script + ') == value',
lang: scriptedField.lang,
params: {value: 1}
}
}
}], 4);
expect(appState.filters).to.have.length(3);
Expand Down
12 changes: 7 additions & 5 deletions src/ui/public/filter_manager/filter_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default function (Private) {
}

if (filter.script) {
return filter.meta.field === fieldName && filter.script.params.value === value;
return filter.meta.field === fieldName && filter.script.script.params.value === value;
}
});

Expand Down Expand Up @@ -57,10 +57,12 @@ export default function (Private) {
filter = {
meta: { negate: negate, index: index, field: fieldName },
script: {
script: '(' + field.script + ') == value',
lang: field.lang,
params: {
value: value
script: {
inline: '(' + field.script + ') == value',
lang: field.lang,
params: {
value: value
}
}
}
};
Expand Down
6 changes: 3 additions & 3 deletions src/ui/public/filter_manager/lib/__tests__/phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ describe('Filter Manager', function () {

it('should return a script filter when passed a scripted field', function () {
expected.meta.field = 'script number';
expected.script = {
script: '(' + indexPattern.fields.byName['script number'].script + ') == value',
_.set(expected, 'script.script', {
inline: '(' + indexPattern.fields.byName['script number'].script + ') == value',
lang: 'expression',
params: {
value: 5,
}
};
});
expect(fn(indexPattern.fields.byName['script number'], 5, indexPattern)).to.eql(expected);
});
});
Expand Down
20 changes: 10 additions & 10 deletions src/ui/public/filter_manager/lib/__tests__/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,16 @@ describe('Filter Manager', function () {

it('should return a script filter when passed a scripted field', function () {
expected.meta.field = 'script number';
expected.script = {
_.set(expected, 'script.script', {
lang: 'expression',
script: '(' + indexPattern.fields.byName['script number'].script + ')>=gte && (' +
indexPattern.fields.byName['script number'].script + ')<=lte',
inline: '(' + indexPattern.fields.byName['script number'].script + ')>=gte && (' +
indexPattern.fields.byName['script number'].script + ')<=lte',
params: {
value: '>=1 <=3',
gte: 1,
lte: 3
}
};
});
expect(fn(indexPattern.fields.byName['script number'], {gte: 1, lte: 3}, indexPattern)).to.eql(expected);
});

Expand All @@ -58,9 +58,9 @@ describe('Filter Manager', function () {
params[key] = 5;
let filter = fn(indexPattern.fields.byName['script number'], params, indexPattern);

expect(filter.script.script).to.be('(' + indexPattern.fields.byName['script number'].script + ')' + operator + key);
expect(filter.script.params[key]).to.be(5);
expect(filter.script.params.value).to.be(operator + 5);
expect(filter.script.script.inline).to.be('(' + indexPattern.fields.byName['script number'].script + ')' + operator + key);
expect(filter.script.script.params[key]).to.be(5);
expect(filter.script.script.params.value).to.be(operator + 5);

});
});
Expand All @@ -77,16 +77,16 @@ describe('Filter Manager', function () {
});

it('contain a param for the finite side', function () {
expect(filter.script.params).to.have.property('gte', 0);
expect(filter.script.script.params).to.have.property('gte', 0);
});

it('does not contain a param for the infinite side', function () {
expect(filter.script.params).not.to.have.property('lt');
expect(filter.script.script.params).not.to.have.property('lt');
});

it('does not contain a script condition for the infinite side', function () {
const script = indexPattern.fields.byName['script number'].script;
expect(filter.script.script).to.equal(`(${script})>=gte`);
expect(filter.script.script.inline).to.equal(`(${script})>=gte`);
});
});
});
Expand Down
6 changes: 3 additions & 3 deletions src/ui/public/filter_manager/lib/phrase.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ export default function buildPhraseFilter(field, value, indexPattern) {
let filter = { meta: { index: indexPattern.id} };

if (field.scripted) {
filter.script = {
script: '(' + field.script + ') == value',
_.set(filter, 'script.script', {
inline: '(' + field.script + ') == value',
lang: field.lang,
params: {
value: value
}
};
});
filter.meta.field = field.name;
} else {
filter.query = { match: {} };
Expand Down
4 changes: 2 additions & 2 deletions src/ui/public/filter_manager/lib/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export default function buildRangeFilter(field, params, indexPattern, formattedV
return operators[key] + field.format.convert(val);
}).join(' ');

filter.script = { script: script, params: params, lang: field.lang };
filter.script.params.value = value;
_.set(filter, 'script.script', { inline: script, params: params, lang: field.lang });
filter.script.script.params.value = value;
filter.meta.field = field.name;
} else {
filter.range = {};
Expand Down
2 changes: 1 addition & 1 deletion src/ui/public/index_patterns/_get_computed_fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default function () {
_.each(self.getScriptedFields(), function (field) {
scriptFields[field.name] = {
script: {
script: field.script,
inline: field.script,
lang: field.lang
}
};
Expand Down

0 comments on commit ebb9cd2

Please sign in to comment.