From bd5e16d65c7e982eccdb52392a695ae92eb41b02 Mon Sep 17 00:00:00 2001 From: Ignacio Vera Date: Fri, 22 Jul 2022 08:43:29 +0200 Subject: [PATCH] move geo_shape query builder tests to spatial module (#88588) --- ...oBoundingBoxQueryBuilderGeoPointTests.java | 78 ++++++++++++ .../GeoDistanceQueryBuilderGeoPointTests.java | 48 +++++++ .../GeoShapeQueryBuilderGeoPointTests.java | 13 +- .../GeoShapeQueryBuilderGeoShapeTests.java | 100 --------------- .../GeoBoundingBoxQueryBuilderTestCase.java | 118 +++--------------- .../geo/GeoDistanceQueryBuilderTestCase.java | 64 ++-------- .../geo/GeoShapeQueryBuilderTestCase.java | 28 +++-- ...oBoundingBoxQueryBuilderGeoShapeTests.java | 65 ++++++++++ .../GeoDistanceQueryBuilderGeoShapeTests.java | 66 ++++++++++ .../GeoShapeQueryBuilderGeoShapeTests.java | 118 ++++++++++++++++++ 10 files changed, 429 insertions(+), 269 deletions(-) create mode 100644 server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderGeoPointTests.java create mode 100644 server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderGeoPointTests.java delete mode 100644 server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoShapeTests.java rename server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java => test/framework/src/main/java/org/elasticsearch/search/geo/GeoBoundingBoxQueryBuilderTestCase.java (78%) rename server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java => test/framework/src/main/java/org/elasticsearch/search/geo/GeoDistanceQueryBuilderTestCase.java (81%) rename server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java => test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryBuilderTestCase.java (90%) create mode 100644 x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoBoundingBoxQueryBuilderGeoShapeTests.java create mode 100644 x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoDistanceQueryBuilderGeoShapeTests.java create mode 100644 x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeQueryBuilderGeoShapeTests.java diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderGeoPointTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderGeoPointTests.java new file mode 100644 index 0000000000000..aecd630b3d9ee --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderGeoPointTests.java @@ -0,0 +1,78 @@ +/* + * 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.index.query; + +import org.apache.lucene.document.LatLonDocValuesField; +import org.apache.lucene.document.LatLonPoint; +import org.apache.lucene.geo.GeoEncodingUtils; +import org.apache.lucene.search.IndexOrDocValuesQuery; +import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.Query; +import org.elasticsearch.index.mapper.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.search.geo.GeoBoundingBoxQueryBuilderTestCase; + +import java.io.IOException; + +@SuppressWarnings("checkstyle:MissingJavadocMethod") +public class GeoBoundingBoxQueryBuilderGeoPointTests extends GeoBoundingBoxQueryBuilderTestCase { + + @Override + protected String getFieldName() { + return randomFrom(GEO_POINT_FIELD_NAME, GEO_POINT_ALIAS_FIELD_NAME); + } + + @Override + protected void doAssertLuceneQuery(GeoBoundingBoxQueryBuilder queryBuilder, Query query, SearchExecutionContext context) + throws IOException { + final MappedFieldType fieldType = context.getFieldType(queryBuilder.fieldName()); + if (fieldType == null) { + assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery); + } + assertEquals(GeoPointFieldMapper.GeoPointFieldType.class, fieldType.getClass()); + assertEquals(IndexOrDocValuesQuery.class, query.getClass()); + Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery(); + String expectedFieldName = expectedFieldName(queryBuilder.fieldName()); + double qMinLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(queryBuilder.bottomRight().lat())); + double qMaxLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(queryBuilder.topLeft().lat())); + double qMinLon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(queryBuilder.topLeft().lon())); + double qMaxLon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(queryBuilder.bottomRight().lon())); + assertEquals(LatLonPoint.newBoxQuery(expectedFieldName, qMinLat, qMaxLat, qMinLon, qMaxLon), indexQuery); + Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery(); + assertEquals(LatLonDocValuesField.newSlowBoxQuery(expectedFieldName, qMinLat, qMaxLat, qMinLon, qMaxLon), dvQuery); + } + + public void testValidation() { + PointTester[] testers = { new TopTester(), new LeftTester(), new BottomTester(), new RightTester() }; + + for (PointTester tester : testers) { + GeoBoundingBoxQueryBuilder builder = createTestQueryBuilder(); + tester.invalidateCoordinate(builder.setValidationMethod(GeoValidationMethod.COERCE), false); + QueryValidationException except = builder.checkLatLon(); + assertNull( + "validation w/ coerce should ignore invalid " + + tester.getClass().getName() + + " coordinate: " + + tester.invalidCoordinate + + " ", + except + ); + + tester.invalidateCoordinate(builder.setValidationMethod(GeoValidationMethod.STRICT), false); + except = builder.checkLatLon(); + assertNotNull( + "validation w/o coerce should detect invalid coordinate: " + + tester.getClass().getName() + + " coordinate: " + + tester.invalidCoordinate, + except + ); + } + } +} diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderGeoPointTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderGeoPointTests.java new file mode 100644 index 0000000000000..89b2d3ab853c2 --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderGeoPointTests.java @@ -0,0 +1,48 @@ +/* + * 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.index.query; + +import org.apache.lucene.document.LatLonDocValuesField; +import org.apache.lucene.document.LatLonPoint; +import org.apache.lucene.geo.GeoEncodingUtils; +import org.apache.lucene.search.IndexOrDocValuesQuery; +import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.Query; +import org.elasticsearch.index.mapper.GeoPointFieldMapper; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.search.geo.GeoDistanceQueryBuilderTestCase; + +import java.io.IOException; + +@SuppressWarnings("checkstyle:MissingJavadocMethod") +public class GeoDistanceQueryBuilderGeoPointTests extends GeoDistanceQueryBuilderTestCase { + + @Override + protected String getFieldName() { + return randomFrom(GEO_POINT_FIELD_NAME, GEO_POINT_ALIAS_FIELD_NAME); + } + + @Override + protected void doAssertLuceneQuery(GeoDistanceQueryBuilder queryBuilder, Query query, SearchExecutionContext context) + throws IOException { + final MappedFieldType fieldType = context.getFieldType(queryBuilder.fieldName()); + if (fieldType == null) { + assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery); + } + assertEquals(GeoPointFieldMapper.GeoPointFieldType.class, fieldType.getClass()); + assertEquals(IndexOrDocValuesQuery.class, query.getClass()); + Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery(); + String expectedFieldName = expectedFieldName(queryBuilder.fieldName()); + double qLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(queryBuilder.point().lat())); + double qLon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(queryBuilder.point().lon())); + assertEquals(LatLonPoint.newDistanceQuery(expectedFieldName, qLat, qLon, queryBuilder.distance()), indexQuery); + Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery(); + assertEquals(LatLonDocValuesField.newSlowDistanceQuery(expectedFieldName, qLat, qLon, queryBuilder.distance()), dvQuery); + } +} diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoPointTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoPointTests.java index 373e5cd619a49..cc220438fb448 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoPointTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoPointTests.java @@ -10,23 +10,25 @@ import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.Geometry; +import org.elasticsearch.search.geo.GeoShapeQueryBuilderTestCase; -public class GeoShapeQueryBuilderGeoPointTests extends GeoShapeQueryBuilderTests { +public class GeoShapeQueryBuilderGeoPointTests extends GeoShapeQueryBuilderTestCase { - protected String fieldName() { - return GEO_POINT_FIELD_NAME; + protected String getFieldName() { + return randomFrom(GEO_POINT_FIELD_NAME, GEO_POINT_ALIAS_FIELD_NAME); } + @Override protected GeoShapeQueryBuilder doCreateTestQueryBuilder(boolean indexedShape) { Geometry geometry = GeometryTestUtils.randomPolygon(false); GeoShapeQueryBuilder builder; clearShapeFields(); if (indexedShape == false) { - builder = new GeoShapeQueryBuilder(fieldName(), geometry); + builder = new GeoShapeQueryBuilder(getFieldName(), geometry); } else { indexedShapeToReturn = geometry; indexedShapeId = randomAlphaOfLengthBetween(3, 20); - builder = new GeoShapeQueryBuilder(fieldName(), indexedShapeId); + builder = new GeoShapeQueryBuilder(getFieldName(), indexedShapeId); if (randomBoolean()) { indexedShapeIndex = randomAlphaOfLengthBetween(3, 20); builder.indexedShapeIndex(indexedShapeIndex); @@ -49,5 +51,4 @@ protected GeoShapeQueryBuilder doCreateTestQueryBuilder(boolean indexedShape) { } return builder; } - } diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoShapeTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoShapeTests.java deleted file mode 100644 index f359f4600faaf..0000000000000 --- a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderGeoShapeTests.java +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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.index.query; - -import org.elasticsearch.Version; -import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.compress.CompressedXContent; -import org.elasticsearch.common.geo.ShapeRelation; -import org.elasticsearch.geo.GeometryTestUtils; -import org.elasticsearch.geometry.Geometry; -import org.elasticsearch.geometry.ShapeType; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; -import org.elasticsearch.xcontent.XContentBuilder; - -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; - -public class GeoShapeQueryBuilderGeoShapeTests extends GeoShapeQueryBuilderTests { - - private static final String GEO_SHAPE_FIELD_NAME = "mapped_geo_shape"; - - @Override - protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - XContentBuilder builder = PutMappingRequest.simpleMapping(GEO_SHAPE_FIELD_NAME, "type=geo_shape"); - mapperService.merge("_doc", new CompressedXContent(Strings.toString(builder)), MapperService.MergeReason.MAPPING_UPDATE); - } - - @SuppressWarnings("deprecation") // dependencies in server for geo_shape field should be decoupled - protected Collection> getPlugins() { - return Collections.singletonList(TestGeoShapeFieldMapperPlugin.class); - } - - protected String fieldName() { - return GEO_SHAPE_FIELD_NAME; - } - - protected GeoShapeQueryBuilder doCreateTestQueryBuilder(boolean indexedShape) { - ShapeType shapeType = randomFrom( - ShapeType.POINT, - ShapeType.MULTIPOINT, - ShapeType.LINESTRING, - ShapeType.MULTILINESTRING, - ShapeType.POLYGON - ); - Geometry geometry = GeometryTestUtils.randomGeometry(shapeType, false); - GeoShapeQueryBuilder builder; - clearShapeFields(); - if (indexedShape == false) { - builder = new GeoShapeQueryBuilder(fieldName(), geometry); - } else { - indexedShapeToReturn = geometry; - indexedShapeId = randomAlphaOfLengthBetween(3, 20); - builder = new GeoShapeQueryBuilder(fieldName(), indexedShapeId); - if (randomBoolean()) { - indexedShapeIndex = randomAlphaOfLengthBetween(3, 20); - builder.indexedShapeIndex(indexedShapeIndex); - } - if (randomBoolean()) { - indexedShapePath = randomAlphaOfLengthBetween(3, 20); - builder.indexedShapePath(indexedShapePath); - } - if (randomBoolean()) { - indexedShapeRouting = randomAlphaOfLengthBetween(3, 20); - builder.indexedShapeRouting(indexedShapeRouting); - } - } - if (randomBoolean()) { - SearchExecutionContext context = createSearchExecutionContext(); - if (context.indexVersionCreated().onOrAfter(Version.V_7_5_0)) { // CONTAINS is only supported from version 7.5 - if (shapeType == ShapeType.LINESTRING || shapeType == ShapeType.MULTILINESTRING) { - builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.CONTAINS)); - } else { - builder.relation( - randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.WITHIN, ShapeRelation.CONTAINS) - ); - } - } else { - if (shapeType == ShapeType.LINESTRING || shapeType == ShapeType.MULTILINESTRING) { - builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS)); - } else { - builder.relation(randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.WITHIN)); - } - } - } - - if (randomBoolean()) { - builder.ignoreUnmapped(randomBoolean()); - } - return builder; - } -} diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoBoundingBoxQueryBuilderTestCase.java similarity index 78% rename from server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java rename to test/framework/src/main/java/org/elasticsearch/search/geo/GeoBoundingBoxQueryBuilderTestCase.java index c0875b88207da..b5de27adbf677 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java +++ b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoBoundingBoxQueryBuilderTestCase.java @@ -6,66 +6,38 @@ * Side Public License, v 1. */ -package org.elasticsearch.index.query; +package org.elasticsearch.search.geo; -import org.apache.lucene.document.LatLonDocValuesField; -import org.apache.lucene.document.LatLonPoint; -import org.apache.lucene.geo.GeoEncodingUtils; -import org.apache.lucene.search.IndexOrDocValuesQuery; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.ElasticsearchParseException; -import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.geo.GeoUtils; import org.elasticsearch.geo.GeometryTestUtils; import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.geometry.utils.Geohash; -import org.elasticsearch.index.mapper.GeoPointFieldMapper; -import org.elasticsearch.index.mapper.GeoShapeFieldMapper; -import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.index.query.GeoBoundingBoxQueryBuilder; +import org.elasticsearch.index.query.GeoValidationMethod; +import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.test.AbstractQueryTestCase; -import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; -import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; -import java.util.Collection; -import java.util.Collections; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; @SuppressWarnings("checkstyle:MissingJavadocMethod") -public class GeoBoundingBoxQueryBuilderTests extends AbstractQueryTestCase { +public abstract class GeoBoundingBoxQueryBuilderTestCase extends AbstractQueryTestCase { /** Randomly generate either NaN or one of the two infinity values. */ private static final Double[] brokenDoubles = { Double.NaN, Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY }; - private static final String GEO_SHAPE_FIELD_NAME = "mapped_geo_shape"; - protected static final String GEO_SHAPE_ALIAS_FIELD_NAME = "mapped_geo_shape_alias"; - @Override - protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - final XContentBuilder builder = PutMappingRequest.simpleMapping( - GEO_SHAPE_FIELD_NAME, - "type=geo_shape", - GEO_SHAPE_ALIAS_FIELD_NAME, - "type=alias,path=" + GEO_SHAPE_FIELD_NAME - ); - mapperService.merge("_doc", new CompressedXContent(Strings.toString(builder)), MapperService.MergeReason.MAPPING_UPDATE); - } - - @SuppressWarnings("deprecation") // dependencies in server for geo_shape field should be decoupled - protected Collection> getPlugins() { - return Collections.singletonList(TestGeoShapeFieldMapperPlugin.class); - } + protected abstract String getFieldName(); @Override protected GeoBoundingBoxQueryBuilder doCreateTestQueryBuilder() { - String fieldName = randomFrom(GEO_POINT_FIELD_NAME, GEO_POINT_ALIAS_FIELD_NAME, GEO_SHAPE_FIELD_NAME, GEO_SHAPE_ALIAS_FIELD_NAME); + String fieldName = getFieldName(); GeoBoundingBoxQueryBuilder builder = new GeoBoundingBoxQueryBuilder(fieldName); // make sure that minX != maxX and minY != maxY after geohash encoding Rectangle box = randomValueOtherThanMany( @@ -141,34 +113,6 @@ public void testBrokenCoordinateCanBeSetWithIgnoreMalformed() { } } - public void testValidation() { - PointTester[] testers = { new TopTester(), new LeftTester(), new BottomTester(), new RightTester() }; - - for (PointTester tester : testers) { - GeoBoundingBoxQueryBuilder builder = createTestQueryBuilder(); - tester.invalidateCoordinate(builder.setValidationMethod(GeoValidationMethod.COERCE), false); - QueryValidationException except = builder.checkLatLon(); - assertNull( - "validation w/ coerce should ignore invalid " - + tester.getClass().getName() - + " coordinate: " - + tester.invalidCoordinate - + " ", - except - ); - - tester.invalidateCoordinate(builder.setValidationMethod(GeoValidationMethod.STRICT), false); - except = builder.checkLatLon(); - assertNotNull( - "validation w/o coerce should detect invalid coordinate: " - + tester.getClass().getName() - + " coordinate: " - + tester.invalidCoordinate, - except - ); - } - } - public void testTopBottomCannotBeFlipped() { GeoBoundingBoxQueryBuilder builder = createTestQueryBuilder(); double top = builder.topLeft().getLat(); @@ -241,37 +185,15 @@ public void testStrictnessDefault() { ); } - @Override - protected void doAssertLuceneQuery(GeoBoundingBoxQueryBuilder queryBuilder, Query query, SearchExecutionContext context) - throws IOException { - final MappedFieldType fieldType = context.getFieldType(queryBuilder.fieldName()); - if (fieldType == null) { - assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery); - } else if (fieldType instanceof GeoPointFieldMapper.GeoPointFieldType) { - assertEquals(IndexOrDocValuesQuery.class, query.getClass()); - Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery(); - String expectedFieldName = expectedFieldName(queryBuilder.fieldName()); - double qMinLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(queryBuilder.bottomRight().lat())); - double qMaxLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(queryBuilder.topLeft().lat())); - double qMinLon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(queryBuilder.topLeft().lon())); - double qMaxLon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(queryBuilder.bottomRight().lon())); - assertEquals(LatLonPoint.newBoxQuery(expectedFieldName, qMinLat, qMaxLat, qMinLon, qMaxLon), indexQuery); - Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery(); - assertEquals(LatLonDocValuesField.newSlowBoxQuery(expectedFieldName, qMinLat, qMaxLat, qMinLon, qMaxLon), dvQuery); - } else { - assertEquals(GeoShapeFieldMapper.GeoShapeFieldType.class, fieldType.getClass()); - } - } - - private abstract static class PointTester { + protected abstract static class PointTester { private final double brokenCoordinate = randomFrom(brokenDoubles); - private final double invalidCoordinate; + public final double invalidCoordinate; - private PointTester(double invalidCoodinate) { + protected PointTester(double invalidCoodinate) { this.invalidCoordinate = invalidCoodinate; } - private void invalidateCoordinate(GeoBoundingBoxQueryBuilder qb, boolean useBrokenDouble) { + public void invalidateCoordinate(GeoBoundingBoxQueryBuilder qb, boolean useBrokenDouble) { if (useBrokenDouble) { fillIn(brokenCoordinate, qb); } else { @@ -282,8 +204,8 @@ private void invalidateCoordinate(GeoBoundingBoxQueryBuilder qb, boolean useBrok protected abstract void fillIn(double fillIn, GeoBoundingBoxQueryBuilder qb); } - private static class TopTester extends PointTester { - private TopTester() { + protected static class TopTester extends PointTester { + public TopTester() { super(randomDoubleBetween(GeoUtils.MAX_LAT, Double.MAX_VALUE, false)); } @@ -293,8 +215,8 @@ public void fillIn(double coordinate, GeoBoundingBoxQueryBuilder qb) { } } - private static class LeftTester extends PointTester { - private LeftTester() { + protected static class LeftTester extends PointTester { + public LeftTester() { super(randomDoubleBetween(-Double.MAX_VALUE, GeoUtils.MIN_LON, true)); } @@ -304,8 +226,8 @@ public void fillIn(double coordinate, GeoBoundingBoxQueryBuilder qb) { } } - private static class BottomTester extends PointTester { - private BottomTester() { + protected static class BottomTester extends PointTester { + public BottomTester() { super(randomDoubleBetween(-Double.MAX_VALUE, GeoUtils.MIN_LAT, false)); } @@ -315,8 +237,8 @@ public void fillIn(double coordinate, GeoBoundingBoxQueryBuilder qb) { } } - private static class RightTester extends PointTester { - private RightTester() { + protected static class RightTester extends PointTester { + public RightTester() { super(randomDoubleBetween(GeoUtils.MAX_LON, Double.MAX_VALUE, true)); } diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoDistanceQueryBuilderTestCase.java similarity index 81% rename from server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java rename to test/framework/src/main/java/org/elasticsearch/search/geo/GeoDistanceQueryBuilderTestCase.java index a564b04122537..5293d88e86121 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java +++ b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoDistanceQueryBuilderTestCase.java @@ -6,64 +6,36 @@ * Side Public License, v 1. */ -package org.elasticsearch.index.query; +package org.elasticsearch.search.geo; -import org.apache.lucene.document.LatLonDocValuesField; -import org.apache.lucene.document.LatLonPoint; import org.apache.lucene.geo.GeoEncodingUtils; -import org.apache.lucene.search.IndexOrDocValuesQuery; import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; -import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.common.ParsingException; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.geo.GeoDistance; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.geo.GeometryTestUtils; -import org.elasticsearch.index.mapper.GeoPointFieldMapper; -import org.elasticsearch.index.mapper.GeoShapeFieldMapper; -import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; -import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.index.query.GeoDistanceQueryBuilder; +import org.elasticsearch.index.query.GeoValidationMethod; +import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.test.AbstractQueryTestCase; -import org.elasticsearch.test.TestGeoShapeFieldMapperPlugin; -import org.elasticsearch.xcontent.XContentBuilder; import java.io.IOException; -import java.util.Collection; -import java.util.Collections; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; @SuppressWarnings("checkstyle:MissingJavadocMethod") -public class GeoDistanceQueryBuilderTests extends AbstractQueryTestCase { +public abstract class GeoDistanceQueryBuilderTestCase extends AbstractQueryTestCase { - private static final String GEO_SHAPE_FIELD_NAME = "mapped_geo_shape"; - protected static final String GEO_SHAPE_ALIAS_FIELD_NAME = "mapped_geo_shape_alias"; - - @Override - protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - final XContentBuilder builder = PutMappingRequest.simpleMapping( - GEO_SHAPE_FIELD_NAME, - "type=geo_shape", - GEO_SHAPE_ALIAS_FIELD_NAME, - "type=alias,path=" + GEO_SHAPE_FIELD_NAME - ); - mapperService.merge("_doc", new CompressedXContent(Strings.toString(builder)), MapperService.MergeReason.MAPPING_UPDATE); - } - - @SuppressWarnings("deprecation") // dependencies in server for geo_shape field should be decoupled - protected Collection> getPlugins() { - return Collections.singletonList(TestGeoShapeFieldMapperPlugin.class); - } + protected abstract String getFieldName(); @Override protected GeoDistanceQueryBuilder doCreateTestQueryBuilder() { - String fieldName = randomFrom(GEO_POINT_FIELD_NAME, GEO_POINT_ALIAS_FIELD_NAME, GEO_SHAPE_FIELD_NAME, GEO_SHAPE_ALIAS_FIELD_NAME); + String fieldName = getFieldName(); GeoDistanceQueryBuilder qb = new GeoDistanceQueryBuilder(fieldName); String distance = "" + randomDouble(); if (randomBoolean()) { @@ -140,26 +112,6 @@ public void testToQuery() throws IOException { super.testToQuery(); } - @Override - protected void doAssertLuceneQuery(GeoDistanceQueryBuilder queryBuilder, Query query, SearchExecutionContext context) - throws IOException { - final MappedFieldType fieldType = context.getFieldType(queryBuilder.fieldName()); - if (fieldType == null) { - assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery); - } else if (fieldType instanceof GeoPointFieldMapper.GeoPointFieldType) { - Query indexQuery = ((IndexOrDocValuesQuery) query).getIndexQuery(); - - String expectedFieldName = expectedFieldName(queryBuilder.fieldName()); - double qLat = GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(queryBuilder.point().lat())); - double qLon = GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(queryBuilder.point().lon())); - assertEquals(LatLonPoint.newDistanceQuery(expectedFieldName, qLat, qLon, queryBuilder.distance()), indexQuery); - Query dvQuery = ((IndexOrDocValuesQuery) query).getRandomAccessQuery(); - assertEquals(LatLonDocValuesField.newSlowDistanceQuery(expectedFieldName, qLat, qLon, queryBuilder.distance()), dvQuery); - } else { - assertEquals(GeoShapeFieldMapper.GeoShapeFieldType.class, fieldType.getClass()); - } - } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/86834") public void testParsingAndToQueryGeoJSON() throws IOException { // TODO: GeoJSON support missing for geo_distance query, although all other point formats work diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryBuilderTestCase.java similarity index 90% rename from server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java rename to test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryBuilderTestCase.java index 308324acc43c6..507ea6a89aa04 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java +++ b/test/framework/src/main/java/org/elasticsearch/search/geo/GeoShapeQueryBuilderTestCase.java @@ -5,7 +5,7 @@ * in compliance with, at your election, the Elastic License 2.0 or the Server * Side Public License, v 1. */ -package org.elasticsearch.index.query; +package org.elasticsearch.search.geo; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.ConstantScoreQuery; @@ -22,6 +22,13 @@ import org.elasticsearch.geometry.Rectangle; import org.elasticsearch.geometry.utils.WellKnownText; import org.elasticsearch.index.get.GetResult; +import org.elasticsearch.index.query.BoolQueryBuilder; +import org.elasticsearch.index.query.GeoShapeQueryBuilder; +import org.elasticsearch.index.query.QueryBuilder; +import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.QueryShardException; +import org.elasticsearch.index.query.Rewriteable; +import org.elasticsearch.index.query.SearchExecutionContext; import org.elasticsearch.test.AbstractQueryTestCase; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -39,7 +46,7 @@ import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; -public abstract class GeoShapeQueryBuilderTests extends AbstractQueryTestCase { +public abstract class GeoShapeQueryBuilderTestCase extends AbstractQueryTestCase { protected static String indexedShapeId; protected static String indexedShapePath; @@ -47,14 +54,14 @@ public abstract class GeoShapeQueryBuilderTests extends AbstractQueryTestCase new GeoShapeQueryBuilder(fieldName(), (Geometry) null)); + expectThrows(IllegalArgumentException.class, () -> new GeoShapeQueryBuilder(getFieldName(), (Geometry) null)); } public void testNoIndexedShape() { - IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new GeoShapeQueryBuilder(fieldName(), null, null)); + IllegalArgumentException e = expectThrows( + IllegalArgumentException.class, + () -> new GeoShapeQueryBuilder(getFieldName(), null, null) + ); assertEquals("either shape or indexedShapeId is required", e.getMessage()); } public void testNoRelation() { Geometry shape = GeometryTestUtils.randomGeometry(false); - GeoShapeQueryBuilder builder = new GeoShapeQueryBuilder(fieldName(), shape); + GeoShapeQueryBuilder builder = new GeoShapeQueryBuilder(getFieldName(), shape); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> builder.relation(null)); assertEquals("No Shape Relation defined", e.getMessage()); } @@ -158,7 +168,7 @@ public void testMustRewrite() throws IOException { ); assertEquals("query must be rewritten first", e.getMessage()); QueryBuilder rewrite = rewriteAndFetch(query, createSearchExecutionContext()); - GeoShapeQueryBuilder geoShapeQueryBuilder = new GeoShapeQueryBuilder(fieldName(), indexedShapeToReturn); + GeoShapeQueryBuilder geoShapeQueryBuilder = new GeoShapeQueryBuilder(query.fieldName(), indexedShapeToReturn); geoShapeQueryBuilder.strategy(query.strategy()); geoShapeQueryBuilder.relation(query.relation()); assertEquals(geoShapeQueryBuilder, rewrite); @@ -169,7 +179,7 @@ public void testMultipleRewrite() { QueryBuilder builder = new BoolQueryBuilder().should(shape).should(shape); builder = rewriteAndFetch(builder, createSearchExecutionContext()); - GeoShapeQueryBuilder expectedShape = new GeoShapeQueryBuilder(fieldName(), indexedShapeToReturn); + GeoShapeQueryBuilder expectedShape = new GeoShapeQueryBuilder(shape.fieldName(), indexedShapeToReturn); expectedShape.strategy(shape.strategy()); expectedShape.relation(shape.relation()); QueryBuilder expected = new BoolQueryBuilder().should(expectedShape).should(expectedShape); diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoBoundingBoxQueryBuilderGeoShapeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoBoundingBoxQueryBuilderGeoShapeTests.java new file mode 100644 index 0000000000000..75a97a35a923f --- /dev/null +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoBoundingBoxQueryBuilderGeoShapeTests.java @@ -0,0 +1,65 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.spatial.index.query; + +import org.apache.lucene.search.IndexOrDocValuesQuery; +import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.Query; +import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.query.GeoBoundingBoxQueryBuilder; +import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.search.geo.GeoBoundingBoxQueryBuilderTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; +import org.elasticsearch.xpack.spatial.index.mapper.GeoShapeWithDocValuesFieldMapper; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; + +@SuppressWarnings("checkstyle:MissingJavadocMethod") +public class GeoBoundingBoxQueryBuilderGeoShapeTests extends GeoBoundingBoxQueryBuilderTestCase { + private static final String GEO_SHAPE_FIELD_NAME = "mapped_geo_shape"; + protected static final String GEO_SHAPE_ALIAS_FIELD_NAME = "mapped_geo_shape_alias"; + + @Override + protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { + final XContentBuilder builder = PutMappingRequest.simpleMapping( + GEO_SHAPE_FIELD_NAME, + "type=geo_shape", + GEO_SHAPE_ALIAS_FIELD_NAME, + "type=alias,path=" + GEO_SHAPE_FIELD_NAME + ); + mapperService.merge("_doc", new CompressedXContent(Strings.toString(builder)), MapperService.MergeReason.MAPPING_UPDATE); + } + + @Override + protected Collection> getPlugins() { + return Arrays.asList(LocalStateSpatialPlugin.class); + } + + @Override + protected String getFieldName() { + return randomFrom(GEO_SHAPE_FIELD_NAME, GEO_SHAPE_ALIAS_FIELD_NAME); + } + + @Override + protected void doAssertLuceneQuery(GeoBoundingBoxQueryBuilder queryBuilder, Query query, SearchExecutionContext context) { + final MappedFieldType fieldType = context.getFieldType(queryBuilder.fieldName()); + if (fieldType == null) { + assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery); + } + assertEquals(GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType.class, fieldType.getClass()); + assertEquals(IndexOrDocValuesQuery.class, query.getClass()); + } +} diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoDistanceQueryBuilderGeoShapeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoDistanceQueryBuilderGeoShapeTests.java new file mode 100644 index 0000000000000..2e54c82dc88c2 --- /dev/null +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoDistanceQueryBuilderGeoShapeTests.java @@ -0,0 +1,66 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.spatial.index.query; + +import org.apache.lucene.search.IndexOrDocValuesQuery; +import org.apache.lucene.search.MatchNoDocsQuery; +import org.apache.lucene.search.Query; +import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.index.mapper.MappedFieldType; +import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.query.GeoDistanceQueryBuilder; +import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.search.geo.GeoDistanceQueryBuilderTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; +import org.elasticsearch.xpack.spatial.index.mapper.GeoShapeWithDocValuesFieldMapper; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; + +@SuppressWarnings("checkstyle:MissingJavadocMethod") +public class GeoDistanceQueryBuilderGeoShapeTests extends GeoDistanceQueryBuilderTestCase { + + private static final String GEO_SHAPE_FIELD_NAME = "mapped_geo_shape"; + protected static final String GEO_SHAPE_ALIAS_FIELD_NAME = "mapped_geo_shape_alias"; + + @Override + protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { + final XContentBuilder builder = PutMappingRequest.simpleMapping( + GEO_SHAPE_FIELD_NAME, + "type=geo_shape", + GEO_SHAPE_ALIAS_FIELD_NAME, + "type=alias,path=" + GEO_SHAPE_FIELD_NAME + ); + mapperService.merge("_doc", new CompressedXContent(Strings.toString(builder)), MapperService.MergeReason.MAPPING_UPDATE); + } + + @Override + protected Collection> getPlugins() { + return Arrays.asList(LocalStateSpatialPlugin.class); + } + + @Override + protected String getFieldName() { + return randomFrom(GEO_SHAPE_FIELD_NAME, GEO_SHAPE_ALIAS_FIELD_NAME); + } + + @Override + protected void doAssertLuceneQuery(GeoDistanceQueryBuilder queryBuilder, Query query, SearchExecutionContext context) { + final MappedFieldType fieldType = context.getFieldType(queryBuilder.fieldName()); + if (fieldType == null) { + assertTrue("Found no indexed geo query.", query instanceof MatchNoDocsQuery); + } + assertEquals(GeoShapeWithDocValuesFieldMapper.GeoShapeWithDocValuesFieldType.class, fieldType.getClass()); + assertEquals(IndexOrDocValuesQuery.class, query.getClass()); + } +} diff --git a/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeQueryBuilderGeoShapeTests.java b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeQueryBuilderGeoShapeTests.java new file mode 100644 index 0000000000000..596e0d9c730c8 --- /dev/null +++ b/x-pack/plugin/spatial/src/test/java/org/elasticsearch/xpack/spatial/index/query/GeoShapeQueryBuilderGeoShapeTests.java @@ -0,0 +1,118 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ +package org.elasticsearch.xpack.spatial.index.query; + +import org.elasticsearch.Version; +import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; +import org.elasticsearch.common.Strings; +import org.elasticsearch.common.compress.CompressedXContent; +import org.elasticsearch.common.geo.ShapeRelation; +import org.elasticsearch.geo.GeometryTestUtils; +import org.elasticsearch.geometry.Geometry; +import org.elasticsearch.geometry.ShapeType; +import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.query.GeoShapeQueryBuilder; +import org.elasticsearch.index.query.SearchExecutionContext; +import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.search.geo.GeoShapeQueryBuilderTestCase; +import org.elasticsearch.test.AbstractBuilderTestCase; +import org.elasticsearch.test.ESTestCase; +import org.elasticsearch.xcontent.XContentBuilder; +import org.elasticsearch.xpack.spatial.LocalStateSpatialPlugin; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; + +public class GeoShapeQueryBuilderGeoShapeTests extends GeoShapeQueryBuilderTestCase { + + private static final String GEO_SHAPE_FIELD_NAME = "mapped_geo_shape"; + + protected static final String GEO_SHAPE_ALIAS_FIELD_NAME = "mapped_geo_shape_alias"; + + @Override + protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { + final XContentBuilder builder = PutMappingRequest.simpleMapping( + GEO_SHAPE_FIELD_NAME, + "type=geo_shape", + GEO_SHAPE_ALIAS_FIELD_NAME, + "type=alias,path=" + GEO_SHAPE_FIELD_NAME + ); + mapperService.merge("_doc", new CompressedXContent(Strings.toString(builder)), MapperService.MergeReason.MAPPING_UPDATE); + } + + @Override + protected Collection> getPlugins() { + return Collections.singletonList(LocalStateSpatialPlugin.class); + } + + @Override + protected String getFieldName() { + return randomFrom(GEO_SHAPE_FIELD_NAME, GEO_SHAPE_ALIAS_FIELD_NAME); + } + + @Override + protected GeoShapeQueryBuilder doCreateTestQueryBuilder(boolean indexedShape) { + ShapeType shapeType = ESTestCase.randomFrom( + ShapeType.POINT, + ShapeType.MULTIPOINT, + ShapeType.LINESTRING, + ShapeType.MULTILINESTRING, + ShapeType.POLYGON + ); + Geometry geometry = GeometryTestUtils.randomGeometry(shapeType, false); + GeoShapeQueryBuilder builder; + clearShapeFields(); + if (indexedShape == false) { + builder = new GeoShapeQueryBuilder(getFieldName(), geometry); + } else { + indexedShapeToReturn = geometry; + indexedShapeId = ESTestCase.randomAlphaOfLengthBetween(3, 20); + builder = new GeoShapeQueryBuilder(getFieldName(), indexedShapeId); + if (ESTestCase.randomBoolean()) { + indexedShapeIndex = ESTestCase.randomAlphaOfLengthBetween(3, 20); + builder.indexedShapeIndex(indexedShapeIndex); + } + if (ESTestCase.randomBoolean()) { + indexedShapePath = ESTestCase.randomAlphaOfLengthBetween(3, 20); + builder.indexedShapePath(indexedShapePath); + } + if (ESTestCase.randomBoolean()) { + indexedShapeRouting = ESTestCase.randomAlphaOfLengthBetween(3, 20); + builder.indexedShapeRouting(indexedShapeRouting); + } + } + if (ESTestCase.randomBoolean()) { + SearchExecutionContext context = AbstractBuilderTestCase.createSearchExecutionContext(); + if (context.indexVersionCreated().onOrAfter(Version.V_7_5_0)) { // CONTAINS is only supported from version 7.5 + if (shapeType == ShapeType.LINESTRING || shapeType == ShapeType.MULTILINESTRING) { + builder.relation(ESTestCase.randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.CONTAINS)); + } else { + builder.relation( + ESTestCase.randomFrom( + ShapeRelation.DISJOINT, + ShapeRelation.INTERSECTS, + ShapeRelation.WITHIN, + ShapeRelation.CONTAINS + ) + ); + } + } else { + if (shapeType == ShapeType.LINESTRING || shapeType == ShapeType.MULTILINESTRING) { + builder.relation(ESTestCase.randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS)); + } else { + builder.relation(ESTestCase.randomFrom(ShapeRelation.DISJOINT, ShapeRelation.INTERSECTS, ShapeRelation.WITHIN)); + } + } + } + + if (ESTestCase.randomBoolean()) { + builder.ignoreUnmapped(ESTestCase.randomBoolean()); + } + return builder; + } +}