diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java index f196905d285b0..9227e2f770c1c 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeFieldMapper.java @@ -207,7 +207,7 @@ protected void index(ParseContext context, Geometry geometry) throws IOException if (geometry == null) { return; } - context.doc().addAll(indexer.indexShape(geometry)); + context.doc().addAll(indexer.indexShape(indexer.prepareForIndexing(geometry))); context.addToFieldNames(fieldType().name()); } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeIndexer.java b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeIndexer.java index 5cd29ecc3e865..daba8f0876b4b 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeIndexer.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/GeoShapeIndexer.java @@ -168,11 +168,10 @@ public Geometry visit(Rectangle rectangle) { } public List indexShape(Geometry shape) { - LuceneGeometryIndexer visitor = new LuceneGeometryIndexer(name); - shape = prepareForIndexing(shape); if (shape == null) { return Collections.emptyList(); } + LuceneGeometryIndexer visitor = new LuceneGeometryIndexer(name); shape.visit(visitor); return visitor.fields(); } diff --git a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeScriptDocValuesIT.java b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeScriptDocValuesIT.java index 689b0dc64d16c..30ec035136efd 100644 --- a/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeScriptDocValuesIT.java +++ b/x-pack/plugin/spatial/src/internalClusterTest/java/org/elasticsearch/xpack/spatial/search/GeoShapeScriptDocValuesIT.java @@ -22,6 +22,8 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.Geometry; +import org.elasticsearch.geometry.LinearRing; +import org.elasticsearch.geometry.Polygon; import org.elasticsearch.geometry.utils.WellKnownText; import org.elasticsearch.index.fielddata.ScriptDocValues; import org.elasticsearch.index.mapper.GeoShapeIndexer; @@ -147,6 +149,15 @@ public void testRandomShape() throws Exception { return true; } }, () -> GeometryTestUtils.randomGeometry(false))); + doTestGeometry(geometry); + } + + public void testPolygonDateline() throws Exception { + Geometry geometry = new Polygon(new LinearRing(new double[]{170, 190, 190, 170, 170}, new double[]{-5, -5, 5, 5, -5})); + doTestGeometry(geometry); + } + + private void doTestGeometry(Geometry geometry) throws IOException { client().prepareIndex("test").setId("1") .setSource(jsonBuilder().startObject() .field("name", "TestPosition") diff --git a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java index 69270ad0f5231..cc6e355832c0c 100644 --- a/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java +++ b/x-pack/plugin/spatial/src/main/java/org/elasticsearch/xpack/spatial/index/mapper/GeoShapeWithDocValuesFieldMapper.java @@ -216,6 +216,7 @@ protected void index(ParseContext context, Geometry geometry) throws IOException if (geometry == null) { return; } + geometry = indexer.prepareForIndexing(geometry); List fields = indexer.indexShape(geometry); if (fieldType().isSearchable()) { context.doc().addAll(fields); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java index 0f512e606f24a..f3cecc497f089 100644 --- a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/ingest/CircleProcessorTests.java @@ -226,7 +226,7 @@ public void testGeoShapeQueryAcrossDateline() throws IOException { try (Directory dir = newDirectory(); RandomIndexWriter w = new RandomIndexWriter(random(), dir)) { Document doc = new Document(); GeoShapeIndexer indexer = new GeoShapeIndexer(true, fieldName); - for (IndexableField field : indexer.indexShape(geometry)) { + for (IndexableField field : indexer.indexShape(indexer.prepareForIndexing(geometry))) { doc.add(field); } w.addDocument(doc);