Skip to content
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

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

Merged
merged 3 commits into from Jan 11, 2019
Merged
Changes from 2 commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -357,7 +357,7 @@ protected static Polygon polygonS4J(GeometryFactory factory, 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);
x[i] = Math.abs(shell[i].x) > 180 ? normalizeLon(shell[i].x) : shell[i].x;

This comment has been minimized.

Copy link
@imotov

imotov Jan 10, 2019

Member

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

This comment has been minimized.

Copy link
@iverase

iverase Jan 10, 2019

Author Contributor

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.

This comment has been minimized.

Copy link
@imotov

imotov Jan 10, 2019

Member

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);
}

@@ -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);
@@ -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;
@@ -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);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.