Skip to content

Commit dd1943c

Browse files
author
Hendrik Muhs
authored
[ML-DataFrame] allow aggs as abbreviation for aggregations (#38706)
allow aggs as abbreviation for aggregations
1 parent 124b170 commit dd1943c

File tree

3 files changed

+86
-4
lines changed

3 files changed

+86
-4
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/IndicesClientIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,7 @@ public void testCRUDIndexTemplateWithTypes() throws Exception {
16971697
assertTrue(template2.mappings().containsKey("custom_doc_type"));
16981698

16991699
List<String> names = randomBoolean()
1700-
? Arrays.asList("*-1", "template-2")
1700+
? Arrays.asList("*plate-1", "template-2")
17011701
: Arrays.asList("template-*");
17021702
GetIndexTemplatesRequest getBothRequest = new GetIndexTemplatesRequest(names);
17031703
org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse getBoth = execute(
@@ -1834,7 +1834,7 @@ public void testIndexTemplatesExist() throws Exception {
18341834

18351835
{
18361836
final List<String> templateNames = randomBoolean()
1837-
? Arrays.asList("*-1", "template-2")
1837+
? Arrays.asList("*plate-1", "template-2")
18381838
: Arrays.asList("template-*");
18391839

18401840
final IndexTemplatesExistRequest bothRequest = new IndexTemplatesExistRequest(templateNames);

x-pack/plugin/data-frame/src/main/java/org/elasticsearch/xpack/dataframe/transforms/pivot/PivotConfig.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import java.util.Objects;
2323

2424
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
25+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
2526

2627
public class PivotConfig implements Writeable, ToXContentObject {
2728

2829
private static final String NAME = "data_frame_transform_pivot";
2930
private static final ParseField GROUP_BY = new ParseField("group_by");
3031
private static final ParseField AGGREGATIONS = new ParseField("aggregations");
32+
private static final ParseField AGGS = new ParseField("aggs");
33+
3134

3235
private final List<GroupConfig> groups;
3336
private final AggregationConfig aggregationConfig;
@@ -40,14 +43,32 @@ private static ConstructingObjectParser<PivotConfig, Void> createParser(boolean
4043
args -> {
4144
@SuppressWarnings("unchecked")
4245
List<GroupConfig> groups = (List<GroupConfig>) args[0];
43-
AggregationConfig aggregationConfig = (AggregationConfig) args[1];
46+
47+
// allow "aggs" and "aggregations" but require one to be specified
48+
// if somebody specifies both: throw
49+
AggregationConfig aggregationConfig = null;
50+
if (args[1] != null) {
51+
aggregationConfig = (AggregationConfig) args[1];
52+
}
53+
54+
if (args[2] != null) {
55+
if (aggregationConfig != null) {
56+
throw new IllegalArgumentException("Found two aggregation definitions: [aggs] and [aggregations]");
57+
}
58+
aggregationConfig = (AggregationConfig) args[2];
59+
}
60+
if (aggregationConfig == null) {
61+
throw new IllegalArgumentException("Required [aggregations]");
62+
}
63+
4464
return new PivotConfig(groups, aggregationConfig);
4565
});
4666

4767
parser.declareObjectArray(constructorArg(),
4868
(p, c) -> (GroupConfig.fromXContent(p, lenient)), GROUP_BY);
4969

50-
parser.declareObject(constructorArg(), (p, c) -> AggregationConfig.fromXContent(p, lenient), AGGREGATIONS);
70+
parser.declareObject(optionalConstructorArg(), (p, c) -> AggregationConfig.fromXContent(p, lenient), AGGREGATIONS);
71+
parser.declareObject(optionalConstructorArg(), (p, c) -> AggregationConfig.fromXContent(p, lenient), AGGS);
5172

5273
return parser;
5374
}

x-pack/plugin/data-frame/src/test/java/org/elasticsearch/xpack/dataframe/transforms/pivot/PivotConfigTests.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
package org.elasticsearch.xpack.dataframe.transforms.pivot;
88

99
import org.elasticsearch.common.io.stream.Writeable.Reader;
10+
import org.elasticsearch.common.xcontent.DeprecationHandler;
1011
import org.elasticsearch.common.xcontent.XContentParser;
12+
import org.elasticsearch.common.xcontent.XContentType;
1113
import org.elasticsearch.xpack.dataframe.transforms.AbstractSerializingDataFrameTestCase;
1214

1315
import java.io.IOException;
@@ -50,4 +52,63 @@ protected PivotConfig createTestInstance() {
5052
protected Reader<PivotConfig> instanceReader() {
5153
return PivotConfig::new;
5254
}
55+
56+
public void testAggsAbbreviations() throws IOException {
57+
String pivotAggs = "{"
58+
+ " \"group_by\": [ {"
59+
+ " \"id\": {"
60+
+ " \"terms\": {"
61+
+ " \"field\": \"id\""
62+
+ "} } } ],"
63+
+ " \"aggs\": {"
64+
+ " \"avg\": {"
65+
+ " \"avg\": {"
66+
+ " \"field\": \"points\""
67+
+ "} } } }";
68+
69+
PivotConfig p1 = createPivotConfigFromString(pivotAggs);
70+
String pivotAggregations = pivotAggs.replace("aggs", "aggregations");
71+
assertNotEquals(pivotAggs, pivotAggregations);
72+
PivotConfig p2 = createPivotConfigFromString(pivotAggregations);
73+
assertEquals(p1,p2);
74+
}
75+
76+
public void testMissingAggs() throws IOException {
77+
String pivot = "{"
78+
+ " \"group_by\": [ {"
79+
+ " \"id\": {"
80+
+ " \"terms\": {"
81+
+ " \"field\": \"id\""
82+
+ "} } } ] }";
83+
84+
expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot));
85+
}
86+
87+
public void testDoubleAggs() throws IOException {
88+
String pivot = "{"
89+
+ " \"group_by\": [ {"
90+
+ " \"id\": {"
91+
+ " \"terms\": {"
92+
+ " \"field\": \"id\""
93+
+ "} } } ],"
94+
+ " \"aggs\": {"
95+
+ " \"avg\": {"
96+
+ " \"avg\": {"
97+
+ " \"field\": \"points\""
98+
+ "} } },"
99+
+ " \"aggregations\": {"
100+
+ " \"avg\": {"
101+
+ " \"avg\": {"
102+
+ " \"field\": \"points\""
103+
+ "} } }"
104+
+ "}";
105+
106+
expectThrows(IllegalArgumentException.class, () -> createPivotConfigFromString(pivot));
107+
}
108+
109+
private PivotConfig createPivotConfigFromString(String json) throws IOException {
110+
final XContentParser parser = XContentType.JSON.xContent().createParser(xContentRegistry(),
111+
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, json);
112+
return PivotConfig.fromXContent(parser, false);
113+
}
53114
}

0 commit comments

Comments
 (0)