-
Notifications
You must be signed in to change notification settings - Fork 25.7k
Add parsing methods for InternalDateHistogram and InternalHistogram #24213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.aggregations; | ||
|
|
||
| import org.elasticsearch.common.CheckedFunction; | ||
| import org.elasticsearch.common.xcontent.ObjectParser; | ||
| import org.elasticsearch.common.xcontent.XContentBuilder; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.common.xcontent.XContentParserUtils; | ||
| import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.function.Supplier; | ||
|
|
||
| import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; | ||
|
|
||
| public abstract class ParsedMultiBucketAggregation extends ParsedAggregation implements MultiBucketsAggregation { | ||
|
|
||
| protected final List<ParsedBucket<?>> buckets = new ArrayList<>(); | ||
| protected boolean keyed; | ||
|
|
||
| @Override | ||
| protected XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { | ||
| if (keyed) { | ||
| builder.startObject(CommonFields.BUCKETS.getPreferredName()); | ||
| } else { | ||
| builder.startArray(CommonFields.BUCKETS.getPreferredName()); | ||
| } | ||
| for (ParsedBucket<?> bucket : buckets) { | ||
| bucket.toXContent(builder, params); | ||
| } | ||
| if (keyed) { | ||
| builder.endObject(); | ||
| } else { | ||
| builder.endArray(); | ||
| } | ||
| return builder; | ||
| } | ||
|
|
||
| protected static void declareMultiBucketAggregationFields(final ObjectParser<? extends ParsedMultiBucketAggregation, Void> objectParser, | ||
| final CheckedFunction<XContentParser, ParsedBucket<?>, IOException> bucketParser, | ||
| final CheckedFunction<XContentParser, ParsedBucket<?>, IOException> keyedBucketParser) { | ||
| declareAggregationFields(objectParser); | ||
| objectParser.declareField((parser, aggregation, context) -> { | ||
| XContentParser.Token token = parser.currentToken(); | ||
| if (token == XContentParser.Token.START_OBJECT) { | ||
| aggregation.keyed = true; | ||
| while (parser.nextToken() != XContentParser.Token.END_OBJECT) { | ||
| aggregation.buckets.add(keyedBucketParser.apply(parser)); | ||
| } | ||
| } else if (token == XContentParser.Token.START_ARRAY) { | ||
| aggregation.keyed = false; | ||
| while (parser.nextToken() != XContentParser.Token.END_ARRAY) { | ||
| aggregation.buckets.add(bucketParser.apply(parser)); | ||
| } | ||
| } | ||
| }, CommonFields.BUCKETS, ObjectParser.ValueType.OBJECT_ARRAY); | ||
| } | ||
|
|
||
| public static class ParsedBucket<T> implements MultiBucketsAggregation.Bucket { | ||
|
|
||
| private Aggregations aggregations; | ||
| private T key; | ||
| private String keyAsString; | ||
| private long docCount; | ||
| private boolean keyed; | ||
|
|
||
| protected void setKey(T key) { | ||
| this.key = key; | ||
| } | ||
|
|
||
| @Override | ||
| public Object getKey() { | ||
| return key; | ||
| } | ||
|
|
||
| protected void setKeyAsString(String keyAsString) { | ||
| this.keyAsString = keyAsString; | ||
| } | ||
|
|
||
| @Override | ||
| public String getKeyAsString() { | ||
| return keyAsString; | ||
| } | ||
|
|
||
| protected void setDocCount(long docCount) { | ||
| this.docCount = docCount; | ||
| } | ||
|
|
||
| @Override | ||
| public long getDocCount() { | ||
| return docCount; | ||
| } | ||
|
|
||
| public void setKeyed(boolean keyed) { | ||
| this.keyed = keyed; | ||
| } | ||
|
|
||
| protected void setAggregations(Aggregations aggregations) { | ||
| this.aggregations = aggregations; | ||
| } | ||
|
|
||
| @Override | ||
| public Aggregations getAggregations() { | ||
| return aggregations; | ||
| } | ||
|
|
||
| @Override | ||
| public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { | ||
| if (keyed) { | ||
| // Subclasses can override the getKeyAsString method to handle specific cases like | ||
| // keyed bucket with RAW doc value format where the key_as_string field is not printed | ||
| // out but we still need to have a string version of the key to use as the bucket's name. | ||
| builder.startObject(getKeyAsString()); | ||
| } else { | ||
| builder.startObject(); | ||
| } | ||
| if (keyAsString != null) { | ||
| builder.field(CommonFields.KEY_AS_STRING.getPreferredName(), getKeyAsString()); | ||
| } | ||
| builder.field(CommonFields.KEY.getPreferredName(), key); | ||
| builder.field(CommonFields.DOC_COUNT.getPreferredName(), docCount); | ||
| aggregations.toXContentInternal(builder, params); | ||
| builder.endObject(); | ||
| return builder; | ||
| } | ||
|
|
||
| protected static <T, B extends ParsedBucket<T>> B parseXContent(final XContentParser parser, | ||
| final boolean keyed, | ||
| final Supplier<B> bucketSupplier, | ||
| final CheckedFunction<XContentParser, T, IOException> keyParser) | ||
| throws IOException { | ||
| final B bucket = bucketSupplier.get(); | ||
| bucket.setKeyed(keyed); | ||
| XContentParser.Token token = parser.currentToken(); | ||
| String currentFieldName = parser.currentName(); | ||
| if (keyed) { | ||
| ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser::getTokenLocation); | ||
| ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation); | ||
| } | ||
|
|
||
| List<Aggregation> aggregations = new ArrayList<>(); | ||
| while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { | ||
| if (token == XContentParser.Token.FIELD_NAME) { | ||
| currentFieldName = parser.currentName(); | ||
| } else if (token.isValue()) { | ||
| if (CommonFields.KEY_AS_STRING.getPreferredName().equals(currentFieldName)) { | ||
| bucket.setKeyAsString(parser.text()); | ||
| } else if (CommonFields.KEY.getPreferredName().equals(currentFieldName)) { | ||
| bucket.setKey(keyParser.apply(parser)); | ||
| } else if (CommonFields.DOC_COUNT.getPreferredName().equals(currentFieldName)) { | ||
| bucket.setDocCount(parser.longValue()); | ||
| } | ||
| } else if (token == XContentParser.Token.START_OBJECT) { | ||
| aggregations.add(XContentParserUtils.parseTypedKeysObject(parser, Aggregation.TYPED_KEYS_DELIMITER, Aggregation.class)); | ||
| } | ||
| } | ||
| bucket.setAggregations(new Aggregations(aggregations)); | ||
| return bucket; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.aggregations.bucket.histogram; | ||
|
|
||
| import org.elasticsearch.common.xcontent.ObjectParser; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.search.DocValueFormat; | ||
| import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; | ||
| import org.joda.time.DateTime; | ||
| import org.joda.time.DateTimeZone; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ParsedDateHistogram extends ParsedMultiBucketAggregation implements Histogram { | ||
|
|
||
| @Override | ||
| protected String getType() { | ||
| return DateHistogramAggregationBuilder.NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends Histogram.Bucket> getBuckets() { | ||
| return buckets.stream().map(bucket -> (Histogram.Bucket) bucket).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static ObjectParser<ParsedDateHistogram, Void> PARSER = | ||
| new ObjectParser<>(ParsedDateHistogram.class.getSimpleName(), true, ParsedDateHistogram::new); | ||
| static { | ||
| declareMultiBucketAggregationFields(PARSER, | ||
| parser -> ParsedBucket.fromXContent(parser, false), | ||
| parser -> ParsedBucket.fromXContent(parser, true)); | ||
| } | ||
|
|
||
| public static ParsedDateHistogram fromXContent(XContentParser parser, String name) throws IOException { | ||
| ParsedDateHistogram aggregation = PARSER.parse(parser, null); | ||
| aggregation.setName(name); | ||
| return aggregation; | ||
| } | ||
|
|
||
| public static class ParsedBucket extends ParsedMultiBucketAggregation.ParsedBucket<Long> implements Histogram.Bucket { | ||
|
|
||
| @Override | ||
| public Object getKey() { | ||
| return new DateTime(super.getKey(), DateTimeZone.UTC); | ||
| } | ||
|
|
||
| @Override | ||
| public String getKeyAsString() { | ||
| String keyAsString = super.getKeyAsString(); | ||
| if (keyAsString != null) { | ||
| return keyAsString; | ||
| } else { | ||
| return DocValueFormat.RAW.format((Long) super.getKey()); | ||
| } | ||
| } | ||
|
|
||
| static ParsedBucket fromXContent(XContentParser parser, boolean keyed) throws IOException { | ||
| return parseXContent(parser, keyed, ParsedBucket::new, XContentParser::longValue); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* | ||
| * Licensed to Elasticsearch under one or more contributor | ||
| * license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright | ||
| * ownership. Elasticsearch licenses this file to you under | ||
| * the Apache License, Version 2.0 (the "License"); you may | ||
| * not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package org.elasticsearch.search.aggregations.bucket.histogram; | ||
|
|
||
| import org.elasticsearch.common.xcontent.ObjectParser; | ||
| import org.elasticsearch.common.xcontent.XContentParser; | ||
| import org.elasticsearch.search.DocValueFormat; | ||
| import org.elasticsearch.search.aggregations.ParsedMultiBucketAggregation; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ParsedHistogram extends ParsedMultiBucketAggregation implements Histogram { | ||
|
|
||
| @Override | ||
| protected String getType() { | ||
| return HistogramAggregationBuilder.NAME; | ||
| } | ||
|
|
||
| @Override | ||
| public List<? extends Histogram.Bucket> getBuckets() { | ||
| return buckets.stream().map(bucket -> (Histogram.Bucket) bucket).collect(Collectors.toList()); | ||
| } | ||
|
|
||
| private static ObjectParser<ParsedHistogram, Void> PARSER = | ||
| new ObjectParser<>(ParsedHistogram.class.getSimpleName(), true, ParsedHistogram::new); | ||
| static { | ||
|
||
| declareMultiBucketAggregationFields(PARSER, | ||
| parser -> ParsedBucket.fromXContent(parser, false), | ||
| parser -> ParsedBucket.fromXContent(parser, true)); | ||
| } | ||
|
|
||
| public static ParsedHistogram fromXContent(XContentParser parser, String name) throws IOException { | ||
| ParsedHistogram aggregation = PARSER.parse(parser, null); | ||
| aggregation.setName(name); | ||
| return aggregation; | ||
| } | ||
|
|
||
| static class ParsedBucket extends ParsedMultiBucketAggregation.ParsedBucket<Double> implements Histogram.Bucket { | ||
|
|
||
| @Override | ||
| public String getKeyAsString() { | ||
| String keyAsString = super.getKeyAsString(); | ||
| if (keyAsString != null) { | ||
| return keyAsString; | ||
| } else { | ||
| return DocValueFormat.RAW.format((Double) getKey()); | ||
| } | ||
| } | ||
|
|
||
| static ParsedBucket fromXContent(XContentParser parser, boolean keyed) throws IOException { | ||
| return parseXContent(parser, keyed, ParsedBucket::new, XContentParser::doubleValue); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but then why don't we use the getter below too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It can be aligned - both now use getKeyAsString()