|
| 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.geobounds; |
| 21 | + |
| 22 | +import org.elasticsearch.common.collect.Tuple; |
| 23 | +import org.elasticsearch.common.geo.GeoPoint; |
| 24 | +import org.elasticsearch.common.xcontent.ConstructingObjectParser; |
| 25 | +import org.elasticsearch.common.xcontent.ObjectParser; |
| 26 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 27 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 28 | +import org.elasticsearch.search.aggregations.ParsedAggregation; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | + |
| 32 | +import static org.elasticsearch.common.xcontent.ConstructingObjectParser.constructorArg; |
| 33 | +import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.BOTTOM_RIGHT_FIELD; |
| 34 | +import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.BOUNDS_FIELD; |
| 35 | +import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.LAT_FIELD; |
| 36 | +import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.LON_FIELD; |
| 37 | +import static org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds.TOP_LEFT_FIELD; |
| 38 | + |
| 39 | +public class ParsedGeoBounds extends ParsedAggregation implements GeoBounds { |
| 40 | + private GeoPoint topLeft; |
| 41 | + private GeoPoint bottomRight; |
| 42 | + |
| 43 | + @Override |
| 44 | + public String getType() { |
| 45 | + return GeoBoundsAggregationBuilder.NAME; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public XContentBuilder doXContentBody(XContentBuilder builder, Params params) throws IOException { |
| 50 | + if (topLeft != null) { |
| 51 | + builder.startObject("bounds"); |
| 52 | + builder.startObject("top_left"); |
| 53 | + builder.field("lat", topLeft.getLat()); |
| 54 | + builder.field("lon", topLeft.getLon()); |
| 55 | + builder.endObject(); |
| 56 | + builder.startObject("bottom_right"); |
| 57 | + builder.field("lat", bottomRight.getLat()); |
| 58 | + builder.field("lon", bottomRight.getLon()); |
| 59 | + builder.endObject(); |
| 60 | + builder.endObject(); |
| 61 | + } |
| 62 | + return builder; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public GeoPoint topLeft() { |
| 67 | + return topLeft; |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public GeoPoint bottomRight() { |
| 72 | + return bottomRight; |
| 73 | + } |
| 74 | + |
| 75 | + private static final ObjectParser<ParsedGeoBounds, Void> PARSER = new ObjectParser<>(ParsedGeoBounds.class.getSimpleName(), true, |
| 76 | + ParsedGeoBounds::new); |
| 77 | + |
| 78 | + private static final ConstructingObjectParser<Tuple<GeoPoint, GeoPoint>, Void> BOUNDS_PARSER = |
| 79 | + new ConstructingObjectParser<>(ParsedGeoBounds.class.getSimpleName() + "_BOUNDS", true, |
| 80 | + args -> new Tuple<>((GeoPoint) args[0], (GeoPoint) args[1])); |
| 81 | + |
| 82 | + private static final ObjectParser<GeoPoint, Void> GEO_POINT_PARSER = new ObjectParser<>( |
| 83 | + ParsedGeoBounds.class.getSimpleName() + "_POINT", true, GeoPoint::new); |
| 84 | + |
| 85 | + static { |
| 86 | + declareAggregationFields(PARSER); |
| 87 | + PARSER.declareObject((agg, bbox) -> { |
| 88 | + agg.topLeft = bbox.v1(); |
| 89 | + agg.bottomRight = bbox.v2(); |
| 90 | + }, BOUNDS_PARSER, BOUNDS_FIELD); |
| 91 | + |
| 92 | + BOUNDS_PARSER.declareObject(constructorArg(), GEO_POINT_PARSER, TOP_LEFT_FIELD); |
| 93 | + BOUNDS_PARSER.declareObject(constructorArg(), GEO_POINT_PARSER, BOTTOM_RIGHT_FIELD); |
| 94 | + |
| 95 | + GEO_POINT_PARSER.declareDouble(GeoPoint::resetLat, LAT_FIELD); |
| 96 | + GEO_POINT_PARSER.declareDouble(GeoPoint::resetLon, LON_FIELD); |
| 97 | + } |
| 98 | + |
| 99 | + public static ParsedGeoBounds fromXContent(XContentParser parser, final String name) { |
| 100 | + ParsedGeoBounds geoBounds = PARSER.apply(parser, null); |
| 101 | + geoBounds.setName(name); |
| 102 | + return geoBounds; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments