Skip to content

Commit 92d7430

Browse files
committed
[Rollup] Histo group config should support scaled_floats (#32048)
Metric config already whitelist scaled_floats, but it wasn't added to the histo group config. This centralizes the mapping types map so that both metrics and histo (and any future configs) use the same map. Fixes #32035
1 parent 6509c59 commit 92d7430

File tree

4 files changed

+18
-20
lines changed

4 files changed

+18
-20
lines changed

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/RollupField.java

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package org.elasticsearch.xpack.core.rollup;
77

88
import org.elasticsearch.common.ParseField;
9+
import org.elasticsearch.index.mapper.NumberFieldMapper;
910
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
1011
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
1112
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
@@ -15,6 +16,8 @@
1516

1617
import java.util.Arrays;
1718
import java.util.List;
19+
import java.util.stream.Collectors;
20+
import java.util.stream.Stream;
1821

1922
public class RollupField {
2023
// Fields that are used both in core Rollup actions and Rollup plugin
@@ -34,6 +37,16 @@ public class RollupField {
3437
public static final List<String> SUPPORTED_METRICS = Arrays.asList(MaxAggregationBuilder.NAME, MinAggregationBuilder.NAME,
3538
SumAggregationBuilder.NAME, AvgAggregationBuilder.NAME, ValueCountAggregationBuilder.NAME);
3639

40+
// these mapper types are used by the configs (metric, histo, etc) to validate field mappings
41+
public static final List<String> NUMERIC_FIELD_MAPPER_TYPES;
42+
static {
43+
List<String> types = Stream.of(NumberFieldMapper.NumberType.values())
44+
.map(NumberFieldMapper.NumberType::typeName)
45+
.collect(Collectors.toList());
46+
types.add("scaled_float"); // have to add manually since scaled_float is in a module
47+
NUMERIC_FIELD_MAPPER_TYPES = types;
48+
}
49+
3750
/**
3851
* Format to the appropriate Rollup field name convention
3952
*

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/HistoGroupConfig.java

+1-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.common.xcontent.ObjectParser;
1616
import org.elasticsearch.common.xcontent.ToXContentFragment;
1717
import org.elasticsearch.common.xcontent.XContentBuilder;
18-
import org.elasticsearch.index.mapper.NumberFieldMapper;
1918
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
2019
import org.elasticsearch.search.aggregations.bucket.composite.HistogramValuesSourceBuilder;
2120
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder;
@@ -30,7 +29,6 @@
3029
import java.util.Objects;
3130
import java.util.Set;
3231
import java.util.stream.Collectors;
33-
import java.util.stream.Stream;
3432

3533
/**
3634
* The configuration object for the histograms in the rollup config
@@ -51,10 +49,6 @@ public class HistoGroupConfig implements Writeable, ToXContentFragment {
5149

5250
private static final ParseField INTERVAL = new ParseField("interval");
5351
private static final ParseField FIELDS = new ParseField("fields");
54-
private static final List<String> MAPPER_TYPES = Stream.of(NumberFieldMapper.NumberType.values())
55-
.map(NumberFieldMapper.NumberType::typeName)
56-
.collect(Collectors.toList());
57-
5852

5953
private final long interval;
6054
private final String[] fields;
@@ -126,7 +120,7 @@ public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCa
126120
Map<String, FieldCapabilities> fieldCaps = fieldCapsResponse.get(field);
127121
if (fieldCaps != null && fieldCaps.isEmpty() == false) {
128122
fieldCaps.forEach((key, value) -> {
129-
if (MAPPER_TYPES.contains(key)) {
123+
if (RollupField.NUMERIC_FIELD_MAPPER_TYPES.contains(key)) {
130124
if (value.isAggregatable() == false) {
131125
validationException.addValidationError("The field [" + field + "] must be aggregatable across all indices, " +
132126
"but is not.");

x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/rollup/job/MetricConfig.java

+1-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.elasticsearch.common.xcontent.ObjectParser;
1616
import org.elasticsearch.common.xcontent.ToXContentFragment;
1717
import org.elasticsearch.common.xcontent.XContentBuilder;
18-
import org.elasticsearch.index.mapper.NumberFieldMapper;
1918
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
2019
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
2120
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
@@ -32,7 +31,6 @@
3231
import java.util.Map;
3332
import java.util.Objects;
3433
import java.util.stream.Collectors;
35-
import java.util.stream.Stream;
3634

3735
/**
3836
* The configuration object for the metrics portion of a rollup job config
@@ -66,15 +64,6 @@ public class MetricConfig implements Writeable, ToXContentFragment {
6664
private static final ParseField AVG = new ParseField("avg");
6765
private static final ParseField VALUE_COUNT = new ParseField("value_count");
6866

69-
private static final List<String> MAPPER_TYPES;
70-
static {
71-
List<String> types = Stream.of(NumberFieldMapper.NumberType.values())
72-
.map(NumberFieldMapper.NumberType::typeName)
73-
.collect(Collectors.toList());
74-
types.add("scaled_float"); // have to add manually since scaled_float is in a module
75-
MAPPER_TYPES = types;
76-
}
77-
7867
public static final ObjectParser<MetricConfig.Builder, Void> PARSER = new ObjectParser<>(NAME, MetricConfig.Builder::new);
7968

8069
static {
@@ -153,7 +142,7 @@ public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCa
153142
Map<String, FieldCapabilities> fieldCaps = fieldCapsResponse.get(field);
154143
if (fieldCaps != null && fieldCaps.isEmpty() == false) {
155144
fieldCaps.forEach((key, value) -> {
156-
if (MAPPER_TYPES.contains(key)) {
145+
if (RollupField.NUMERIC_FIELD_MAPPER_TYPES.contains(key)) {
157146
if (value.isAggregatable() == false) {
158147
validationException.addValidationError("The field [" + field + "] must be aggregatable across all indices, " +
159148
"but is not.");

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/rollup/job/HistoGroupConfigSerializingTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import org.elasticsearch.common.xcontent.XContentParser;
1212
import org.elasticsearch.test.AbstractSerializingTestCase;
1313
import org.elasticsearch.xpack.core.rollup.ConfigTestHelpers;
14+
import org.elasticsearch.xpack.core.rollup.RollupField;
1415

1516
import java.io.IOException;
1617
import java.util.Collections;
@@ -111,7 +112,8 @@ public void testValidateMatchingField() throws IOException {
111112
// Have to mock fieldcaps because the ctor's aren't public...
112113
FieldCapabilities fieldCaps = mock(FieldCapabilities.class);
113114
when(fieldCaps.isAggregatable()).thenReturn(true);
114-
responseMap.put("my_field", Collections.singletonMap("long", fieldCaps));
115+
String mappingType = randomFrom(RollupField.NUMERIC_FIELD_MAPPER_TYPES);
116+
responseMap.put("my_field", Collections.singletonMap(mappingType, fieldCaps));
115117

116118
HistoGroupConfig config = new HistoGroupConfig.Builder()
117119
.setFields(Collections.singletonList("my_field"))

0 commit comments

Comments
 (0)