Skip to content

Commit ed04fac

Browse files
committed
[Rollup] Move toBuilders() methods out of rollup config objects
1 parent 7e5efad commit ed04fac

File tree

8 files changed

+236
-197
lines changed

8 files changed

+236
-197
lines changed

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,11 @@
2020
import org.elasticsearch.common.xcontent.ToXContentObject;
2121
import org.elasticsearch.common.xcontent.XContentBuilder;
2222
import org.elasticsearch.common.xcontent.XContentParser;
23-
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
24-
import org.elasticsearch.search.aggregations.bucket.composite.DateHistogramValuesSourceBuilder;
2523
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder;
2624
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval;
27-
import org.elasticsearch.xpack.core.rollup.RollupField;
2825
import org.joda.time.DateTimeZone;
2926

3027
import java.io.IOException;
31-
import java.util.Collections;
32-
import java.util.List;
3328
import java.util.Map;
3429
import java.util.Objects;
3530

@@ -182,19 +177,6 @@ public Rounding createRounding() {
182177
return createRounding(interval.toString(), timeZone);
183178
}
184179

185-
/**
186-
* This returns a set of aggregation builders which represent the configured
187-
* set of date histograms. Used by the rollup indexer to iterate over historical data
188-
*/
189-
public List<CompositeValuesSourceBuilder<?>> toBuilders() {
190-
DateHistogramValuesSourceBuilder vsBuilder =
191-
new DateHistogramValuesSourceBuilder(RollupField.formatIndexerAggName(field, DateHistogramAggregationBuilder.NAME));
192-
vsBuilder.dateHistogramInterval(interval);
193-
vsBuilder.field(field);
194-
vsBuilder.timeZone(toDateTimeZone(timeZone));
195-
return Collections.singletonList(vsBuilder);
196-
}
197-
198180
public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
199181
ActionRequestValidationException validationException) {
200182

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,13 @@
1616
import org.elasticsearch.common.xcontent.ToXContentObject;
1717
import org.elasticsearch.common.xcontent.XContentBuilder;
1818
import org.elasticsearch.common.xcontent.XContentParser;
19-
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
20-
import org.elasticsearch.search.aggregations.bucket.composite.HistogramValuesSourceBuilder;
21-
import org.elasticsearch.search.aggregations.bucket.histogram.HistogramAggregationBuilder;
2219
import org.elasticsearch.xpack.core.rollup.RollupField;
2320

2421
import java.io.IOException;
2522
import java.util.Arrays;
26-
import java.util.Collections;
2723
import java.util.List;
2824
import java.util.Map;
2925
import java.util.Objects;
30-
import java.util.stream.Collectors;
3126

3227
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3328

@@ -85,25 +80,6 @@ public String[] getFields() {
8580
return fields;
8681
}
8782

88-
/**
89-
* This returns a set of aggregation builders which represent the configured
90-
* set of histograms. Used by the rollup indexer to iterate over historical data
91-
*/
92-
public List<CompositeValuesSourceBuilder<?>> toBuilders() {
93-
if (fields.length == 0) {
94-
return Collections.emptyList();
95-
}
96-
97-
return Arrays.stream(fields).map(f -> {
98-
HistogramValuesSourceBuilder vsBuilder
99-
= new HistogramValuesSourceBuilder(RollupField.formatIndexerAggName(f, HistogramAggregationBuilder.NAME));
100-
vsBuilder.interval(interval);
101-
vsBuilder.field(f);
102-
vsBuilder.missingBucket(true);
103-
return vsBuilder;
104-
}).collect(Collectors.toList());
105-
}
106-
10783
public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
10884
ActionRequestValidationException validationException) {
10985

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

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,9 @@
1616
import org.elasticsearch.common.xcontent.ToXContentObject;
1717
import org.elasticsearch.common.xcontent.XContentBuilder;
1818
import org.elasticsearch.common.xcontent.XContentParser;
19-
import org.elasticsearch.search.aggregations.metrics.avg.AvgAggregationBuilder;
20-
import org.elasticsearch.search.aggregations.metrics.max.MaxAggregationBuilder;
21-
import org.elasticsearch.search.aggregations.metrics.min.MinAggregationBuilder;
22-
import org.elasticsearch.search.aggregations.metrics.sum.SumAggregationBuilder;
23-
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
24-
import org.elasticsearch.search.aggregations.support.ValueType;
25-
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregationBuilder;
2619
import org.elasticsearch.xpack.core.rollup.RollupField;
2720

2821
import java.io.IOException;
29-
import java.util.ArrayList;
30-
import java.util.Collections;
3122
import java.util.List;
3223
import java.util.Map;
3324
import java.util.Objects;
@@ -53,11 +44,11 @@
5344
public class MetricConfig implements Writeable, ToXContentObject {
5445

5546
// TODO: replace these with an enum
56-
private static final ParseField MIN = new ParseField("min");
57-
private static final ParseField MAX = new ParseField("max");
58-
private static final ParseField SUM = new ParseField("sum");
59-
private static final ParseField AVG = new ParseField("avg");
60-
private static final ParseField VALUE_COUNT = new ParseField("value_count");
47+
public static final ParseField MIN = new ParseField("min");
48+
public static final ParseField MAX = new ParseField("max");
49+
public static final ParseField SUM = new ParseField("sum");
50+
public static final ParseField AVG = new ParseField("avg");
51+
public static final ParseField VALUE_COUNT = new ParseField("value_count");
6152

6253
static final String NAME = "metrics";
6354
private static final String FIELD = "field";
@@ -111,46 +102,6 @@ public List<String> getMetrics() {
111102
return metrics;
112103
}
113104

114-
/**
115-
* This returns a set of aggregation builders which represent the configured
116-
* set of metrics. Used by the rollup indexer to iterate over historical data
117-
*/
118-
public List<ValuesSourceAggregationBuilder.LeafOnly> toBuilders() {
119-
if (metrics.size() == 0) {
120-
return Collections.emptyList();
121-
}
122-
123-
List<ValuesSourceAggregationBuilder.LeafOnly> aggs = new ArrayList<>(metrics.size());
124-
for (String metric : metrics) {
125-
ValuesSourceAggregationBuilder.LeafOnly newBuilder;
126-
if (metric.equals(MIN.getPreferredName())) {
127-
newBuilder = new MinAggregationBuilder(RollupField.formatFieldName(field, MinAggregationBuilder.NAME, RollupField.VALUE));
128-
} else if (metric.equals(MAX.getPreferredName())) {
129-
newBuilder = new MaxAggregationBuilder(RollupField.formatFieldName(field, MaxAggregationBuilder.NAME, RollupField.VALUE));
130-
} else if (metric.equals(AVG.getPreferredName())) {
131-
// Avgs are sum + count
132-
newBuilder = new SumAggregationBuilder(RollupField.formatFieldName(field, AvgAggregationBuilder.NAME, RollupField.VALUE));
133-
ValuesSourceAggregationBuilder.LeafOnly countBuilder
134-
= new ValueCountAggregationBuilder(
135-
RollupField.formatFieldName(field, AvgAggregationBuilder.NAME, RollupField.COUNT_FIELD), ValueType.NUMERIC);
136-
countBuilder.field(field);
137-
aggs.add(countBuilder);
138-
} else if (metric.equals(SUM.getPreferredName())) {
139-
newBuilder = new SumAggregationBuilder(RollupField.formatFieldName(field, SumAggregationBuilder.NAME, RollupField.VALUE));
140-
} else if (metric.equals(VALUE_COUNT.getPreferredName())) {
141-
// TODO allow non-numeric value_counts.
142-
// Hardcoding this is fine for now since the job validation guarantees that all metric fields are numerics
143-
newBuilder = new ValueCountAggregationBuilder(
144-
RollupField.formatFieldName(field, ValueCountAggregationBuilder.NAME, RollupField.VALUE), ValueType.NUMERIC);
145-
} else {
146-
throw new IllegalArgumentException("Unsupported metric type [" + metric + "]");
147-
}
148-
newBuilder.field(field);
149-
aggs.add(newBuilder);
150-
}
151-
return aggs;
152-
}
153-
154105
public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
155106
ActionRequestValidationException validationException) {
156107

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,11 @@
1818
import org.elasticsearch.common.xcontent.XContentParser;
1919
import org.elasticsearch.index.mapper.KeywordFieldMapper;
2020
import org.elasticsearch.index.mapper.TextFieldMapper;
21-
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
22-
import org.elasticsearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder;
23-
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
24-
import org.elasticsearch.xpack.core.rollup.RollupField;
2521

2622
import java.io.IOException;
2723
import java.util.Arrays;
2824
import java.util.List;
2925
import java.util.Map;
30-
import java.util.stream.Collectors;
3126

3227
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
3328

@@ -79,20 +74,6 @@ public String[] getFields() {
7974
return fields;
8075
}
8176

82-
/**
83-
* This returns a set of aggregation builders which represent the configured
84-
* set of date histograms. Used by the rollup indexer to iterate over historical data
85-
*/
86-
public List<CompositeValuesSourceBuilder<?>> toBuilders() {
87-
return Arrays.stream(fields).map(f -> {
88-
TermsValuesSourceBuilder vsBuilder
89-
= new TermsValuesSourceBuilder(RollupField.formatIndexerAggName(f, TermsAggregationBuilder.NAME));
90-
vsBuilder.field(f);
91-
vsBuilder.missingBucket(true);
92-
return vsBuilder;
93-
}).collect(Collectors.toList());
94-
}
95-
9677
public void validateMappings(Map<String, Map<String, FieldCapabilities>> fieldCapsResponse,
9778
ActionRequestValidationException validationException) {
9879

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

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@
99
import org.elasticsearch.action.fieldcaps.FieldCapabilities;
1010
import org.elasticsearch.common.io.stream.Writeable;
1111
import org.elasticsearch.common.xcontent.XContentParser;
12-
import org.elasticsearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder;
1312
import org.elasticsearch.test.AbstractSerializingTestCase;
1413

1514
import java.io.IOException;
1615
import java.util.Collections;
1716
import java.util.HashMap;
18-
import java.util.List;
1917
import java.util.Map;
2018

2119
import static org.elasticsearch.xpack.core.rollup.ConfigTestHelpers.randomTermsGroupConfig;
2220
import static org.hamcrest.Matchers.equalTo;
2321
import static org.mockito.Mockito.mock;
24-
import static org.mockito.Mockito.when;
2522

2623
public class TermsGroupConfigSerializingTests extends AbstractSerializingTestCase<TermsGroupConfig> {
2724

@@ -77,62 +74,4 @@ public void testValidateFieldWrongType() {
7774
assertThat(e.validationErrors().get(0), equalTo("The field referenced by a terms group must be a [numeric] or " +
7875
"[keyword/text] type, but found [geo_point] for field [my_field]"));
7976
}
80-
81-
public void testValidateFieldMatchingNotAggregatable() {
82-
ActionRequestValidationException e = new ActionRequestValidationException();
83-
Map<String, Map<String, FieldCapabilities>> responseMap = new HashMap<>();
84-
85-
// Have to mock fieldcaps because the ctor's aren't public...
86-
FieldCapabilities fieldCaps = mock(FieldCapabilities.class);
87-
when(fieldCaps.isAggregatable()).thenReturn(false);
88-
responseMap.put("my_field", Collections.singletonMap(getRandomType(), fieldCaps));
89-
90-
TermsGroupConfig config = new TermsGroupConfig("my_field");
91-
config.validateMappings(responseMap, e);
92-
assertThat(e.validationErrors().get(0), equalTo("The field [my_field] must be aggregatable across all indices, but is not."));
93-
}
94-
95-
public void testValidateMatchingField() {
96-
ActionRequestValidationException e = new ActionRequestValidationException();
97-
Map<String, Map<String, FieldCapabilities>> responseMap = new HashMap<>();
98-
String type = getRandomType();
99-
100-
// Have to mock fieldcaps because the ctor's aren't public...
101-
FieldCapabilities fieldCaps = mock(FieldCapabilities.class);
102-
when(fieldCaps.isAggregatable()).thenReturn(true);
103-
responseMap.put("my_field", Collections.singletonMap(type, fieldCaps));
104-
105-
TermsGroupConfig config = new TermsGroupConfig("my_field");
106-
config.validateMappings(responseMap, e);
107-
if (e.validationErrors().size() != 0) {
108-
fail(e.getMessage());
109-
}
110-
111-
List<CompositeValuesSourceBuilder<?>> builders = config.toBuilders();
112-
assertThat(builders.size(), equalTo(1));
113-
}
114-
115-
private String getRandomType() {
116-
int n = randomIntBetween(0,8);
117-
if (n == 0) {
118-
return "keyword";
119-
} else if (n == 1) {
120-
return "text";
121-
} else if (n == 2) {
122-
return "long";
123-
} else if (n == 3) {
124-
return "integer";
125-
} else if (n == 4) {
126-
return "short";
127-
} else if (n == 5) {
128-
return "float";
129-
} else if (n == 6) {
130-
return "double";
131-
} else if (n == 7) {
132-
return "scaled_float";
133-
} else if (n == 8) {
134-
return "half_float";
135-
}
136-
return "long";
137-
}
13877
}

0 commit comments

Comments
 (0)