Skip to content

Commit c4fc8ed

Browse files
authored
Add parsing for single bucket aggregations (#24564)
This adds parsing to all implementations of SingleBucketAggregations. They are mostly similar, so they share the common base class `ParsedSingleBucketAggregation` and the shared base test `InternalSingleBucketAggregationTestCase`.
1 parent 570390a commit c4fc8ed

File tree

20 files changed

+471
-5
lines changed

20 files changed

+471
-5
lines changed

core/src/main/java/org/elasticsearch/search/aggregations/ParsedAggregation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected static void declareAggregationFields(ObjectParser<? extends ParsedAggr
4141
}
4242

4343
private String name;
44-
Map<String, Object> metadata;
44+
protected Map<String, Object> metadata;
4545

4646
@Override
4747
public final String getName() {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
package org.elasticsearch.search.aggregations.bucket;
20+
21+
import org.elasticsearch.common.xcontent.XContentBuilder;
22+
import org.elasticsearch.common.xcontent.XContentParser;
23+
import org.elasticsearch.common.xcontent.XContentParserUtils;
24+
import org.elasticsearch.search.aggregations.Aggregation;
25+
import org.elasticsearch.search.aggregations.Aggregations;
26+
import org.elasticsearch.search.aggregations.ParsedAggregation;
27+
28+
import java.io.IOException;
29+
import java.util.ArrayList;
30+
import java.util.Collections;
31+
import java.util.List;
32+
33+
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
34+
35+
/**
36+
* A base class for all the single bucket aggregations.
37+
*/
38+
public abstract class ParsedSingleBucketAggregation extends ParsedAggregation implements SingleBucketAggregation {
39+
40+
private long docCount;
41+
protected Aggregations aggregations = new Aggregations(Collections.emptyList());
42+
43+
@Override
44+
public long getDocCount() {
45+
return docCount;
46+
}
47+
48+
protected void setDocCount(long docCount) {
49+
this.docCount = docCount;
50+
}
51+
52+
@Override
53+
public Aggregations getAggregations() {
54+
return aggregations;
55+
}
56+
57+
@Override
58+
public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException {
59+
builder.field(CommonFields.DOC_COUNT.getPreferredName(), docCount);
60+
aggregations.toXContentInternal(builder, params);
61+
return builder;
62+
}
63+
64+
protected static <T extends ParsedSingleBucketAggregation> T parseXContent(final XContentParser parser, T aggregation, String name)
65+
throws IOException {
66+
aggregation.setName(name);
67+
XContentParser.Token token = parser.currentToken();
68+
String currentFieldName = parser.currentName();
69+
if (token == XContentParser.Token.FIELD_NAME) {
70+
token = parser.nextToken();
71+
}
72+
ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser::getTokenLocation);
73+
74+
List<Aggregation> aggregations = new ArrayList<>();
75+
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
76+
if (token == XContentParser.Token.FIELD_NAME) {
77+
currentFieldName = parser.currentName();
78+
} else if (token.isValue()) {
79+
if (CommonFields.DOC_COUNT.getPreferredName().equals(currentFieldName)) {
80+
aggregation.setDocCount(parser.longValue());
81+
}
82+
} else if (token == XContentParser.Token.START_OBJECT) {
83+
if (CommonFields.META.getPreferredName().equals(currentFieldName)) {
84+
aggregation.metadata = parser.map();
85+
} else {
86+
aggregations.add(XContentParserUtils.parseTypedKeysObject(parser, Aggregation.TYPED_KEYS_DELIMITER, Aggregation.class));
87+
}
88+
}
89+
}
90+
aggregation.aggregations = new Aggregations(aggregations);
91+
return aggregation;
92+
}
93+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.elasticsearch.search.aggregations.bucket.children;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation;
23+
24+
import java.io.IOException;
25+
26+
public class ParsedChildren extends ParsedSingleBucketAggregation implements Children {
27+
28+
@Override
29+
public String getType() {
30+
return ChildrenAggregationBuilder.NAME;
31+
}
32+
33+
public static ParsedChildren fromXContent(XContentParser parser, final String name) throws IOException {
34+
return parseXContent(parser, new ParsedChildren(), name);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.elasticsearch.search.aggregations.bucket.filter;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation;
23+
24+
import java.io.IOException;
25+
26+
public class ParsedFilter extends ParsedSingleBucketAggregation implements Filter {
27+
28+
@Override
29+
public String getType() {
30+
return FilterAggregationBuilder.NAME;
31+
}
32+
33+
public static ParsedFilter fromXContent(XContentParser parser, final String name) throws IOException {
34+
return parseXContent(parser, new ParsedFilter(), name);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.elasticsearch.search.aggregations.bucket.global;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation;
23+
24+
import java.io.IOException;
25+
26+
public class ParsedGlobal extends ParsedSingleBucketAggregation implements Global {
27+
28+
@Override
29+
public String getType() {
30+
return GlobalAggregationBuilder.NAME;
31+
}
32+
33+
public static ParsedGlobal fromXContent(XContentParser parser, final String name) throws IOException {
34+
return parseXContent(parser, new ParsedGlobal(), name);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.elasticsearch.search.aggregations.bucket.missing;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation;
23+
24+
import java.io.IOException;
25+
26+
public class ParsedMissing extends ParsedSingleBucketAggregation implements Missing {
27+
28+
@Override
29+
public String getType() {
30+
return MissingAggregationBuilder.NAME;
31+
}
32+
33+
public static ParsedMissing fromXContent(XContentParser parser, final String name) throws IOException {
34+
return parseXContent(parser, new ParsedMissing(), name);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.elasticsearch.search.aggregations.bucket.nested;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation;
23+
24+
import java.io.IOException;
25+
26+
public class ParsedNested extends ParsedSingleBucketAggregation implements Nested {
27+
28+
@Override
29+
public String getType() {
30+
return NestedAggregationBuilder.NAME;
31+
}
32+
33+
public static ParsedNested fromXContent(XContentParser parser, final String name) throws IOException {
34+
return parseXContent(parser, new ParsedNested(), name);
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.elasticsearch.search.aggregations.bucket.nested;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation;
23+
24+
import java.io.IOException;
25+
26+
public class ParsedReverseNested extends ParsedSingleBucketAggregation implements Nested {
27+
28+
@Override
29+
public String getType() {
30+
return ReverseNestedAggregationBuilder.NAME;
31+
}
32+
33+
public static ParsedReverseNested fromXContent(XContentParser parser, final String name) throws IOException {
34+
return parseXContent(parser, new ParsedReverseNested(), name);
35+
}
36+
}

core/src/main/java/org/elasticsearch/search/aggregations/bucket/sampler/InternalSampler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public String getWriteableName() {
4949

5050
@Override
5151
public String getType() {
52-
return "sampler";
52+
return NAME;
5353
}
5454

5555
@Override
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
package org.elasticsearch.search.aggregations.bucket.sampler;
20+
21+
import org.elasticsearch.common.xcontent.XContentParser;
22+
import org.elasticsearch.search.aggregations.bucket.ParsedSingleBucketAggregation;
23+
24+
import java.io.IOException;
25+
26+
public class ParsedSampler extends ParsedSingleBucketAggregation implements Sampler {
27+
28+
@Override
29+
public String getType() {
30+
return InternalSampler.NAME;
31+
}
32+
33+
public static ParsedSampler fromXContent(XContentParser parser, final String name) throws IOException {
34+
return parseXContent(parser, new ParsedSampler(), name);
35+
}
36+
}

0 commit comments

Comments
 (0)