Commit d8b4ed3a by Giordano Ricci Committed by GitHub

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

parent a21c1b08
......@@ -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;
......
......@@ -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',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment