Skip to content

Commit dc34f28

Browse files
committed
[ML-Dataframe] Data frame config HLRC objects (elastic#39691)
1 parent 1520ddc commit dc34f28

19 files changed

+1604
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.dataframe.transforms;
21+
22+
import org.elasticsearch.client.dataframe.transforms.pivot.PivotConfig;
23+
import org.elasticsearch.common.ParseField;
24+
import org.elasticsearch.common.Strings;
25+
import org.elasticsearch.common.xcontent.ConstructingObjectParser;
26+
import org.elasticsearch.common.xcontent.ToXContentObject;
27+
import org.elasticsearch.common.xcontent.XContentBuilder;
28+
import org.elasticsearch.common.xcontent.XContentParser;
29+
30+
import java.io.IOException;
31+
import java.util.Objects;
32+
33+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg;
34+
import static org.elasticsearch.common.xcontent.ConstructingObjectParser.optionalConstructorArg;
35+
36+
public class DataFrameTransformConfig implements ToXContentObject {
37+
38+
public static final ParseField ID = new ParseField("id");
39+
public static final ParseField SOURCE = new ParseField("source");
40+
public static final ParseField DEST = new ParseField("dest");
41+
public static final ParseField QUERY = new ParseField("query");
42+
// types of transforms
43+
public static final ParseField PIVOT_TRANSFORM = new ParseField("pivot");
44+
45+
private final String id;
46+
private final String source;
47+
private final String dest;
48+
private final QueryConfig queryConfig;
49+
private final PivotConfig pivotConfig;
50+
51+
private static final ConstructingObjectParser<DataFrameTransformConfig, String> PARSER =
52+
new ConstructingObjectParser<>("data_frame_transform", true,
53+
(args) -> {
54+
String id = (String) args[0];
55+
String source = (String) args[1];
56+
String dest = (String) args[2];
57+
QueryConfig queryConfig = (QueryConfig) args[3];
58+
PivotConfig pivotConfig = (PivotConfig) args[4];
59+
return new DataFrameTransformConfig(id, source, dest, queryConfig, pivotConfig);
60+
});
61+
62+
static {
63+
PARSER.declareString(constructorArg(), ID);
64+
PARSER.declareString(constructorArg(), SOURCE);
65+
PARSER.declareString(constructorArg(), DEST);
66+
PARSER.declareObject(optionalConstructorArg(), (p, c) -> QueryConfig.fromXContent(p), QUERY);
67+
PARSER.declareObject(optionalConstructorArg(), (p, c) -> PivotConfig.fromXContent(p), PIVOT_TRANSFORM);
68+
}
69+
70+
public static DataFrameTransformConfig fromXContent(final XContentParser parser) {
71+
return PARSER.apply(parser, null);
72+
}
73+
74+
75+
public DataFrameTransformConfig(final String id,
76+
final String source,
77+
final String dest,
78+
final QueryConfig queryConfig,
79+
final PivotConfig pivotConfig) {
80+
this.id = Objects.requireNonNull(id);
81+
this.source = Objects.requireNonNull(source);
82+
this.dest = Objects.requireNonNull(dest);
83+
this.queryConfig = queryConfig;
84+
this.pivotConfig = pivotConfig;
85+
}
86+
87+
public String getId() {
88+
return id;
89+
}
90+
91+
public String getSource() {
92+
return source;
93+
}
94+
95+
public String getDestination() {
96+
return dest;
97+
}
98+
99+
public PivotConfig getPivotConfig() {
100+
return pivotConfig;
101+
}
102+
103+
public QueryConfig getQueryConfig() {
104+
return queryConfig;
105+
}
106+
107+
public boolean isValid() {
108+
if (queryConfig != null && queryConfig.isValid() == false) {
109+
return false;
110+
}
111+
112+
if (pivotConfig == null || pivotConfig.isValid() == false) {
113+
return false;
114+
}
115+
116+
return true;
117+
}
118+
119+
@Override
120+
public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException {
121+
builder.startObject();
122+
builder.field(ID.getPreferredName(), id);
123+
builder.field(SOURCE.getPreferredName(), source);
124+
builder.field(DEST.getPreferredName(), dest);
125+
if (queryConfig != null) {
126+
builder.field(QUERY.getPreferredName(), queryConfig);
127+
}
128+
if (pivotConfig != null) {
129+
builder.field(PIVOT_TRANSFORM.getPreferredName(), pivotConfig);
130+
}
131+
builder.endObject();
132+
return builder;
133+
}
134+
135+
@Override
136+
public boolean equals(Object other) {
137+
if (this == other) {
138+
return true;
139+
}
140+
141+
if (other == null || getClass() != other.getClass()) {
142+
return false;
143+
}
144+
145+
final DataFrameTransformConfig that = (DataFrameTransformConfig) other;
146+
147+
return Objects.equals(this.id, that.id)
148+
&& Objects.equals(this.source, that.source)
149+
&& Objects.equals(this.dest, that.dest)
150+
&& Objects.equals(this.queryConfig, that.queryConfig)
151+
&& Objects.equals(this.pivotConfig, that.pivotConfig);
152+
}
153+
154+
@Override
155+
public int hashCode() {
156+
return Objects.hash(id, source, dest, queryConfig, pivotConfig);
157+
}
158+
159+
@Override
160+
public String toString() {
161+
return Strings.toString(this, true, true);
162+
}
163+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.dataframe.transforms;
21+
22+
import org.elasticsearch.common.xcontent.ToXContentObject;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
import org.elasticsearch.index.query.AbstractQueryBuilder;
26+
import org.elasticsearch.index.query.QueryBuilder;
27+
28+
import java.io.IOException;
29+
import java.util.Objects;
30+
31+
public class QueryConfig implements ToXContentObject {
32+
33+
private final QueryBuilder query;
34+
35+
public static QueryConfig fromXContent(XContentParser parser) throws IOException {
36+
QueryBuilder query = AbstractQueryBuilder.parseInnerQueryBuilder(parser);
37+
return new QueryConfig(query);
38+
}
39+
40+
public QueryConfig(QueryBuilder query) {
41+
this.query = query;
42+
}
43+
44+
@Override
45+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
46+
query.toXContent(builder, params);
47+
return builder;
48+
}
49+
50+
public QueryBuilder getQuery() {
51+
return query;
52+
}
53+
54+
@Override
55+
public int hashCode() {
56+
return Objects.hash(query);
57+
}
58+
59+
@Override
60+
public boolean equals(Object other) {
61+
if (this == other) {
62+
return true;
63+
}
64+
65+
if (other == null || getClass() != other.getClass()) {
66+
return false;
67+
}
68+
69+
final QueryConfig that = (QueryConfig) other;
70+
71+
return Objects.equals(this.query, that.query);
72+
}
73+
74+
public boolean isValid() {
75+
return this.query != null;
76+
}
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.client.dataframe.transforms.pivot;
21+
22+
import org.elasticsearch.common.xcontent.ToXContentObject;
23+
import org.elasticsearch.common.xcontent.XContentBuilder;
24+
import org.elasticsearch.common.xcontent.XContentParser;
25+
import org.elasticsearch.search.aggregations.AggregationBuilder;
26+
import org.elasticsearch.search.aggregations.AggregatorFactories;
27+
28+
import java.io.IOException;
29+
import java.util.Collection;
30+
import java.util.Objects;
31+
32+
public class AggregationConfig implements ToXContentObject {
33+
private final AggregatorFactories.Builder aggregations;
34+
35+
public static AggregationConfig fromXContent(XContentParser parser) throws IOException {
36+
if (parser.currentToken() == null) {
37+
parser.nextToken();
38+
}
39+
AggregatorFactories.Builder aggregations = AggregatorFactories.parseAggregators(parser);
40+
return new AggregationConfig(aggregations);
41+
}
42+
43+
public AggregationConfig(AggregatorFactories.Builder aggregations) {
44+
this.aggregations = aggregations;
45+
}
46+
47+
@Override
48+
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
49+
aggregations.toXContent(builder, params);
50+
return builder;
51+
}
52+
53+
public Collection<AggregationBuilder> getAggregatorFactories() {
54+
return aggregations.getAggregatorFactories();
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return Objects.hash(aggregations);
60+
}
61+
62+
@Override
63+
public boolean equals(Object other) {
64+
if (this == other) {
65+
return true;
66+
}
67+
68+
if (other == null || getClass() != other.getClass()) {
69+
return false;
70+
}
71+
72+
final AggregationConfig that = (AggregationConfig) other;
73+
74+
return Objects.equals(this.aggregations, that.aggregations);
75+
}
76+
77+
public boolean isValid() {
78+
return this.aggregations != null;
79+
}
80+
}

0 commit comments

Comments
 (0)