-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Support geotile_grid aggregation in composite agg sources #45810
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 11 commits
96887d7
bb572f7
a8af9fa
5a3a0a9
6cadfa4
b22d8ca
82c3b56
9b21532
3ba8579
30dacd4
ecdcce6
2e4461d
022e66d
ff518c9
b3e6f9d
54c9e12
c30cc32
2a09dc1
5ecd671
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,117 @@ | ||
/* | ||
* 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.composite; | ||
|
||
import org.elasticsearch.common.ParseField; | ||
import org.elasticsearch.common.geo.GeoUtils; | ||
import org.elasticsearch.common.io.stream.StreamInput; | ||
import org.elasticsearch.common.io.stream.StreamOutput; | ||
import org.elasticsearch.common.xcontent.ObjectParser; | ||
import org.elasticsearch.common.xcontent.XContentBuilder; | ||
import org.elasticsearch.common.xcontent.XContentParser; | ||
import org.elasticsearch.geometry.utils.Geohash; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.search.DocValueFormat; | ||
import org.elasticsearch.search.aggregations.bucket.geogrid.CellIdSource; | ||
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridAggregationBuilder; | ||
import org.elasticsearch.search.aggregations.support.ValueType; | ||
import org.elasticsearch.search.aggregations.support.ValuesSource; | ||
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig; | ||
import org.elasticsearch.search.internal.SearchContext; | ||
|
||
import java.io.IOException; | ||
import java.util.Objects; | ||
|
||
public class GeoHashGridValuesSourceBuilder extends CompositeValuesSourceBuilder<GeoHashGridValuesSourceBuilder> { | ||
static final String TYPE = "geohash_grid"; | ||
|
||
private static final ObjectParser<GeoHashGridValuesSourceBuilder, Void> PARSER; | ||
static { | ||
PARSER = new ObjectParser<>(GeoHashGridValuesSourceBuilder.TYPE); | ||
PARSER.declareInt(GeoHashGridValuesSourceBuilder::precision, new ParseField("precision")); | ||
CompositeValuesSourceParserHelper.declareValuesSourceFields(PARSER, ValueType.NUMERIC); | ||
} | ||
|
||
static GeoHashGridValuesSourceBuilder parse(String name, XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, new GeoHashGridValuesSourceBuilder(name), null); | ||
} | ||
|
||
private int precision = GeoHashGridAggregationBuilder.DEFAULT_PRECISION; | ||
|
||
GeoHashGridValuesSourceBuilder(String name) { | ||
super(name); | ||
} | ||
|
||
GeoHashGridValuesSourceBuilder(StreamInput in) throws IOException { | ||
super(in); | ||
this.precision = in.readInt(); | ||
} | ||
|
||
public GeoHashGridValuesSourceBuilder precision(int precision) { | ||
this.precision = GeoUtils.checkPrecisionRange(precision); | ||
return this; | ||
} | ||
|
||
@Override | ||
protected void innerWriteTo(StreamOutput out) throws IOException { | ||
out.writeInt(precision); | ||
} | ||
|
||
@Override | ||
protected void doXContentBody(XContentBuilder builder, Params params) throws IOException { | ||
builder.field("precision", precision); | ||
} | ||
|
||
@Override | ||
String type() { | ||
return TYPE; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(super.hashCode(), precision); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) return true; | ||
if (obj == null || getClass() != obj.getClass()) return false; | ||
if (super.equals(obj) == false) return false; | ||
GeoHashGridValuesSourceBuilder other = (GeoHashGridValuesSourceBuilder) obj; | ||
return precision == other.precision; | ||
} | ||
|
||
@Override | ||
protected CompositeValuesSourceConfig innerBuild(SearchContext context, ValuesSourceConfig<?> config) throws IOException { | ||
ValuesSource orig = config.toValuesSource(context.getQueryShardContext()); | ||
if (orig == null) { | ||
orig = ValuesSource.GeoPoint.EMPTY; | ||
} | ||
if (orig instanceof ValuesSource.GeoPoint) { | ||
ValuesSource.GeoPoint geoPoint = (ValuesSource.GeoPoint) orig; | ||
// is specified in the builder. | ||
final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; | ||
CellIdSource cellIdSource = new CellIdSource(geoPoint, precision, Geohash::longEncode); | ||
return new CompositeValuesSourceConfig(name, fieldType, cellIdSource, DocValueFormat.GEOHASH, order(), missingBucket()); | ||
} else { | ||
throw new IllegalArgumentException("invalid source, expected numeric, got " + orig.getClass().getSimpleName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: expected geo_point ? |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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.composite; | ||
|
||
import org.apache.lucene.index.LeafReaderContext; | ||
import org.apache.lucene.index.SortedNumericDocValues; | ||
import org.elasticsearch.common.CheckedFunction; | ||
import org.elasticsearch.common.util.BigArrays; | ||
import org.elasticsearch.geometry.Point; | ||
import org.elasticsearch.geometry.utils.Geohash; | ||
import org.elasticsearch.index.mapper.MappedFieldType; | ||
import org.elasticsearch.search.DocValueFormat; | ||
import org.elasticsearch.search.aggregations.bucket.geogrid.CellIdSource; | ||
|
||
import java.io.IOException; | ||
import java.util.function.LongUnaryOperator; | ||
|
||
/** | ||
* A {@link SingleDimensionValuesSource} for geohash values. | ||
* | ||
* Since geohash values can be represented as long values, this class is almost the same as {@link LongValuesSource} | ||
* The main differences is {@link GeohashValuesSource#setAfter(Comparable)} as it needs to accept geohash string values. | ||
*/ | ||
class GeohashValuesSource extends LongValuesSource { | ||
private final int precision; | ||
private final CellIdSource.GeoPointLongEncoder encoder; | ||
GeohashValuesSource(BigArrays bigArrays, | ||
MappedFieldType fieldType, | ||
CheckedFunction<LeafReaderContext, SortedNumericDocValues, IOException> docValuesFunc, | ||
LongUnaryOperator rounding, | ||
DocValueFormat format, | ||
boolean missingBucket, | ||
int size, | ||
int reverseMul, | ||
int precision, | ||
CellIdSource.GeoPointLongEncoder encoder) { | ||
super(bigArrays, fieldType, docValuesFunc, rounding, format, missingBucket, size, reverseMul); | ||
this.precision = precision; | ||
this.encoder = encoder; | ||
} | ||
|
||
@Override | ||
void setAfter(Comparable value) { | ||
if (missingBucket && value == null) { | ||
afterValue = null; | ||
} else if (value instanceof Number) { | ||
afterValue = ((Number) value).longValue(); | ||
} else { | ||
// if it is a string it should be a geohash formatted value. | ||
// We need to preserve the precision between the decoding the geohash and encoding it into a long | ||
Point point = Geohash.toPoint(value.toString()); | ||
afterValue = encoder.encode(point.getLon(), point.getLat(), precision); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was the best way I could figure out how to transform a geohash value into a long value of the appropriate precision. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally this should be implemented in the geohash |
||
} | ||
} | ||
} |
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.
should we throw an IAE if
format(DocValueFormat)
is used since it is ignored in the build ?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.
Since we don't declare a
format
field in thePARSER
we throw an unknown field error and the terms and histogram sources don't do this check.I can add it if that is the prevailing opinion.
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.
That works for rest queries but users of the HLRC can use this builder directly so it would be nice to fail early if they try to set the format ?
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.
Ah, I did not know the HLRC had direct access to this. Will definitely update to prevent setting it.