|
| 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.bucket.composite; |
| 21 | + |
| 22 | +import org.elasticsearch.common.ParseField; |
| 23 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 24 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 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.index.mapper.MappedFieldType; |
| 29 | +import org.elasticsearch.search.DocValueFormat; |
| 30 | +import org.elasticsearch.search.aggregations.bucket.geogrid.CellIdSource; |
| 31 | +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileGridAggregationBuilder; |
| 32 | +import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; |
| 33 | +import org.elasticsearch.search.aggregations.support.ValueType; |
| 34 | +import org.elasticsearch.search.aggregations.support.ValuesSource; |
| 35 | +import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; |
| 36 | +import org.elasticsearch.search.internal.SearchContext; |
| 37 | + |
| 38 | +import java.io.IOException; |
| 39 | +import java.util.Objects; |
| 40 | + |
| 41 | +public class GeoTileGridValuesSourceBuilder extends CompositeValuesSourceBuilder<GeoTileGridValuesSourceBuilder> { |
| 42 | + static final String TYPE = "geotile_grid"; |
| 43 | + |
| 44 | + private static final ObjectParser<GeoTileGridValuesSourceBuilder, Void> PARSER; |
| 45 | + static { |
| 46 | + PARSER = new ObjectParser<>(GeoTileGridValuesSourceBuilder.TYPE); |
| 47 | + PARSER.declareInt(GeoTileGridValuesSourceBuilder::precision, new ParseField("precision")); |
| 48 | + CompositeValuesSourceParserHelper.declareValuesSourceFields(PARSER, ValueType.NUMERIC); |
| 49 | + } |
| 50 | + |
| 51 | + static GeoTileGridValuesSourceBuilder parse(String name, XContentParser parser) throws IOException { |
| 52 | + return PARSER.parse(parser, new GeoTileGridValuesSourceBuilder(name), null); |
| 53 | + } |
| 54 | + |
| 55 | + private int precision = GeoTileGridAggregationBuilder.DEFAULT_PRECISION; |
| 56 | + |
| 57 | + GeoTileGridValuesSourceBuilder(String name) { |
| 58 | + super(name); |
| 59 | + } |
| 60 | + |
| 61 | + GeoTileGridValuesSourceBuilder(StreamInput in) throws IOException { |
| 62 | + super(in); |
| 63 | + this.precision = in.readInt(); |
| 64 | + } |
| 65 | + |
| 66 | + public GeoTileGridValuesSourceBuilder precision(int precision) { |
| 67 | + this.precision = GeoTileUtils.checkPrecisionRange(precision); |
| 68 | + return this; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public GeoTileGridValuesSourceBuilder format(String format) { |
| 73 | + throw new IllegalArgumentException("[format] is not supported for [" + TYPE + "]"); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + protected void innerWriteTo(StreamOutput out) throws IOException { |
| 78 | + out.writeInt(precision); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + protected void doXContentBody(XContentBuilder builder, Params params) throws IOException { |
| 83 | + builder.field("precision", precision); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + String type() { |
| 88 | + return TYPE; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public int hashCode() { |
| 93 | + return Objects.hash(super.hashCode(), precision); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean equals(Object obj) { |
| 98 | + if (this == obj) return true; |
| 99 | + if (obj == null || getClass() != obj.getClass()) return false; |
| 100 | + if (super.equals(obj) == false) return false; |
| 101 | + GeoTileGridValuesSourceBuilder other = (GeoTileGridValuesSourceBuilder) obj; |
| 102 | + return precision == other.precision; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + protected CompositeValuesSourceConfig innerBuild(SearchContext context, ValuesSourceConfig<?> config) throws IOException { |
| 107 | + ValuesSource orig = config.toValuesSource(context.getQueryShardContext()); |
| 108 | + if (orig == null) { |
| 109 | + orig = ValuesSource.GeoPoint.EMPTY; |
| 110 | + } |
| 111 | + if (orig instanceof ValuesSource.GeoPoint) { |
| 112 | + ValuesSource.GeoPoint geoPoint = (ValuesSource.GeoPoint) orig; |
| 113 | + // is specified in the builder. |
| 114 | + final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; |
| 115 | + CellIdSource cellIdSource = new CellIdSource(geoPoint, precision, GeoTileUtils::longEncode); |
| 116 | + return new CompositeValuesSourceConfig(name, fieldType, cellIdSource, DocValueFormat.GEOTILE, order(), missingBucket()); |
| 117 | + } else { |
| 118 | + throw new IllegalArgumentException("invalid source, expected geo_point, got " + orig.getClass().getSimpleName()); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments