-
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
Merged
benwtrent
merged 19 commits into
elastic:master
from
benwtrent:feature/add-geohash_grid-to-composite-aggs
Sep 5, 2019
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
96887d7
Support geohash_grid aggregation in composite agg sources
benwtrent bb572f7
adding license headers
benwtrent a8af9fa
Merge branch 'master' into feature/add-geohash_grid-to-composite-aggs
elasticmachine 5a3a0a9
Merge branch 'master' into feature/add-geohash_grid-to-composite-aggs
elasticmachine 6cadfa4
making cellIDsource a public class per PR comments
benwtrent b22d8ca
Merge branch 'feature/add-geohash_grid-to-composite-aggs' of github.c…
benwtrent 82c3b56
Merge branch 'master' into feature/add-geohash_grid-to-composite-aggs
elasticmachine 9b21532
Throwing error on bwc serialization failure, removing unused shardSize
benwtrent 3ba8579
Merge branch 'feature/add-geohash_grid-to-composite-aggs' of github.c…
benwtrent 30dacd4
Merge branch 'master' into feature/add-geohash_grid-to-composite-aggs
elasticmachine ecdcce6
removing bad method call
benwtrent 2e4461d
correcting error message
benwtrent 022e66d
making .format(String) throw IAE
benwtrent ff518c9
move to use geotile instead of hash
benwtrent b3e6f9d
Merge branch 'master' into feature/add-geohash_grid-to-composite-aggs
benwtrent 54c9e12
minor fixups
benwtrent c30cc32
fixing test
benwtrent 2a09dc1
moving DocValueFormat for geotile
benwtrent 5ecd671
moving geotile so that it can be serialized
benwtrent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...rg/elasticsearch/search/aggregations/bucket/composite/GeoTileGridValuesSourceBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* 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.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.index.mapper.MappedFieldType; | ||
import org.elasticsearch.search.DocValueFormat; | ||
import org.elasticsearch.search.aggregations.bucket.geogrid.CellIdSource; | ||
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileGridAggregationBuilder; | ||
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; | ||
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 GeoTileGridValuesSourceBuilder extends CompositeValuesSourceBuilder<GeoTileGridValuesSourceBuilder> { | ||
static final String TYPE = "geotile_grid"; | ||
|
||
private static final ObjectParser<GeoTileGridValuesSourceBuilder, Void> PARSER; | ||
static { | ||
PARSER = new ObjectParser<>(GeoTileGridValuesSourceBuilder.TYPE); | ||
PARSER.declareInt(GeoTileGridValuesSourceBuilder::precision, new ParseField("precision")); | ||
CompositeValuesSourceParserHelper.declareValuesSourceFields(PARSER, ValueType.NUMERIC); | ||
} | ||
|
||
static GeoTileGridValuesSourceBuilder parse(String name, XContentParser parser) throws IOException { | ||
return PARSER.parse(parser, new GeoTileGridValuesSourceBuilder(name), null); | ||
} | ||
|
||
private int precision = GeoTileGridAggregationBuilder.DEFAULT_PRECISION; | ||
|
||
GeoTileGridValuesSourceBuilder(String name) { | ||
super(name); | ||
} | ||
|
||
GeoTileGridValuesSourceBuilder(StreamInput in) throws IOException { | ||
super(in); | ||
this.precision = in.readInt(); | ||
} | ||
|
||
public GeoTileGridValuesSourceBuilder precision(int precision) { | ||
this.precision = GeoTileUtils.checkPrecisionRange(precision); | ||
return this; | ||
} | ||
|
||
@Override | ||
public GeoTileGridValuesSourceBuilder format(String format) { | ||
throw new IllegalArgumentException("[format] is not supported for [" + TYPE + "]"); | ||
} | ||
|
||
@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; | ||
GeoTileGridValuesSourceBuilder other = (GeoTileGridValuesSourceBuilder) 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, GeoTileUtils::longEncode); | ||
return new CompositeValuesSourceConfig(name, fieldType, cellIdSource, GEOTILE, order(), missingBucket()); | ||
} else { | ||
throw new IllegalArgumentException("invalid source, expected geo_point, got " + orig.getClass().getSimpleName()); | ||
} | ||
} | ||
|
||
DocValueFormat GEOTILE = new DocValueFormat() { | ||
|
||
@Override | ||
public String getWriteableName() { | ||
return "geo_tile"; | ||
} | ||
|
||
@Override | ||
public void writeTo(StreamOutput out) { | ||
} | ||
|
||
@Override | ||
public String format(long value) { | ||
return GeoTileUtils.stringEncode(value); | ||
} | ||
|
||
@Override | ||
public String format(double value) { | ||
return format((long) value); | ||
} | ||
}; | ||
|
||
} |
61 changes: 61 additions & 0 deletions
61
...main/java/org/elasticsearch/search/aggregations/bucket/composite/GeoTileValuesSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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.index.mapper.MappedFieldType; | ||
import org.elasticsearch.search.DocValueFormat; | ||
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils; | ||
|
||
import java.io.IOException; | ||
import java.util.function.LongUnaryOperator; | ||
|
||
/** | ||
* A {@link SingleDimensionValuesSource} for geotile values. | ||
* | ||
* Since geotile values can be represented as long values, this class is almost the same as {@link LongValuesSource} | ||
* The main differences is {@link GeoTileValuesSource#setAfter(Comparable)} as it needs to accept geotile string values i.e. "zoom/x/y". | ||
*/ | ||
class GeoTileValuesSource extends LongValuesSource { | ||
GeoTileValuesSource(BigArrays bigArrays, | ||
MappedFieldType fieldType, | ||
CheckedFunction<LeafReaderContext, SortedNumericDocValues, IOException> docValuesFunc, | ||
LongUnaryOperator rounding, | ||
DocValueFormat format, | ||
boolean missingBucket, | ||
int size, | ||
int reverseMul) { | ||
super(bigArrays, fieldType, docValuesFunc, rounding, format, missingBucket, size, reverseMul); | ||
} | ||
|
||
@Override | ||
void setAfter(Comparable value) { | ||
if (missingBucket && value == null) { | ||
afterValue = null; | ||
} else if (value instanceof Number) { | ||
afterValue = ((Number) value).longValue(); | ||
} else { | ||
afterValue = GeoTileUtils.longEncode(value.toString()); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 this is the only place it is used, I put it here. But, we may want to support something like this elsewhere? I am not sure.
If so, I will happily move it to the docvalue format file. Also, I used the name
geo_tile
as a parallel togeo_hash
but I am not sure if that is the correct name for it.@talevy let me know what you think.
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 right. I think that it is fine to place here for the reasons you stated. Would this be nicer to have closer to the top of the file and set to private+static?
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.
@talevy updated :)
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.
@talevy the
geo_tile
needs to be writable/readable via streams. So, I think moving it to theDocValueFormat
file may be best. I did such. Let me know what you think.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.
the move makes sense given what Zach mentioned below!