Elasticsearch: fix handling of null values in query_builder (#30234)

This commit is contained in:
Giordano Ricci 2021-01-13 09:04:36 +00:00 committed by GitHub
parent a21c1b0841
commit d8b4ed3a0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 4 deletions

View File

@ -334,10 +334,11 @@ export class ElasticQueryBuilder {
metricAgg = { field: metric.field };
}
metricAgg = {
...metricAgg,
...(isMetricAggregationWithSettings(metric) && metric.settings),
};
if (isMetricAggregationWithSettings(metric)) {
Object.entries(metric.settings || {})
.filter(([_, v]) => v !== null)
.forEach(([k, v]) => (metricAgg[k] = v));
}
aggField[metric.type] = metricAgg;
nestedAggs.aggs[metric.id] = aggField;

View File

@ -24,6 +24,22 @@ describe('ElasticQueryBuilder', () => {
expect(query.aggs['1'].date_histogram.extended_bounds.min).toBe('$timeFrom');
});
it('should clean settings from null values', () => {
const query = builder.build({
refId: 'A',
// The following `missing: null as any` is because previous versions of the DS where
// storing null in the query model when inputting an empty string,
// which were then removed in the query builder.
// The new version doesn't store empty strings at all. This tests ensures backward compatinility.
metrics: [{ type: 'avg', id: '0', settings: { missing: null as any, script: '1' } }],
timeField: '@timestamp',
bucketAggs: [{ type: 'date_histogram', field: '@timestamp', id: '1' }],
});
expect(query.aggs['1'].aggs['0'].avg.missing).not.toBeDefined();
expect(query.aggs['1'].aggs['0'].avg.script).toBeDefined();
});
it('with multiple bucket aggs', () => {
const query = builder.build({
refId: 'A',