Skip to content

Commit

Permalink
[GeoPoint] Grid aggregations with bounds should exclude touching tiles (
Browse files Browse the repository at this point in the history
  • Loading branch information
iverase authored Apr 30, 2021
1 parent e85889a commit 793166f
Show file tree
Hide file tree
Showing 15 changed files with 375 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ var bbox = geohash.decode_bbox('u17');
==== Requests with additional bounding box filtering

The `geohash_grid` aggregation supports an optional `bounds` parameter
that restricts the points considered to those that fall within the
that restricts the cells considered to those that intersects the
bounds provided. The `bounds` parameter accepts the bounding box in
all the same <<query-dsl-geo-bounding-box-query-accepted-formats,accepted formats>> of the
bounds specified in the Geo Bounding Box Query. This bounding box can be used with or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ POST /museums/_search?size=0
==== Requests with additional bounding box filtering

The `geotile_grid` aggregation supports an optional `bounds` parameter
that restricts the points considered to those that fall within the
that restricts the cells considered to those that intersects the
bounds provided. The `bounds` parameter accepts the bounding box in
all the same <<query-dsl-geo-bounding-box-query-accepted-formats,accepted formats>> of the
bounds specified in the Geo Bounding Box Query. This bounding box can be used with or
without an additional `geo_bounding_box` query filtering the points prior to aggregating.
without an additional `geo_bounding_box` query for filtering the points prior to aggregating.
It is an independent bounding box that can intersect with, be equal to, or be disjoint
to any additional `geo_bounding_box` queries defined in the context of the aggregation.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
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.GeoTileCellIdSource;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileGridAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoTileUtils;
import org.elasticsearch.search.aggregations.support.CoreValuesSourceType;
Expand Down Expand Up @@ -78,11 +78,10 @@ static void register(ValuesSourceRegistry.Builder builder) {
ValuesSource.GeoPoint geoPoint = (ValuesSource.GeoPoint) valuesSourceConfig.getValuesSource();
// is specified in the builder.
final MappedFieldType fieldType = valuesSourceConfig.fieldType();
CellIdSource cellIdSource = new CellIdSource(
GeoTileCellIdSource cellIdSource = new GeoTileCellIdSource(
geoPoint,
precision,
boundingBox,
GeoTileUtils::longEncode
boundingBox
);
return new CompositeValuesSourceConfig(
name,
Expand All @@ -100,7 +99,7 @@ static void register(ValuesSourceRegistry.Builder builder) {
CompositeValuesSourceConfig compositeValuesSourceConfig

) -> {
final CellIdSource cis = (CellIdSource) compositeValuesSourceConfig.valuesSource();
final ValuesSource.Numeric cis = (ValuesSource.Numeric) compositeValuesSourceConfig.valuesSource();
return new GeoTileValuesSource(
bigArrays,
compositeValuesSourceConfig.fieldType(),
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
abstract class CellValues extends AbstractSortingNumericDocValues {
private MultiGeoPointValues geoValues;
protected int precision;
protected CellIdSource.GeoPointLongEncoder encoder;

protected CellValues(MultiGeoPointValues geoValues, int precision, CellIdSource.GeoPointLongEncoder encoder) {
protected CellValues(MultiGeoPointValues geoValues, int precision) {
this.geoValues = geoValues;
this.precision = precision;
this.encoder = encoder;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.search.aggregations.bucket.geogrid;

import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.SortedNumericDocValues;
import org.elasticsearch.common.geo.GeoBoundingBox;
import org.elasticsearch.geometry.Rectangle;
import org.elasticsearch.geometry.utils.Geohash;
import org.elasticsearch.index.fielddata.MultiGeoPointValues;
import org.elasticsearch.index.fielddata.SortedBinaryDocValues;
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
import org.elasticsearch.search.aggregations.support.ValuesSource;

/**
* Class to help convert {@link MultiGeoPointValues}
* to GeoHash bucketing.
*/
public class GeoHashCellIdSource extends ValuesSource.Numeric {
private final GeoPoint valuesSource;
private final int precision;
private final GeoBoundingBox geoBoundingBox;

public GeoHashCellIdSource(GeoPoint valuesSource, int precision, GeoBoundingBox geoBoundingBox) {
this.valuesSource = valuesSource;
this.precision = precision;
this.geoBoundingBox = geoBoundingBox;
}

public int precision() {
return precision;
}

@Override
public boolean isFloatingPoint() {
return false;
}

@Override
public SortedNumericDocValues longValues(LeafReaderContext ctx) {
return geoBoundingBox.isUnbounded() ?
new UnboundedCellValues(valuesSource.geoPointValues(ctx), precision) :
new BoundedCellValues(valuesSource.geoPointValues(ctx), precision, geoBoundingBox);
}

@Override
public SortedNumericDoubleValues doubleValues(LeafReaderContext ctx) {
throw new UnsupportedOperationException();
}

@Override
public SortedBinaryDocValues bytesValues(LeafReaderContext ctx) {
throw new UnsupportedOperationException();
}

private static class UnboundedCellValues extends CellValues {

UnboundedCellValues(MultiGeoPointValues geoValues, int precision) {
super(geoValues, precision);
}

@Override
int advanceValue(org.elasticsearch.common.geo.GeoPoint target, int valuesIdx) {
values[valuesIdx] = Geohash.longEncode(target.getLon(), target.getLat(), precision);
return valuesIdx + 1;
}
}

private static class BoundedCellValues extends CellValues {

private final GeoBoundingBox bbox;
private final boolean crossesDateline;

BoundedCellValues(MultiGeoPointValues geoValues, int precision, GeoBoundingBox bbox) {
super(geoValues, precision);
this.bbox = bbox;
this.crossesDateline = bbox.right() < bbox.left();
}

@Override
int advanceValue(org.elasticsearch.common.geo.GeoPoint target, int valuesIdx) {
final String hash = Geohash.stringEncode(target.getLon(), target.getLat(), precision);
if (validHash(hash)) {
values[valuesIdx] = Geohash.longEncode(hash);
return valuesIdx + 1;
}
return valuesIdx;
}

private boolean validHash(String hash) {
final Rectangle rectangle = Geohash.toBoundingBox(hash);
// touching hashes are excluded
if (bbox.top() > rectangle.getMinY() && bbox.bottom() < rectangle.getMaxY()) {
if (crossesDateline) {
return bbox.left() < rectangle.getMaxX() || bbox.right() > rectangle.getMinX();
} else {
return bbox.left() < rectangle.getMaxX() && bbox.right() > rectangle.getMinX();
}
}
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
package org.elasticsearch.search.aggregations.bucket.geogrid;

import org.elasticsearch.common.geo.GeoBoundingBox;
import org.elasticsearch.geometry.utils.Geohash;
import org.elasticsearch.search.aggregations.Aggregator;
import org.elasticsearch.search.aggregations.AggregatorFactories;
import org.elasticsearch.search.aggregations.AggregatorFactory;
Expand Down Expand Up @@ -98,11 +97,10 @@ static void registerAggregators(ValuesSourceRegistry.Builder builder) {
parent,
cardinality,
metadata) -> {
CellIdSource cellIdSource = new CellIdSource(
GeoHashCellIdSource cellIdSource = new GeoHashCellIdSource(
(ValuesSource.GeoPoint) valuesSource,
precision,
geoBoundingBox,
Geohash::longEncode
geoBoundingBox
);
return new GeoHashGridAggregator(
name,
Expand All @@ -115,7 +113,6 @@ static void registerAggregators(ValuesSourceRegistry.Builder builder) {
cardinality,
metadata
);
},
true);
}, true);
}
}
Loading

0 comments on commit 793166f

Please sign in to comment.