diff --git a/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPolygonShapeQueries.java b/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPolygonShapeQueries.java index 55dc57c5f1f..f640591cf3e 100644 --- a/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPolygonShapeQueries.java +++ b/lucene/core/src/test/org/apache/lucene/document/TestLatLonMultiPolygonShapeQueries.java @@ -36,10 +36,40 @@ public class TestLatLonMultiPolygonShapeQueries extends BaseLatLonShapeTestCase int n = random().nextInt(4) + 1; Polygon[] polygons = new Polygon[n]; for (int i =0; i < n; i++) { - polygons[i] = (Polygon) getShapeType().nextShape(); + int repetitions = 0; + while (true) { + Polygon p = (Polygon) getShapeType().nextShape(); + // polygons are disjoint so CONTAINS works. Note that if we intersect + // any shape then contains return false. + if (isDisjoint(polygons, p)) { + polygons[i] = p; + break; + } + repetitions++; + if (repetitions > 50) { + // try again + return nextShape(); + } + } } return polygons; } + + private boolean isDisjoint(Polygon[] polygons, Polygon check) { + // we use bounding boxes so we do not get intersecting polygons. + for (Polygon polygon : polygons) { + if (polygon != null) { + if (getEncoder().quantizeY(polygon.minLat) > getEncoder().quantizeY(check.maxLat) + || getEncoder().quantizeY(polygon.maxLat) < getEncoder().quantizeY(check.minLat) + || getEncoder().quantizeX(polygon.minLon) > getEncoder().quantizeX(check.maxLon) + || getEncoder().quantizeX(polygon.maxLon) < getEncoder().quantizeX(check.minLon)) { + continue; + } + return false; + } + } + return true; + } @Override protected Field[] createIndexableFields(String name, Object o) {