Skip to content

Geo: Do not normalize the longitude with value -180 for Lucene shapes #37299

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
merged 3 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ protected static org.apache.lucene.geo.Polygon polygonLucene(Coordinate[][] poly
holes = new org.apache.lucene.geo.Polygon[polygon.length - 1];
for (int i = 0; i < holes.length; ++i) {
Coordinate[] coords = polygon[i+1];
//We do not have holes on the dateline as they get eliminated
//when breaking the polygon around it.
double[] x = new double[coords.length];
double[] y = new double[coords.length];
for (int c = 0; c < coords.length; ++c) {
Expand All @@ -357,7 +359,9 @@ protected static org.apache.lucene.geo.Polygon polygonLucene(Coordinate[][] poly
double[] x = new double[shell.length];
double[] y = new double[shell.length];
for (int i = 0; i < shell.length; ++i) {
x[i] = normalizeLon(shell[i].x);
//Lucene Tessellator treats different +180 and -180 and we should keep the sign.
//normalizeLon method excludes -180.
x[i] = Math.abs(shell[i].x) > 180 ? normalizeLon(shell[i].x) : shell[i].x;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you clarify why this is done only here and not in toPolygonLucene() and in LineStringBuilder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not an expert in JTS, but I guess the implementation understand that -180/+180 are the same point and it creates the right polygon. Lucene implementation, and in particular the tesselator implementation does not understand that and it won't create the right polygon. It will join the points without crossing the dateline.

I don't think it is a bug in Lucene's implementation but a feature and we need to adapt accordingly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed it with @iverase and he clarified this. Thanks! I think it would be nice to add a javadoc comment here to explain why this is not needed for holes and in toPolygonLucene() to explain why it is not needed there.

y[i] = normalizeLat(shell[i].y);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1126,7 +1126,7 @@ public void testParseGeometryCollection() throws IOException {
),
new org.apache.lucene.geo.Polygon(
new double[] {12.142857142857142d, -12.142857142857142d, -10d, 10d, 12.142857142857142d},
new double[] {180d, 180d, -177d, -177d, 180d}
new double[] {-180d, -180d, -177d, -177d, -180d}
)
};
assertGeometryEquals(luceneExpected, geometryCollectionGeoJson, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.cluster.ClusterState;
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.geo.builders.PointBuilder;
import org.elasticsearch.common.geo.builders.ShapeBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -158,6 +159,88 @@ public void testIndexShapeRouting() throws Exception {
assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
}

public void testIndexPolygonDateLine() throws Exception {
String mappingVector = "{\n" +
" \"properties\": {\n" +
" \"shape\": {\n" +
" \"type\": \"geo_shape\"\n" +
" }\n" +
" }\n" +
" }";

String mappingQuad = "{\n" +
" \"properties\": {\n" +
" \"shape\": {\n" +
" \"type\": \"geo_shape\",\n" +
" \"tree\": \"quadtree\"\n" +
" }\n" +
" }\n" +
" }";


// create index
assertAcked(client().admin().indices().prepareCreate("vector").addMapping("doc", mappingVector, XContentType.JSON).get());
ensureGreen();

assertAcked(client().admin().indices().prepareCreate("quad").addMapping("doc", mappingQuad, XContentType.JSON).get());
ensureGreen();

String source = "{\n" +
" \"shape\" : \"POLYGON((179 0, -179 0, -179 2, 179 2, 179 0))\""+
"}";

indexRandom(true, client().prepareIndex("quad", "doc", "0").setSource(source, XContentType.JSON));
indexRandom(true, client().prepareIndex("vector", "doc", "0").setSource(source, XContentType.JSON));

SearchResponse searchResponse = client().prepareSearch("quad").setQuery(
geoShapeQuery("shape", new PointBuilder(-179.75, 1))
).get();


assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));

searchResponse = client().prepareSearch("quad").setQuery(
geoShapeQuery("shape", new PointBuilder(90, 1))
).get();

assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L));

searchResponse = client().prepareSearch("quad").setQuery(
geoShapeQuery("shape", new PointBuilder(-180, 1))
).get();

assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
searchResponse = client().prepareSearch("quad").setQuery(
geoShapeQuery("shape", new PointBuilder(180, 1))
).get();

assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));

searchResponse = client().prepareSearch("vector").setQuery(
geoShapeQuery("shape", new PointBuilder(90, 1))
).get();

assertThat(searchResponse.getHits().getTotalHits().value, equalTo(0L));

searchResponse = client().prepareSearch("vector").setQuery(
geoShapeQuery("shape", new PointBuilder(-179.75, 1))
).get();

assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));

searchResponse = client().prepareSearch("vector").setQuery(
geoShapeQuery("shape", new PointBuilder(-180, 1))
).get();

assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));

searchResponse = client().prepareSearch("vector").setQuery(
geoShapeQuery("shape", new PointBuilder(180, 1))
).get();

assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
}

private String findNodeName(String index) {
ClusterState state = client().admin().cluster().prepareState().get().getState();
IndexShardRoutingTable shard = state.getRoutingTable().index(index).shard(0);
Expand Down