|
| 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.search.aggregations.metrics.geocentroid; |
| 21 | + |
| 22 | +import org.elasticsearch.common.geo.GeoPoint; |
| 23 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 26 | +import org.elasticsearch.search.aggregations.ParsedAggregation; |
| 27 | +import org.elasticsearch.search.aggregations.metrics.geocentroid.InternalGeoCentroid.Fields; |
| 28 | + |
| 29 | +import java.io.IOException; |
| 30 | + |
| 31 | +/** |
| 32 | + * Serialization and merge logic for {@link GeoCentroidAggregator}. |
| 33 | + */ |
| 34 | +public class ParsedGeoCentroid extends ParsedAggregation implements GeoCentroid { |
| 35 | + private GeoPoint centroid; |
| 36 | + private long count; |
| 37 | + |
| 38 | + @Override |
| 39 | + public GeoPoint centroid() { |
| 40 | + return centroid; |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public long count() { |
| 45 | + return count; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected String getType() { |
| 50 | + return GeoCentroidAggregationBuilder.NAME; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { |
| 55 | + if (centroid != null) { |
| 56 | + builder.startObject(Fields.CENTROID.getPreferredName()); |
| 57 | + { |
| 58 | + builder.field(Fields.CENTROID_LAT.getPreferredName(), centroid.lat()); |
| 59 | + builder.field(Fields.CENTROID_LON.getPreferredName(), centroid.lon()); |
| 60 | + } |
| 61 | + builder.endObject(); |
| 62 | + } |
| 63 | + builder.field(Fields.COUNT.getPreferredName(), count); |
| 64 | + return builder; |
| 65 | + } |
| 66 | + |
| 67 | + private static final ObjectParser<ParsedGeoCentroid, Void> PARSER = new ObjectParser<>(ParsedGeoCentroid.class.getSimpleName(), true, |
| 68 | + ParsedGeoCentroid::new); |
| 69 | + |
| 70 | + private static final ObjectParser<GeoPoint, Void> GEO_POINT_PARSER = new ObjectParser<>( |
| 71 | + ParsedGeoCentroid.class.getSimpleName() + "_POINT", true, GeoPoint::new); |
| 72 | + |
| 73 | + static { |
| 74 | + declareAggregationFields(PARSER); |
| 75 | + PARSER.declareObject((agg, centroid) -> agg.centroid = centroid, GEO_POINT_PARSER, Fields.CENTROID); |
| 76 | + PARSER.declareLong((agg, count) -> agg.count = count, Fields.COUNT); |
| 77 | + |
| 78 | + GEO_POINT_PARSER.declareDouble(GeoPoint::resetLat, Fields.CENTROID_LAT); |
| 79 | + GEO_POINT_PARSER.declareDouble(GeoPoint::resetLon, Fields.CENTROID_LON); |
| 80 | + } |
| 81 | + |
| 82 | + public static ParsedGeoCentroid fromXContent(XContentParser parser, final String name) { |
| 83 | + ParsedGeoCentroid geoCentroid = PARSER.apply(parser, null); |
| 84 | + geoCentroid.setName(name); |
| 85 | + return geoCentroid; |
| 86 | + } |
| 87 | +} |
0 commit comments