From c2a0a098906ec1441494c32e3df829d8b5ba7026 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Thu, 8 Mar 2018 12:04:31 +0100 Subject: [PATCH 1/4] AbstractQueryTestCase should run without type less often AbstractQueryTestCase is instanciated without mapping type one third of the time. Since most of the tests are disabled when there is no type this commit modifies the probability to use rarely() instead of a fixed probability. --- .../test/AbstractQueryTestCase.java | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index a56180bfc5ecf..11aeaa9ffadf8 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -187,14 +187,10 @@ public static void beforeClass() { index = new Index(randomAlphaOfLengthBetween(1, 10), "_na_"); - // Set a single type in the index - switch (random().nextInt(3)) { - case 0: + if (rarely()) { currentTypes = new String[0]; // no types - break; - default: + } else { currentTypes = new String[] { "_doc" }; - break; } randomTypes = getRandomTypes(); } @@ -858,21 +854,17 @@ protected static String getRandomRewriteMethod() { } private static String[] getRandomTypes() { - String[] types; - if (currentTypes.length > 0 && randomBoolean()) { - int numberOfQueryTypes = randomIntBetween(1, currentTypes.length); - types = new String[numberOfQueryTypes]; - for (int i = 0; i < numberOfQueryTypes; i++) { - types[i] = randomFrom(currentTypes); - } - } else { - if (randomBoolean()) { - types = new String[]{MetaData.ALL}; - } else { - types = new String[0]; - } + int rand = random().nextInt(3); + switch (rand) { + case 0: + return new String[0]; + case 1: + return currentTypes; + case 2: + return new String[] {MetaData.ALL}; + default: + throw new AssertionError("invalid random number " + rand); } - return types; } protected static Fuzziness randomFuzziness(String fieldName) { From cc8d1b70d0077f8d730cf494998e56f16f55bfeb Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Fri, 9 Mar 2018 11:55:06 +0100 Subject: [PATCH 2/4] Always create _doc type for the main index This commit changes the randomization to always create an index with a type. It also adds a way to create a query shard context that maps to an index with no type registered in order to explicitely test cases where there is no type. --- .../index/query/DisMaxQueryBuilderTests.java | 1 - .../index/query/ExistsQueryBuilderTests.java | 6 +- .../index/query/FuzzyQueryBuilderTests.java | 5 - .../GeoBoundingBoxQueryBuilderTests.java | 24 +--- .../query/GeoDistanceQueryBuilderTests.java | 20 ---- .../query/GeoPolygonQueryBuilderTests.java | 21 ---- .../query/GeoShapeQueryBuilderTests.java | 12 -- .../index/query/IdsQueryBuilderTests.java | 28 ++--- .../MatchPhrasePrefixQueryBuilderTests.java | 3 - .../query/MatchPhraseQueryBuilderTests.java | 3 - .../index/query/MatchQueryBuilderTests.java | 8 -- .../query/MoreLikeThisQueryBuilderTests.java | 1 - .../query/MultiMatchQueryBuilderTests.java | 14 --- .../index/query/PrefixQueryBuilderTests.java | 1 - .../query/QueryStringQueryBuilderTests.java | 49 ++------ .../index/query/RangeQueryBuilderTests.java | 49 ++++---- .../index/query/RegexpQueryBuilderTests.java | 1 - .../query/SimpleQueryStringBuilderTests.java | 25 +---- .../index/query/TermQueryBuilderTests.java | 1 - .../index/query/TermsQueryBuilderTests.java | 1 - .../query/WildcardQueryBuilderTests.java | 10 +- .../FunctionScoreQueryBuilderTests.java | 18 --- .../test/AbstractQueryTestCase.java | 106 +++++++++--------- 23 files changed, 100 insertions(+), 307 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/index/query/DisMaxQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/DisMaxQueryBuilderTests.java index ddd3ddf486f99..98a5d91e1b195 100644 --- a/server/src/test/java/org/elasticsearch/index/query/DisMaxQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/DisMaxQueryBuilderTests.java @@ -89,7 +89,6 @@ public void testIllegalArguments() { } public void testToQueryInnerPrefixQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String queryAsString = "{\n" + " \"dis_max\":{\n" + " \"queries\":[\n" + diff --git a/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java index d4547eee26f89..9872d83f9fc62 100644 --- a/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/ExistsQueryBuilderTests.java @@ -68,11 +68,7 @@ protected void doAssertLuceneQuery(ExistsQueryBuilder queryBuilder, Query query, Collection fields = context.getQueryShardContext().simpleMatchToIndexNames(fieldPattern); Collection mappedFields = fields.stream().filter((field) -> context.getQueryShardContext().getObjectMapper(field) != null || context.getQueryShardContext().getMapperService().fullName(field) != null).collect(Collectors.toList()); - if (getCurrentTypes().length == 0) { - assertThat(query, instanceOf(MatchNoDocsQuery.class)); - MatchNoDocsQuery matchNoDocsQuery = (MatchNoDocsQuery) query; - assertThat(matchNoDocsQuery.toString(null), containsString("Missing types in \"exists\" query.")); - } else if (context.mapperService().getIndexSettings().getIndexVersionCreated().before(Version.V_6_1_0)) { + if (context.mapperService().getIndexSettings().getIndexVersionCreated().before(Version.V_6_1_0)) { if (fields.size() == 1) { assertThat(query, instanceOf(ConstantScoreQuery.class)); ConstantScoreQuery constantScoreQuery = (ConstantScoreQuery) query; diff --git a/server/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java index 4fae80d09a51e..074602104e82d 100644 --- a/server/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/FuzzyQueryBuilderTests.java @@ -99,7 +99,6 @@ public void testUnsupportedFuzzinessForStringType() throws IOException { } public void testToQueryWithStringField() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"fuzzy\":{\n" + " \"" + STRING_FIELD_NAME + "\":{\n" + @@ -122,7 +121,6 @@ public void testToQueryWithStringField() throws IOException { } public void testToQueryWithStringFieldDefinedFuzziness() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"fuzzy\":{\n" + " \"" + STRING_FIELD_NAME + "\":{\n" + @@ -145,7 +143,6 @@ public void testToQueryWithStringFieldDefinedFuzziness() throws IOException { } public void testToQueryWithStringFieldDefinedWrongFuzziness() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String queryMissingFuzzinessUpLimit = "{\n" + " \"fuzzy\":{\n" + " \"" + STRING_FIELD_NAME + "\":{\n" + @@ -208,7 +205,6 @@ public void testToQueryWithStringFieldDefinedWrongFuzziness() throws IOException } public void testToQueryWithNumericField() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"fuzzy\":{\n" + " \"" + INT_FIELD_NAME + "\":{\n" + @@ -293,7 +289,6 @@ public void testParseFailsWithValueArray() { } public void testToQueryWithTranspositions() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = new FuzzyQueryBuilder(STRING_FIELD_NAME, "text").toQuery(createShardContext()); assertThat(query, instanceOf(FuzzyQuery.class)); assertEquals(FuzzyQuery.defaultTranspositions, ((FuzzyQuery)query).getTranspositions()); diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java index aeaca328ceb7b..3887186620b1c 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java @@ -107,15 +107,11 @@ public void testValidationNullTypeString() { assertEquals("cannot parse type from null string", e.getMessage()); } - @Override - public void testToQuery() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - super.testToQuery(); - } - public void testExceptionOnMissingTypes() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length == 0); - QueryShardException e = expectThrows(QueryShardException.class, super::testToQuery); + QueryShardContext context = createShardContextWithNoType(); + GeoBoundingBoxQueryBuilder qb = createTestQueryBuilder(); + qb.ignoreUnmapped(false); + QueryShardException e = expectThrows(QueryShardException.class, () -> qb.toQuery(context)); assertEquals("failed to find geo_point field [mapped_geo_point]", e.getMessage()); } @@ -291,7 +287,6 @@ public void fillIn(double coordinate, GeoBoundingBoxQueryBuilder qb) { } public void testParsingAndToQuery1() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_bounding_box\":{\n" + " \"" + GEO_POINT_FIELD_NAME+ "\":{\n" + @@ -304,7 +299,6 @@ public void testParsingAndToQuery1() throws IOException { } public void testParsingAndToQuery2() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_bounding_box\":{\n" + " \"" + GEO_POINT_FIELD_NAME+ "\":{\n" + @@ -323,7 +317,6 @@ public void testParsingAndToQuery2() throws IOException { } public void testParsingAndToQuery3() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_bounding_box\":{\n" + " \"" + GEO_POINT_FIELD_NAME+ "\":{\n" + @@ -336,7 +329,6 @@ public void testParsingAndToQuery3() throws IOException { } public void testParsingAndToQuery4() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_bounding_box\":{\n" + " \"" + GEO_POINT_FIELD_NAME+ "\":{\n" + @@ -349,7 +341,6 @@ public void testParsingAndToQuery4() throws IOException { } public void testParsingAndToQuery5() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_bounding_box\":{\n" + " \"" + GEO_POINT_FIELD_NAME+ "\":{\n" + @@ -362,7 +353,6 @@ public void testParsingAndToQuery5() throws IOException { } public void testParsingAndToQuery6() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_bounding_box\":{\n" + " \"" + GEO_POINT_FIELD_NAME+ "\":{\n" + @@ -450,12 +440,6 @@ public void testFromWKT() throws IOException { assertEquals(expectedJson, GeoExecType.MEMORY, parsed.type()); } - @Override - public void testMustRewrite() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - super.testMustRewrite(); - } - public void testIgnoreUnmapped() throws IOException { final GeoBoundingBoxQueryBuilder queryBuilder = new GeoBoundingBoxQueryBuilder("unmapped").setCorners(1.0, 0.0, 0.0, 1.0); queryBuilder.ignoreUnmapped(true); diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java index eca99f95c486b..66064ca0f176a 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java @@ -121,7 +121,6 @@ public void testIllegalValues() { */ @Override public void testToQuery() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); super.testToQuery(); } @@ -145,7 +144,6 @@ protected void doAssertLuceneQuery(GeoDistanceQueryBuilder queryBuilder, Query q } public void testParsingAndToQuery1() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"12mi\",\n" + @@ -159,7 +157,6 @@ public void testParsingAndToQuery1() throws IOException { } public void testParsingAndToQuery2() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"12mi\",\n" + @@ -170,7 +167,6 @@ public void testParsingAndToQuery2() throws IOException { } public void testParsingAndToQuery3() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"12mi\",\n" + @@ -181,7 +177,6 @@ public void testParsingAndToQuery3() throws IOException { } public void testParsingAndToQuery4() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"12mi\",\n" + @@ -192,7 +187,6 @@ public void testParsingAndToQuery4() throws IOException { } public void testParsingAndToQuery5() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":12,\n" + @@ -207,7 +201,6 @@ public void testParsingAndToQuery5() throws IOException { } public void testParsingAndToQuery6() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"12\",\n" + @@ -222,7 +215,6 @@ public void testParsingAndToQuery6() throws IOException { } public void testParsingAndToQuery7() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"19.312128\",\n" + @@ -236,7 +228,6 @@ public void testParsingAndToQuery7() throws IOException { } public void testParsingAndToQuery8() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":19.312128,\n" + @@ -250,7 +241,6 @@ public void testParsingAndToQuery8() throws IOException { } public void testParsingAndToQuery9() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"19.312128\",\n" + @@ -265,7 +255,6 @@ public void testParsingAndToQuery9() throws IOException { } public void testParsingAndToQuery10() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":19.312128,\n" + @@ -280,7 +269,6 @@ public void testParsingAndToQuery10() throws IOException { } public void testParsingAndToQuery11() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"19.312128km\",\n" + @@ -294,7 +282,6 @@ public void testParsingAndToQuery11() throws IOException { } public void testParsingAndToQuery12() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_distance\":{\n" + " \"distance\":\"12mi\",\n" + @@ -309,7 +296,6 @@ public void testParsingAndToQuery12() throws IOException { } private void assertGeoDistanceRangeQuery(String query, double lat, double lon, double distance, DistanceUnit distanceUnit) throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query parsedQuery = parseQuery(query).toQuery(createShardContext()); // TODO: what can we check? } @@ -333,12 +319,6 @@ public void testFromJson() throws IOException { assertEquals(json, 12000.0, parsed.distance(), 0.0001); } - @Override - public void testMustRewrite() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - super.testMustRewrite(); - } - public void testIgnoreUnmapped() throws IOException { final GeoDistanceQueryBuilder queryBuilder = new GeoDistanceQueryBuilder("unmapped").point(0.0, 0.0).distance("20m"); queryBuilder.ignoreUnmapped(true); diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java index 7b8c1177ec8ac..11fdfc2966c05 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java @@ -63,17 +63,6 @@ protected void doAssertLuceneQuery(GeoPolygonQueryBuilder queryBuilder, Query qu // todo LatLonPointInPolygon is package private } - /** - * Overridden here to ensure the test is only run if at least one type is - * present in the mappings. Geo queries do not execute if the field is not - * explicitly mapped - */ - @Override - public void testToQuery() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - super.testToQuery(); - } - private static List randomPolygon() { ShapeBuilder shapeBuilder = null; // This is a temporary fix because sometimes the RandomShapeGenerator @@ -139,7 +128,6 @@ public void testParsingAndToQueryParsingExceptions() throws IOException { } public void testParsingAndToQuery1() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_polygon\":{\n" + " \"" + GEO_POINT_FIELD_NAME + "\":{\n" + @@ -155,7 +143,6 @@ public void testParsingAndToQuery1() throws IOException { } public void testParsingAndToQuery2() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_polygon\":{\n" + " \"" + GEO_POINT_FIELD_NAME + "\":{\n" + @@ -180,7 +167,6 @@ public void testParsingAndToQuery2() throws IOException { } public void testParsingAndToQuery3() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_polygon\":{\n" + " \"" + GEO_POINT_FIELD_NAME + "\":{\n" + @@ -196,7 +182,6 @@ public void testParsingAndToQuery3() throws IOException { } public void testParsingAndToQuery4() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_polygon\":{\n" + " \"" + GEO_POINT_FIELD_NAME + "\":{\n" + @@ -235,12 +220,6 @@ public void testFromJson() throws IOException { assertEquals(json, 4, parsed.points().size()); } - @Override - public void testMustRewrite() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - super.testMustRewrite(); - } - public void testIgnoreUnmapped() throws IOException { List polygon = randomPolygon(); final GeoPolygonQueryBuilder queryBuilder = new GeoPolygonQueryBuilder("unmapped", polygon); diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java index 96d756f008366..2a5474ad7873b 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java @@ -146,17 +146,6 @@ protected void doAssertLuceneQuery(GeoShapeQueryBuilder queryBuilder, Query quer assertThat(query, anyOf(instanceOf(BooleanQuery.class), instanceOf(ConstantScoreQuery.class))); } - /** - * Overridden here to ensure the test is only run if at least one type is - * present in the mappings. Geo queries do not execute if the field is not - * explicitly mapped - */ - @Override - public void testToQuery() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - super.testToQuery(); - } - public void testNoFieldName() throws Exception { ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> new GeoShapeQueryBuilder(null, shape)); @@ -272,7 +261,6 @@ public void testIgnoreUnmapped() throws IOException { } public void testWrongFieldType() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); ShapeType shapeType = ShapeType.randomType(random()); ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType); final GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder(STRING_FIELD_NAME, shape); diff --git a/server/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java index e440fc0277229..c7db2493bdfd6 100644 --- a/server/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/IdsQueryBuilderTests.java @@ -38,23 +38,17 @@ public class IdsQueryBuilderTests extends AbstractQueryTestCase @Override protected IdsQueryBuilder doCreateTestQueryBuilder() { - String[] types; - if (getCurrentTypes() != null && getCurrentTypes().length > 0 && randomBoolean()) { - int numberOfTypes = randomIntBetween(1, getCurrentTypes().length); - types = new String[numberOfTypes]; - for (int i = 0; i < numberOfTypes; i++) { - if (frequently()) { - types[i] = randomFrom(getCurrentTypes()); - } else { - types[i] = randomAlphaOfLengthBetween(1, 10); - } - } - } else { - if (randomBoolean()) { - types = new String[]{MetaData.ALL}; + final String type; + if (randomBoolean()) { + if (frequently()) { + type = "_doc"; } else { - types = new String[0]; + type = randomAlphaOfLengthBetween(1, 10); } + } else if (randomBoolean()) { + type = MetaData.ALL; + } else { + type = null; } int numberOfIds = randomIntBetween(0, 10); String[] ids = new String[numberOfIds]; @@ -62,8 +56,8 @@ protected IdsQueryBuilder doCreateTestQueryBuilder() { ids[i] = randomAlphaOfLengthBetween(1, 10); } IdsQueryBuilder query; - if (types.length > 0 || randomBoolean()) { - query = new IdsQueryBuilder().types(types); + if (type != null && randomBoolean()) { + query = new IdsQueryBuilder().types(type); query.addIds(ids); } else { query = new IdsQueryBuilder(); diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java index b4d5f98fe0b47..49b917d3ac7a5 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchPhrasePrefixQueryBuilderTests.java @@ -44,9 +44,6 @@ public class MatchPhrasePrefixQueryBuilderTests extends AbstractQueryTestCase 0); - } Object value; if (fieldName.equals(STRING_FIELD_NAME)) { int terms = randomIntBetween(0, 3); diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java index d37d6eeb88fdf..f5f3ec8ff5e0f 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchPhraseQueryBuilderTests.java @@ -44,9 +44,6 @@ public class MatchPhraseQueryBuilderTests extends AbstractQueryTestCase 0); - } Object value; if (fieldName.equals(STRING_FIELD_NAME)) { int terms = randomIntBetween(0, 3); diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java index e8699897d667d..83e4dd56885f7 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java @@ -60,9 +60,6 @@ public class MatchQueryBuilderTests extends AbstractQueryTestCase 0); - } Object value; if (fieldName.equals(STRING_FIELD_NAME)) { int terms = randomIntBetween(0, 3); @@ -267,7 +264,6 @@ public void testSimpleMatchQuery() throws IOException { } public void testFuzzinessOnNonStringField() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); MatchQueryBuilder query = new MatchQueryBuilder(INT_FIELD_NAME, 42); query.fuzziness(randomFuzziness(INT_FIELD_NAME)); QueryShardContext context = createShardContext(); @@ -288,7 +284,6 @@ public void testFuzzinessOnNonStringField() throws Exception { } public void testExactOnUnsupportedField() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); MatchQueryBuilder query = new MatchQueryBuilder(GEO_POINT_FIELD_NAME, "2,3"); QueryShardContext context = createShardContext(); QueryShardException e = expectThrows(QueryShardException.class, () -> query.toQuery(context)); @@ -340,7 +335,6 @@ public void testParseFailsWithTermsArray() throws Exception { } public void testExceptionUsingAnalyzerOnNumericField() { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext shardContext = createShardContext(); MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder(DOUBLE_FIELD_NAME, 6.075210893508043E-4); matchQueryBuilder.analyzer("simple"); @@ -359,7 +353,6 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws } public void testMatchPhrasePrefixWithBoost() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext context = createShardContext(); assumeTrue("test runs only when the index version is on or after V_5_0_0_alpha1", context.indexVersionCreated().onOrAfter(Version.V_5_0_0_alpha1)); @@ -383,7 +376,6 @@ public void testMatchPhrasePrefixWithBoost() throws Exception { } public void testLenientPhraseQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext context = createShardContext(); MatchQuery b = new MatchQuery(context); b.setLenient(true); diff --git a/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java index 922aa9a682f45..3604c3048fb30 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MoreLikeThisQueryBuilderTests.java @@ -293,7 +293,6 @@ public void testValidateEmptyLike() { } public void testUnsupportedFields() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String unsupportedField = randomFrom(INT_FIELD_NAME, DOUBLE_FIELD_NAME, DATE_FIELD_NAME); MoreLikeThisQueryBuilder queryBuilder = new MoreLikeThisQueryBuilder(new String[] {unsupportedField}, new String[]{"some text"}, null) .failOnUnsupportedField(true); diff --git a/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java index b6d4816b01f4a..5a12b588e5efc 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MultiMatchQueryBuilderTests.java @@ -65,9 +65,6 @@ public class MultiMatchQueryBuilderTests extends AbstractQueryTestCase 0); - } final Object value; if (fieldName.equals(STRING_FIELD_NAME)) { @@ -173,7 +170,6 @@ public void testIllegaArguments() { } public void testToQueryBoost() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext shardContext = createShardContext(); MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder("test"); multiMatchQueryBuilder.field(STRING_FIELD_NAME, 5f); @@ -191,7 +187,6 @@ public void testToQueryBoost() throws IOException { } public void testToQueryMultipleTermsBooleanQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = multiMatchQuery("test1 test2").field(STRING_FIELD_NAME).useDisMax(false).toQuery(createShardContext()); assertThat(query, instanceOf(BooleanQuery.class)); BooleanQuery bQuery = (BooleanQuery) query; @@ -201,7 +196,6 @@ public void testToQueryMultipleTermsBooleanQuery() throws Exception { } public void testToQueryMultipleFieldsDisableDismax() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = multiMatchQuery("test").field(STRING_FIELD_NAME).field(STRING_FIELD_NAME_2).useDisMax(false).toQuery(createShardContext()); assertThat(query, instanceOf(DisjunctionMaxQuery.class)); DisjunctionMaxQuery dQuery = (DisjunctionMaxQuery) query; @@ -212,7 +206,6 @@ public void testToQueryMultipleFieldsDisableDismax() throws Exception { } public void testToQueryMultipleFieldsDisMaxQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = multiMatchQuery("test").field(STRING_FIELD_NAME).field(STRING_FIELD_NAME_2).useDisMax(true).toQuery(createShardContext()); assertThat(query, instanceOf(DisjunctionMaxQuery.class)); DisjunctionMaxQuery disMaxQuery = (DisjunctionMaxQuery) query; @@ -225,7 +218,6 @@ public void testToQueryMultipleFieldsDisMaxQuery() throws Exception { } public void testToQueryFieldsWildcard() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = multiMatchQuery("test").field("mapped_str*").useDisMax(false).toQuery(createShardContext()); assertThat(query, instanceOf(DisjunctionMaxQuery.class)); DisjunctionMaxQuery dQuery = (DisjunctionMaxQuery) query; @@ -236,7 +228,6 @@ public void testToQueryFieldsWildcard() throws Exception { } public void testToQueryFieldMissing() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); assertThat(multiMatchQuery("test").field(MISSING_WILDCARD_FIELD_NAME).toQuery(createShardContext()), instanceOf(MatchNoDocsQuery.class)); assertThat(multiMatchQuery("test").field(MISSING_FIELD_NAME).toQuery(createShardContext()), instanceOf(MatchNoDocsQuery.class)); } @@ -306,7 +297,6 @@ public void testQueryParameterArrayException() { } public void testExceptionUsingAnalyzerOnNumericField() { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext shardContext = createShardContext(); MultiMatchQueryBuilder multiMatchQueryBuilder = new MultiMatchQueryBuilder(6.075210893508043E-4); multiMatchQueryBuilder.field(DOUBLE_FIELD_NAME); @@ -316,7 +306,6 @@ public void testExceptionUsingAnalyzerOnNumericField() { } public void testFuzzinessOnNonStringField() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); MultiMatchQueryBuilder query = new MultiMatchQueryBuilder(42).field(INT_FIELD_NAME).field(BOOLEAN_FIELD_NAME); query.fuzziness(randomFuzziness(INT_FIELD_NAME)); QueryShardContext context = createShardContext(); @@ -335,8 +324,6 @@ public void testFuzzinessOnNonStringField() throws Exception { } public void testToFuzzyQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - MultiMatchQueryBuilder qb = new MultiMatchQueryBuilder("text").field(STRING_FIELD_NAME); qb.fuzziness(Fuzziness.TWO); qb.prefixLength(2); @@ -350,7 +337,6 @@ public void testToFuzzyQuery() throws Exception { } public void testDefaultField() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext context = createShardContext(); MultiMatchQueryBuilder builder = new MultiMatchQueryBuilder("hello"); // should pass because we set lenient to true when default field is `*` diff --git a/server/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java index 11cf639974557..788787514f031 100644 --- a/server/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/PrefixQueryBuilderTests.java @@ -105,7 +105,6 @@ public void testFromJson() throws IOException { } public void testNumeric() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); PrefixQueryBuilder query = prefixQuery(INT_FIELD_NAME, "12*"); QueryShardContext context = createShardContext(); QueryShardException e = expectThrows(QueryShardException.class, diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index fb0bedc695b67..5795416d28f50 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -191,7 +191,6 @@ public void testToQueryMatchAllQuery() throws Exception { } public void testToQueryTermQuery() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("test").defaultField(STRING_FIELD_NAME).toQuery(createShardContext()); assertThat(query, instanceOf(TermQuery.class)); TermQuery termQuery = (TermQuery) query; @@ -199,7 +198,6 @@ public void testToQueryTermQuery() throws IOException { } public void testToQueryPhraseQuery() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("\"term1 term2\"") .defaultField(STRING_FIELD_NAME) .phraseSlop(3) @@ -213,7 +211,6 @@ public void testToQueryPhraseQuery() throws IOException { } public void testToQueryBoosts() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext shardContext = createShardContext(); QueryStringQueryBuilder queryStringQuery = queryStringQuery(STRING_FIELD_NAME + ":boosted^2"); Query query = queryStringQuery.toQuery(shardContext); @@ -253,7 +250,6 @@ public void testToQueryBoosts() throws Exception { } public void testToQueryMultipleTermsBooleanQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("test1 test2").field(STRING_FIELD_NAME) .toQuery(createShardContext()); assertThat(query, instanceOf(BooleanQuery.class)); @@ -266,7 +262,6 @@ public void testToQueryMultipleTermsBooleanQuery() throws Exception { } public void testToQueryMultipleFieldsBooleanQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("test").field(STRING_FIELD_NAME) .field(STRING_FIELD_NAME_2) .toQuery(createShardContext()); @@ -280,7 +275,6 @@ public void testToQueryMultipleFieldsBooleanQuery() throws Exception { } public void testToQueryMultipleFieldsDisMaxQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("test").field(STRING_FIELD_NAME).field(STRING_FIELD_NAME_2) .toQuery(createShardContext()); assertThat(query, instanceOf(DisjunctionMaxQuery.class)); @@ -291,7 +285,6 @@ public void testToQueryMultipleFieldsDisMaxQuery() throws Exception { } public void testToQueryFieldsWildcard() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("test").field("mapped_str*").toQuery(createShardContext()); assertThat(query, instanceOf(DisjunctionMaxQuery.class)); DisjunctionMaxQuery dQuery = (DisjunctionMaxQuery) query; @@ -303,7 +296,6 @@ public void testToQueryFieldsWildcard() throws Exception { } public void testToQueryDisMaxQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("test").field(STRING_FIELD_NAME, 2.2f) .field(STRING_FIELD_NAME_2) .toQuery(createShardContext()); @@ -315,7 +307,6 @@ public void testToQueryDisMaxQuery() throws Exception { } public void testToQueryWildcardQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); for (Operator op : Operator.values()) { BooleanClause.Occur defaultOp = op.toBooleanClauseOccur(); QueryStringQueryParser queryParser = new QueryStringQueryParser(createShardContext(), STRING_FIELD_NAME); @@ -338,7 +329,6 @@ public void testToQueryWildcardQuery() throws Exception { } public void testToQueryWilcardQueryWithSynonyms() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); for (Operator op : Operator.values()) { BooleanClause.Occur defaultOp = op.toBooleanClauseOccur(); QueryStringQueryParser queryParser = new QueryStringQueryParser(createShardContext(), STRING_FIELD_NAME); @@ -371,7 +361,6 @@ public void testToQueryWilcardQueryWithSynonyms() throws Exception { } public void testToQueryWithGraph() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); for (Operator op : Operator.values()) { BooleanClause.Occur defaultOp = op.toBooleanClauseOccur(); QueryStringQueryParser queryParser = new QueryStringQueryParser(createShardContext(), STRING_FIELD_NAME); @@ -486,7 +475,6 @@ public void testToQueryWithGraph() throws Exception { } public void testToQueryRegExpQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = queryStringQuery("/foo*bar/").defaultField(STRING_FIELD_NAME) .maxDeterminizedStates(5000) .toQuery(createShardContext()); @@ -496,7 +484,6 @@ public void testToQueryRegExpQuery() throws Exception { } public void testToQueryRegExpQueryTooComplex() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryStringQueryBuilder queryBuilder = queryStringQuery("/[ac]*a[ac]{50,200}/").defaultField(STRING_FIELD_NAME); TooComplexToDeterminizeException e = expectThrows(TooComplexToDeterminizeException.class, @@ -509,7 +496,6 @@ public void testToQueryRegExpQueryTooComplex() throws Exception { * Validates that {@code max_determinized_states} can be parsed and lowers the allowed number of determinized states. */ public void testToQueryRegExpQueryMaxDeterminizedStatesParsing() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); XContentBuilder builder = JsonXContent.contentBuilder(); builder.startObject(); { builder.startObject("query_string"); { @@ -532,7 +518,6 @@ public void testToQueryRegExpQueryMaxDeterminizedStatesParsing() throws Exceptio * Validates that {@code max_determinized_states} can be parsed and lowers the allowed number of determinized states. */ public void testEnabledPositionIncrements() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); XContentBuilder builder = JsonXContent.contentBuilder(); builder.startObject(); { @@ -550,8 +535,6 @@ public void testEnabledPositionIncrements() throws Exception { } public void testToQueryFuzzyQueryAutoFuziness() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - int length = randomIntBetween(1, 10); StringBuilder queryString = new StringBuilder(); for (int i = 0; i < length; i++) { @@ -576,7 +559,6 @@ public void testToQueryFuzzyQueryAutoFuziness() throws Exception { } public void testFuzzyNumeric() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryStringQueryBuilder query = queryStringQuery("12~0.2").defaultField(INT_FIELD_NAME); QueryShardContext context = createShardContext(); IllegalArgumentException e = expectThrows(IllegalArgumentException.class, @@ -588,7 +570,6 @@ public void testFuzzyNumeric() throws Exception { } public void testPrefixNumeric() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryStringQueryBuilder query = queryStringQuery("12*").defaultField(INT_FIELD_NAME); QueryShardContext context = createShardContext(); QueryShardException e = expectThrows(QueryShardException.class, @@ -600,7 +581,6 @@ public void testPrefixNumeric() throws Exception { } public void testExactGeo() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryStringQueryBuilder query = queryStringQuery("2,3").defaultField(GEO_POINT_FIELD_NAME); QueryShardContext context = createShardContext(); QueryShardException e = expectThrows(QueryShardException.class, @@ -612,7 +592,6 @@ public void testExactGeo() throws Exception { } public void testTimezone() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String queryAsString = "{\n" + " \"query_string\":{\n" + " \"time_zone\":\"Europe/Paris\",\n" + @@ -634,7 +613,6 @@ public void testTimezone() throws Exception { } public void testToQueryBooleanQueryMultipleBoosts() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); int numBoosts = randomIntBetween(2, 10); float[] boosts = new float[numBoosts + 1]; String queryStringPrefix = ""; @@ -673,7 +651,6 @@ public void testToQueryBooleanQueryMultipleBoosts() throws Exception { } public void testToQueryPhraseQueryBoostAndSlop() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder("\"test phrase\"~2").field(STRING_FIELD_NAME, 5f); Query query = queryStringQueryBuilder.toQuery(createShardContext()); @@ -687,7 +664,6 @@ public void testToQueryPhraseQueryBoostAndSlop() throws IOException { } public void testToQueryWildcardNonExistingFields() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder("foo bar").field("invalid*"); Query query = queryStringQueryBuilder.toQuery(createShardContext()); @@ -706,7 +682,6 @@ public void testToQueryWildcardNonExistingFields() throws IOException { } public void testToQueryTextParsing() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); { QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder("foo bar") @@ -805,16 +780,15 @@ public void testExistsFieldQuery() throws Exception { QueryShardContext context = createShardContext(); QueryStringQueryBuilder queryBuilder = new QueryStringQueryBuilder(STRING_FIELD_NAME + ":*"); Query query = queryBuilder.toQuery(context); - if (getCurrentTypes().length > 0) { - if (context.getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) - && (context.fieldMapper(STRING_FIELD_NAME).omitNorms() == false)) { - assertThat(query, equalTo(new ConstantScoreQuery(new NormsFieldExistsQuery(STRING_FIELD_NAME)))); - } else { - assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", STRING_FIELD_NAME))))); - } + if (context.getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) + && (context.fieldMapper(STRING_FIELD_NAME).omitNorms() == false)) { + assertThat(query, equalTo(new ConstantScoreQuery(new NormsFieldExistsQuery(STRING_FIELD_NAME)))); } else { - assertThat(query, equalTo(new MatchNoDocsQuery())); + assertThat(query, equalTo(new ConstantScoreQuery(new TermQuery(new Term("_field_names", STRING_FIELD_NAME))))); } + QueryShardContext contextNoType = createShardContextWithNoType(); + query = queryBuilder.toQuery(contextNoType); + assertThat(query, equalTo(new MatchNoDocsQuery())); queryBuilder = new QueryStringQueryBuilder("*:*"); query = queryBuilder.toQuery(context); @@ -828,7 +802,6 @@ public void testExistsFieldQuery() throws Exception { } public void testDisabledFieldNamesField() throws Exception { - assumeTrue("No types", getCurrentTypes().length > 0); QueryShardContext context = createShardContext(); context.getMapperService().merge("_doc", new CompressedXContent( @@ -886,7 +859,6 @@ public void testFromJson() throws IOException { } public void testExpandedTerms() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); // Prefix Query query = new QueryStringQueryBuilder("aBc*") .field(STRING_FIELD_NAME) @@ -949,7 +921,6 @@ public void testDefaultFieldsWithFields() throws IOException { } public void testLenientRewriteToMatchNoDocs() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); // Term Query query = new QueryStringQueryBuilder("hello") .field(INT_FIELD_NAME) @@ -995,7 +966,6 @@ public void testUnmappedFieldRewriteToMatchNoDocs() throws IOException { } public void testDefaultField() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext context = createShardContext(); context.getIndexSettings().updateIndexMetaData( newIndexMeta("index", context.getIndexSettings().getSettings(), Settings.builder().putList("index.query.default_field", @@ -1021,7 +991,6 @@ public void testDefaultField() throws Exception { * the quote analyzer should overwrite any other forced analyzer in quoted parts of the query */ public void testQuoteAnalyzer() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); // Prefix Query query = new QueryStringQueryBuilder("ONE \"TWO THREE\"") .field(STRING_FIELD_NAME) @@ -1040,8 +1009,6 @@ public void testQuoteAnalyzer() throws Exception { } public void testToFuzzyQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - Query query = new QueryStringQueryBuilder("text~2") .field(STRING_FIELD_NAME) .fuzzyPrefixLength(2) @@ -1053,7 +1020,6 @@ public void testToFuzzyQuery() throws Exception { } public void testWithStopWords() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = new QueryStringQueryBuilder("the quick fox") .field(STRING_FIELD_NAME) .analyzer("english") @@ -1066,7 +1032,6 @@ public void testWithStopWords() throws Exception { } public void testWithPrefixStopWords() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = new QueryStringQueryBuilder("the* quick fox") .field(STRING_FIELD_NAME) .analyzer("english") diff --git a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index 3668c7dec17a0..99c9cafcce1b5 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -130,26 +130,21 @@ protected Map getAlternateVersions() { protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException { if (queryBuilder.from() == null && queryBuilder.to() == null) { final Query expectedQuery; - if (getCurrentTypes().length > 0) { - if (context.mapperService().getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) - && context.mapperService().fullName(queryBuilder.fieldName()).hasDocValues()) { - expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(queryBuilder.fieldName())); - } else if (context.mapperService().getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) - && context.mapperService().fullName(queryBuilder.fieldName()).omitNorms() == false) { - expectedQuery = new ConstantScoreQuery(new NormsFieldExistsQuery(queryBuilder.fieldName())); - } else { - expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, queryBuilder.fieldName()))); - } + if (context.mapperService().getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) + && context.mapperService().fullName(queryBuilder.fieldName()).hasDocValues()) { + expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(queryBuilder.fieldName())); + } else if (context.mapperService().getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) && + context.mapperService().fullName(queryBuilder.fieldName()).omitNorms() == false) { + expectedQuery = new ConstantScoreQuery(new NormsFieldExistsQuery(queryBuilder.fieldName())); } else { - expectedQuery = new MatchNoDocsQuery("no mappings yet"); + expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, queryBuilder.fieldName()))); } assertThat(query, equalTo(expectedQuery)); - } else if (getCurrentTypes().length == 0 || - (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false - && queryBuilder.fieldName().equals(INT_FIELD_NAME) == false - && queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) == false - && queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME) == false)) { + } else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && + queryBuilder.fieldName().equals(INT_FIELD_NAME) == false && + queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) == false && + queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME) == false) { assertThat(query, instanceOf(TermRangeQuery.class)); TermRangeQuery termRangeQuery = (TermRangeQuery) query; assertThat(termRangeQuery.getField(), equalTo(queryBuilder.fieldName())); @@ -254,7 +249,6 @@ public void testToQueryUnmappedWithTimezone() throws QueryShardException { } public void testToQueryNumericField() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query parsedQuery = rangeQuery(INT_FIELD_NAME).from(23).to(54).includeLower(true).includeUpper(false).toQuery(createShardContext()); // since age is automatically registered in data, we encode it as numeric assertThat(parsedQuery, instanceOf(IndexOrDocValuesQuery.class)); @@ -264,7 +258,6 @@ public void testToQueryNumericField() throws IOException { } public void testDateRangeQueryFormat() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); // We test 01/01/2012 from gte and 2030 for lt String query = "{\n" + " \"range\" : {\n" + @@ -299,7 +292,6 @@ public void testDateRangeQueryFormat() throws IOException { } public void testDateRangeBoundaries() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"range\" : {\n" + " \"" + DATE_FIELD_NAME + "\" : {\n" + @@ -336,7 +328,6 @@ public void testDateRangeBoundaries() throws IOException { } public void testDateRangeQueryTimezone() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"range\" : {\n" + " \"" + DATE_FIELD_NAME + "\" : {\n" + @@ -424,19 +415,19 @@ protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteC assertThat(rewrittenRange.to(), equalTo(null)); // Range query with open bounds rewrite to an exists query - final Query luceneQuery = rewrittenRange.toQuery(queryShardContext); + Query luceneQuery = rewrittenRange.toQuery(queryShardContext); final Query expectedQuery; - if (getCurrentTypes().length > 0) { - if (queryShardContext.getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) - && queryShardContext.fieldMapper(query.fieldName()).hasDocValues()) { - expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(query.fieldName())); - } else { - expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, query.fieldName()))); - } + if (queryShardContext.getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) + && queryShardContext.fieldMapper(query.fieldName()).hasDocValues()) { + expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(query.fieldName())); } else { - expectedQuery = new MatchNoDocsQuery("no mappings yet"); + expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, query.fieldName()))); } assertThat(luceneQuery, equalTo(expectedQuery)); + + QueryShardContext queryShardContextWithUnkType = createShardContextWithNoType(); + luceneQuery = rewrittenRange.toQuery(queryShardContextWithUnkType); + assertThat(luceneQuery, equalTo(new MatchNoDocsQuery("no mappings yet"))); } public void testRewriteDateToMatchAllWithTimezoneAndFormat() throws IOException { diff --git a/server/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java index efbb84c3239dd..62c7868c66d11 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RegexpQueryBuilderTests.java @@ -114,7 +114,6 @@ public void testFromJson() throws IOException { } public void testNumeric() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); RegexpQueryBuilder query = new RegexpQueryBuilder(INT_FIELD_NAME, "12"); QueryShardContext context = createShardContext(); QueryShardException e = expectThrows(QueryShardException.class, () -> query.toQuery(context)); diff --git a/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java index 7ff2b7ec12285..18297a8d259c5 100644 --- a/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/SimpleQueryStringBuilderTests.java @@ -247,11 +247,8 @@ public void testDefaultFieldParsing() throws IOException { assertThat(queryBuilder.fields().size(), equalTo(0)); QueryShardContext shardContext = createShardContext(); - // the remaining tests requires either a mapping that we register with types in base test setup - if (getCurrentTypes().length > 0) { - Query luceneQuery = queryBuilder.toQuery(shardContext); - assertThat(luceneQuery, anyOf(instanceOf(BooleanQuery.class), instanceOf(DisjunctionMaxQuery.class))); - } + Query luceneQuery = queryBuilder.toQuery(shardContext); + assertThat(luceneQuery, anyOf(instanceOf(BooleanQuery.class), instanceOf(DisjunctionMaxQuery.class))); } /* @@ -315,7 +312,6 @@ private static int shouldClauses(BooleanQuery query) { } public void testToQueryBoost() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext shardContext = createShardContext(); SimpleQueryStringBuilder simpleQueryStringBuilder = new SimpleQueryStringBuilder("test"); simpleQueryStringBuilder.field(STRING_FIELD_NAME, 5); @@ -380,7 +376,6 @@ public void testFromJson() throws IOException { } public void testMinimumShouldMatch() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext shardContext = createShardContext(); int numberOfTerms = randomIntBetween(1, 4); StringBuilder queryString = new StringBuilder(); @@ -421,13 +416,10 @@ public void testIndexMetaField() throws IOException { simpleQueryStringBuilder.field("_index"); Query query = simpleQueryStringBuilder.toQuery(shardContext); assertThat(query, notNullValue()); - if (getCurrentTypes().length > 0) { - assertThat(query, instanceOf(MatchAllDocsQuery.class)); - } + assertThat(query, instanceOf(MatchAllDocsQuery.class)); } public void testExpandedTerms() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); // Prefix Query query = new SimpleQueryStringBuilder("aBc*") .field(STRING_FIELD_NAME) @@ -456,7 +448,6 @@ public void testExpandedTerms() throws Exception { } public void testAnalyzeWildcard() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings(); settings.analyzeWildcard(true); SimpleQueryStringQueryParser parser = new SimpleQueryStringQueryParser(new StandardAnalyzer(), @@ -480,7 +471,6 @@ public void testAnalyzeWildcard() throws IOException { } public void testAnalyzerWildcardWithSynonyms() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings(); settings.analyzeWildcard(true); SimpleQueryStringQueryParser parser = new SimpleQueryStringQueryParser(new MockRepeatAnalyzer(), @@ -512,7 +502,6 @@ public void testAnalyzerWildcardWithSynonyms() throws IOException { } public void testAnalyzerWithGraph() { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings(); settings.analyzeWildcard(true); SimpleQueryStringQueryParser parser = new SimpleQueryStringQueryParser(new MockSynonymAnalyzer(), @@ -557,7 +546,6 @@ public void testAnalyzerWithGraph() { } public void testQuoteFieldSuffix() { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); SimpleQueryStringQueryParser.Settings settings = new SimpleQueryStringQueryParser.Settings(); settings.analyzeWildcard(true); settings.quoteFieldSuffix("_2"); @@ -575,7 +563,6 @@ public void testQuoteFieldSuffix() { } public void testDefaultField() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext context = createShardContext(); context.getIndexSettings().updateIndexMetaData( newIndexMeta("index", context.getIndexSettings().getSettings(), Settings.builder().putList("index.query.default_field", @@ -598,8 +585,6 @@ public void testDefaultField() throws Exception { } public void testToFuzzyQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - Query query = new SimpleQueryStringBuilder("text~2") .field(STRING_FIELD_NAME) .fuzzyPrefixLength(2) @@ -611,8 +596,6 @@ public void testToFuzzyQuery() throws Exception { } public void testLenientToPrefixQuery() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - Query query = new SimpleQueryStringBuilder("t*") .field(DATE_FIELD_NAME) .field(STRING_FIELD_NAME) @@ -626,7 +609,6 @@ public void testLenientToPrefixQuery() throws Exception { } public void testWithStopWords() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = new SimpleQueryStringBuilder("the quick fox") .field(STRING_FIELD_NAME) .analyzer("english") @@ -639,7 +621,6 @@ public void testWithStopWords() throws Exception { } public void testWithPrefixStopWords() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); Query query = new SimpleQueryStringBuilder("the* quick fox") .field(STRING_FIELD_NAME) .analyzer("english") diff --git a/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java index 45cbd43864f90..fb9ac09a12c4e 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TermQueryBuilderTests.java @@ -135,7 +135,6 @@ public void testFromJson() throws IOException { } public void testGeo() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); TermQueryBuilder query = new TermQueryBuilder(GEO_POINT_FIELD_NAME, "2,3"); QueryShardContext context = createShardContext(); QueryShardException e = expectThrows(QueryShardException.class, () -> query.toQuery(context)); diff --git a/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java index c945e595213fd..2fd5bee35d756 100644 --- a/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java @@ -258,7 +258,6 @@ public void testMustRewrite() throws IOException { } public void testGeo() throws Exception { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); TermsQueryBuilder query = new TermsQueryBuilder(GEO_POINT_FIELD_NAME, "2,3"); QueryShardContext context = createShardContext(); QueryShardException e = expectThrows(QueryShardException.class, diff --git a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java index 7da423de25b05..c2ac5a1f3ffd4 100644 --- a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java @@ -138,19 +138,17 @@ public void testWithMetaDataField() throws IOException { assertEquals(expected, query); } } - - public void testIndexWildcard() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); + public void testIndexWildcard() throws IOException { QueryShardContext context = createShardContext(); String index = context.getFullyQualifiedIndexName(); - + Query query = new WildcardQueryBuilder("_index", index).doToQuery(context); assertThat(query instanceof MatchAllDocsQuery, equalTo(true)); - + query = new WildcardQueryBuilder("_index", index + "*").doToQuery(context); assertThat(query instanceof MatchAllDocsQuery, equalTo(true)); - + query = new WildcardQueryBuilder("_index", "index_" + index + "*").doToQuery(context); assertThat(query instanceof MatchNoDocsQuery, equalTo(true)); } diff --git a/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index a2865cfa129b5..11d3b9c8835a0 100644 --- a/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -255,17 +255,6 @@ protected void doAssertLuceneQuery(FunctionScoreQueryBuilder queryBuilder, Query assertThat(query, either(instanceOf(FunctionScoreQuery.class)).or(instanceOf(FunctionScoreQuery.class))); } - /** - * Overridden here to ensure the test is only run if at least one type is - * present in the mappings. Functions require the field to be - * explicitly mapped - */ - @Override - public void testToQuery() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - super.testToQuery(); - } - public void testIllegalArguments() { expectThrows(IllegalArgumentException.class, () -> new FunctionScoreQueryBuilder((QueryBuilder) null)); expectThrows(IllegalArgumentException.class, () -> new FunctionScoreQueryBuilder((ScoreFunctionBuilder) null)); @@ -486,7 +475,6 @@ public void testProperErrorMessageWhenMissingFunction() throws IOException { } public void testWeight1fStillProducesWeightFunction() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String queryString = jsonBuilder().startObject() .startObject("function_score") .startArray("functions") @@ -649,12 +637,6 @@ public void testFromJson() throws IOException { assertEquals(json, 1, parsed.getMinScore(), 0.0001); } - @Override - public void testMustRewrite() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); - super.testMustRewrite(); - } - public void testRewrite() throws IOException { FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString())) diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index 11aeaa9ffadf8..29354536bfb89 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -37,7 +37,7 @@ import org.elasticsearch.action.termvectors.MultiTermVectorsResponse; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.cluster.metadata.MetaData; +import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; @@ -157,18 +157,17 @@ public abstract class AbstractQueryTestCase> protected static Version indexVersionCreated; private static ServiceHolder serviceHolder; + private static ServiceHolder serviceHolderWithNoType; private static int queryNameId = 0; private static Settings nodeSettings; private static Index index; - private static String[] currentTypes; - private static String[] randomTypes; + private static Index indexWithNoType; protected static Index getIndex() { return index; } - - protected static String[] getCurrentTypes() { - return currentTypes; + protected static Index getIndexWithNoType() { + return indexWithNoType; } protected Collection> getPlugins() { @@ -185,14 +184,8 @@ public static void beforeClass() { .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) .build(); - index = new Index(randomAlphaOfLengthBetween(1, 10), "_na_"); - - if (rarely()) { - currentTypes = new String[0]; // no types - } else { - currentTypes = new String[] { "_doc" }; - } - randomTypes = getRandomTypes(); + index = new Index(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLength(10)); + indexWithNoType = new Index(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLength(10)); } protected Settings indexSettings() { @@ -207,18 +200,24 @@ protected Settings indexSettings() { @AfterClass public static void afterClass() throws Exception { IOUtils.close(serviceHolder); + IOUtils.close(serviceHolderWithNoType); serviceHolder = null; + serviceHolderWithNoType = null; } @Before public void beforeTest() throws IOException { if (serviceHolder == null) { - serviceHolder = new ServiceHolder(nodeSettings, indexSettings(), getPlugins(), this); + serviceHolder = new ServiceHolder(nodeSettings, indexSettings(), getPlugins(), true, this::initializeAdditionalMappings); } serviceHolder.clientInvocationHandler.delegate = this; + if (serviceHolderWithNoType == null) { + serviceHolderWithNoType = new ServiceHolder(nodeSettings, indexSettings(), getPlugins(), false, null); + } + serviceHolderWithNoType.clientInvocationHandler.delegate = this; } - private static SearchContext getSearchContext(String[] types, QueryShardContext context) { + private static SearchContext getSearchContext(QueryShardContext context) { TestSearchContext testSearchContext = new TestSearchContext(context) { @Override public MapperService mapperService() { @@ -231,7 +230,6 @@ public > IFD getForField(MappedFieldType fieldType } }; - testSearchContext.getQueryShardContext().setTypes(types); return testSearchContext; } @@ -575,7 +573,7 @@ public void testToQuery() throws IOException { context.setAllowUnmappedFields(true); QB firstQuery = createTestQueryBuilder(); QB controlQuery = copyQuery(firstQuery); - SearchContext searchContext = getSearchContext(randomTypes, context); + SearchContext searchContext = getSearchContext(context); /* we use a private rewrite context here since we want the most realistic way of asserting that we are cacheable or not. * We do it this way in SearchService where * we first rewrite the query with a private context, then reset the context and then build the actual lucene query*/ @@ -605,7 +603,7 @@ public void testToQuery() throws IOException { secondQuery.queryName(secondQuery.queryName() == null ? randomAlphaOfLengthBetween(1, 30) : secondQuery.queryName() + randomAlphaOfLengthBetween(1, 10)); } - searchContext = getSearchContext(randomTypes, context); + searchContext = getSearchContext(context); Query secondLuceneQuery = rewriteQuery(secondQuery, context).toQuery(context); assertNotNull("toQuery should not return null", secondLuceneQuery); assertLuceneQuery(secondQuery, secondLuceneQuery, searchContext); @@ -778,7 +776,16 @@ private QB copyQuery(QB query) throws IOException { * @return a new {@link QueryShardContext} based on the base test index and queryParserService */ protected static QueryShardContext createShardContext() { - return serviceHolder.createShardContext(); + QueryShardContext context = serviceHolder.createShardContext(); + context.setTypes("_doc"); + return context; + } + + /** + * @return a new {@link QueryShardContext} based on an index with no type registered + */ + protected static QueryShardContext createShardContextWithNoType() { + return serviceHolderWithNoType.createShardContext(); } /** @@ -830,10 +837,11 @@ protected static String getRandomQueryText() { */ protected static String getRandomFieldName() { // if no type is set then return a random field name - if (currentTypes.length == 0 || randomBoolean()) { + if (randomBoolean()) { return randomAlphaOfLengthBetween(1, 10); + } else { + return randomFrom(MAPPED_LEAF_FIELD_NAMES); } - return randomFrom(MAPPED_LEAF_FIELD_NAMES); } /** @@ -853,20 +861,6 @@ protected static String getRandomRewriteMethod() { return rewrite; } - private static String[] getRandomTypes() { - int rand = random().nextInt(3); - switch (rand) { - case 0: - return new String[0]; - case 1: - return currentTypes; - case 2: - return new String[] {MetaData.ALL}; - default: - throw new AssertionError("invalid random number " + rand); - } - } - protected static Fuzziness randomFuzziness(String fieldName) { switch (fieldName) { case INT_FIELD_NAME: @@ -1020,8 +1014,8 @@ private static class ServiceHolder implements Closeable { private final Client client; private final long nowInMillis = randomNonNegativeLong(); - ServiceHolder(Settings nodeSettings, Settings indexSettings, - Collection> plugins, AbstractQueryTestCase testCase) throws IOException { + ServiceHolder(Settings nodeSettings, Settings indexSettings, Collection> plugins, boolean registerType, + CheckedConsumer mapperServiceConsumer) throws IOException { Environment env = InternalSettingsPreparer.prepareEnvironment(nodeSettings); PluginsService pluginsService; pluginsService = new PluginsService(nodeSettings, null, env.modulesFile(), env.pluginsFile(), plugins); @@ -1068,27 +1062,27 @@ public void onRemoval(ShardId shardId, Accountable accountable) { } }); - for (String type : currentTypes) { - mapperService.merge(type, new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef(type, - STRING_FIELD_NAME, "type=text", - STRING_FIELD_NAME_2, "type=keyword", - INT_FIELD_NAME, "type=integer", - INT_RANGE_FIELD_NAME, "type=integer_range", - DOUBLE_FIELD_NAME, "type=double", - BOOLEAN_FIELD_NAME, "type=boolean", - DATE_FIELD_NAME, "type=date", - DATE_RANGE_FIELD_NAME, "type=date_range", - OBJECT_FIELD_NAME, "type=object", - GEO_POINT_FIELD_NAME, "type=geo_point", - GEO_SHAPE_FIELD_NAME, "type=geo_shape" + if (registerType) { + mapperService.merge("_doc", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("_doc", + STRING_FIELD_NAME, "type=text", + STRING_FIELD_NAME_2, "type=keyword", + INT_FIELD_NAME, "type=integer", + INT_RANGE_FIELD_NAME, "type=integer_range", + DOUBLE_FIELD_NAME, "type=double", + BOOLEAN_FIELD_NAME, "type=boolean", + DATE_FIELD_NAME, "type=date", + DATE_RANGE_FIELD_NAME, "type=date_range", + OBJECT_FIELD_NAME, "type=object", + GEO_POINT_FIELD_NAME, "type=geo_point", + GEO_SHAPE_FIELD_NAME, "type=geo_shape" ).string()), MapperService.MergeReason.MAPPING_UPDATE); + // also add mappings for two inner field in the object field - mapperService.merge(type, new CompressedXContent("{\"properties\":{\"" + OBJECT_FIELD_NAME + "\":{\"type\":\"object\"," - + "\"properties\":{\"" + DATE_FIELD_NAME + "\":{\"type\":\"date\"},\"" + - INT_FIELD_NAME + "\":{\"type\":\"integer\"}}}}}"), - MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge("_doc", new CompressedXContent("{\"properties\":{\"" + OBJECT_FIELD_NAME + "\":{\"type\":\"object\"," + + "\"properties\":{\"" + DATE_FIELD_NAME + "\":{\"type\":\"date\"},\"" + + INT_FIELD_NAME + "\":{\"type\":\"integer\"}}}}}"), MapperService.MergeReason.MAPPING_UPDATE); + mapperServiceConsumer.accept(mapperService); } - testCase.initializeAdditionalMappings(mapperService); } @Override From a5fc4f54499f90fb977b293e1cdb1cfa5e465f27 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Wed, 25 Jul 2018 10:37:54 +0200 Subject: [PATCH 3/4] fix tests after merge --- .../GeoBoundingBoxQueryBuilderTests.java | 15 +- .../query/GeoPolygonQueryBuilderTests.java | 1 - .../index/query/MatchQueryBuilderTests.java | 2 - .../query/QueryStringQueryBuilderTests.java | 1 - .../index/query/RangeQueryBuilderTests.java | 17 +- .../FunctionScoreQueryBuilderTests.java | 4 +- .../bucket/histogram/DateHistogramTests.java | 1 - .../test/AbstractBuilderTestCase.java | 92 ++---- .../test/AbstractQueryTestCase.java | 282 +----------------- 9 files changed, 48 insertions(+), 367 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java index 1522b7f4aacb7..568410fa8bd85 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java @@ -110,18 +110,12 @@ public void testValidationNullTypeString() { assertEquals("cannot parse type from null string", e.getMessage()); } - public void testExceptionOnMissingTypes() throws IOException { -<<<<<<< HEAD + public void testExceptionOnMissingTypes() { QueryShardContext context = createShardContextWithNoType(); GeoBoundingBoxQueryBuilder qb = createTestQueryBuilder(); qb.ignoreUnmapped(false); QueryShardException e = expectThrows(QueryShardException.class, () -> qb.toQuery(context)); - assertEquals("failed to find geo_point field [mapped_geo_point]", e.getMessage()); -======= - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length == 0); - QueryShardException e = expectThrows(QueryShardException.class, super::testToQuery); - assertThat(e.getMessage(), startsWith("failed to find geo_point field [mapped_geo_point")); ->>>>>>> master + assertEquals("failed to find geo_point field [" + qb.fieldName() + "]", e.getMessage()); } public void testBrokenCoordinateCannotBeSet() { @@ -450,8 +444,6 @@ public void testFromWKT() throws IOException { assertEquals(expectedJson, GeoExecType.MEMORY, parsed.type()); } -<<<<<<< HEAD -======= public void testFromGeohash() throws IOException { String json = "{\n" + @@ -511,7 +503,6 @@ public void testMalformedGeohashes() { } public void testHonorsCoercion() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"geo_bounding_box\": {\n" + " \"validation_method\": \"COERCE\",\n" + @@ -532,11 +523,9 @@ public void testHonorsCoercion() throws IOException { @Override public void testMustRewrite() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); super.testMustRewrite(); } ->>>>>>> master public void testIgnoreUnmapped() throws IOException { final GeoBoundingBoxQueryBuilder queryBuilder = new GeoBoundingBoxQueryBuilder("unmapped").setCorners(1.0, 0.0, 0.0, 1.0); queryBuilder.ignoreUnmapped(true); diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java index 3bdb87516bad2..ee64e595e0845 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java @@ -234,7 +234,6 @@ public void testIgnoreUnmapped() throws IOException { } public void testPointValidation() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext context = createShardContext(); String queryInvalidLat = "{\n" + " \"geo_polygon\":{\n" + diff --git a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java index 7c1c33460b711..0de9cac885502 100644 --- a/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/MatchQueryBuilderTests.java @@ -52,8 +52,6 @@ import java.util.Locale; import java.util.Map; -import static org.elasticsearch.test.AbstractBuilderTestCase.STRING_ALIAS_FIELD_NAME; -import static org.elasticsearch.test.AbstractBuilderTestCase.expectedFieldName; import static org.hamcrest.CoreMatchers.either; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.Matchers.containsString; diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java index 3fc48807515c1..87197b662d142 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryStringQueryBuilderTests.java @@ -1221,7 +1221,6 @@ public void testQuoteAnalyzer() throws Exception { } public void testQuoteFieldSuffix() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); QueryShardContext context = createShardContext(); assertEquals(new TermQuery(new Term(STRING_FIELD_NAME, "bar")), new QueryStringQueryBuilder("bar") diff --git a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index 32c0b25bca96e..312007c0be3bb 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -52,9 +52,6 @@ import java.util.Map; import static org.elasticsearch.index.query.QueryBuilders.rangeQuery; -import static org.elasticsearch.test.AbstractBuilderTestCase.DATE_ALIAS_FIELD_NAME; -import static org.elasticsearch.test.AbstractBuilderTestCase.INT_ALIAS_FIELD_NAME; -import static org.elasticsearch.test.AbstractBuilderTestCase.STRING_ALIAS_FIELD_NAME; import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -138,18 +135,18 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, final Query expectedQuery; if (context.mapperService().getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) && context.mapperService().fullName(queryBuilder.fieldName()).hasDocValues()) { - expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(queryBuilder.fieldName())); + expectedQuery = new ConstantScoreQuery(new DocValuesFieldExistsQuery(expectedFieldName)); } else if (context.mapperService().getIndexSettings().getIndexVersionCreated().onOrAfter(Version.V_6_1_0) && context.mapperService().fullName(queryBuilder.fieldName()).omitNorms() == false) { - expectedQuery = new ConstantScoreQuery(new NormsFieldExistsQuery(queryBuilder.fieldName())); + expectedQuery = new ConstantScoreQuery(new NormsFieldExistsQuery(expectedFieldName)); } else { - expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, queryBuilder.fieldName()))); + expectedQuery = new ConstantScoreQuery(new TermQuery(new Term(FieldNamesFieldMapper.NAME, expectedFieldName))); } assertThat(query, equalTo(expectedQuery)); - } else if (queryBuilder.fieldName().equals(DATE_FIELD_NAME) == false && - queryBuilder.fieldName().equals(INT_FIELD_NAME) == false && - queryBuilder.fieldName().equals(DATE_RANGE_FIELD_NAME) == false && - queryBuilder.fieldName().equals(INT_RANGE_FIELD_NAME) == false) { + } else if (expectedFieldName.equals(DATE_FIELD_NAME) == false && + expectedFieldName.equals(INT_FIELD_NAME) == false && + expectedFieldName.equals(DATE_RANGE_FIELD_NAME) == false && + expectedFieldName.equals(INT_RANGE_FIELD_NAME) == false) { assertThat(query, instanceOf(TermRangeQuery.class)); TermRangeQuery termRangeQuery = (TermRangeQuery) query; assertThat(termRangeQuery.getField(), equalTo(expectedFieldName)); diff --git a/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index 7c42486c5abff..cc224019100b5 100644 --- a/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -476,7 +476,7 @@ public void testProperErrorMessageWhenMissingFunction() throws IOException { } public void testWeight1fStillProducesWeightFunction() throws IOException { - String queryString = jsonBuilder().startObject() + String queryString = Strings.toString(jsonBuilder().startObject() .startObject("function_score") .startArray("functions") .startObject() @@ -487,7 +487,7 @@ public void testWeight1fStillProducesWeightFunction() throws IOException { .endObject() .endArray() .endObject() - .endObject().string(); + .endObject()); QueryBuilder query = parseQuery(queryString); assertThat(query, instanceOf(FunctionScoreQueryBuilder.class)); FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) query; diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java index 1f83842eab24f..a2b0fa8848ebd 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java @@ -141,7 +141,6 @@ private static Document documentForDate(String field, long millis) { } public void testRewriteTimeZone() throws IOException { - Assume.assumeTrue(getCurrentTypes().length > 0); // we need mappings FormatDateTimeFormatter format = Joda.forPattern("strict_date_optional_time"); try (Directory dir = newDirectory(); diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java index a891f30b93d21..a9f8102ae908d 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java @@ -31,7 +31,6 @@ import org.elasticsearch.action.termvectors.MultiTermVectorsResponse; import org.elasticsearch.client.Client; import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.cluster.metadata.MetaData; import org.elasticsearch.common.Strings; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.io.stream.NamedWriteableRegistry; @@ -40,7 +39,6 @@ import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.common.xcontent.NamedXContentRegistry; -import org.elasticsearch.core.internal.io.IOUtils; import org.elasticsearch.env.Environment; import org.elasticsearch.env.TestEnvironment; import org.elasticsearch.index.Index; @@ -129,20 +127,16 @@ public abstract class AbstractBuilderTestCase extends ESTestCase { protected static Version indexVersionCreated; private static ServiceHolder serviceHolder; + private static ServiceHolder serviceHolderWithNoType; private static int queryNameId = 0; private static Settings nodeSettings; private static Index index; - private static String[] currentTypes; - protected static String[] randomTypes; + private static Index indexWithNoType; protected static Index getIndex() { return index; } - protected static String[] getCurrentTypes() { - return currentTypes; - } - protected Collection> getPlugins() { return Collections.emptyList(); } @@ -153,40 +147,12 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws @BeforeClass public static void beforeClass() { nodeSettings = Settings.builder() - .put("node.name", AbstractQueryTestCase.class.toString()) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .build(); - - index = new Index(randomAlphaOfLengthBetween(1, 10), "_na_"); - - // Set a single type in the index - switch (random().nextInt(3)) { - case 0: - currentTypes = new String[0]; // no types - break; - default: - currentTypes = new String[] { "_doc" }; - break; - } - randomTypes = getRandomTypes(); - } + .put("node.name", AbstractQueryTestCase.class.toString()) + .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) + .build(); - private static String[] getRandomTypes() { - String[] types; - if (currentTypes.length > 0 && randomBoolean()) { - int numberOfQueryTypes = randomIntBetween(1, currentTypes.length); - types = new String[numberOfQueryTypes]; - for (int i = 0; i < numberOfQueryTypes; i++) { - types[i] = randomFrom(currentTypes); - } - } else { - if (randomBoolean()) { - types = new String[]{MetaData.ALL}; - } else { - types = new String[0]; - } - } - return types; + index = new Index(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLength(10)); + indexWithNoType = new Index(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLength(10)); } @Override @@ -210,34 +176,37 @@ protected static String createUniqueRandomName() { protected Settings indexSettings() { // we have to prefer CURRENT since with the range of versions we support it's rather unlikely to get the current actually. indexVersionCreated = randomBoolean() ? Version.CURRENT - : VersionUtils.randomVersionBetween(random(), null, Version.CURRENT); + : VersionUtils.randomVersionBetween(random(), Version.V_6_0_0, Version.CURRENT); return Settings.builder() .put(IndexMetaData.SETTING_VERSION_CREATED, indexVersionCreated) .build(); } protected static String expectedFieldName(String builderFieldName) { - if (currentTypes.length == 0) { - return builderFieldName; - } return ALIAS_TO_CONCRETE_FIELD_NAME.getOrDefault(builderFieldName, builderFieldName); } @AfterClass public static void afterClass() throws Exception { - IOUtils.close(serviceHolder); + org.apache.lucene.util.IOUtils.close(serviceHolder); + org.apache.lucene.util.IOUtils.close(serviceHolderWithNoType); serviceHolder = null; + serviceHolderWithNoType = null; } @Before public void beforeTest() throws IOException { if (serviceHolder == null) { - serviceHolder = new ServiceHolder(nodeSettings, indexSettings(), getPlugins(), this); + serviceHolder = new ServiceHolder(nodeSettings, indexSettings(), getPlugins(), this, true); } serviceHolder.clientInvocationHandler.delegate = this; + if (serviceHolderWithNoType == null) { + serviceHolderWithNoType = new ServiceHolder(nodeSettings, indexSettings(), getPlugins(), this, false); + } + serviceHolderWithNoType.clientInvocationHandler.delegate = this; } - protected static SearchContext getSearchContext(String[] types, QueryShardContext context) { + protected static SearchContext getSearchContext(QueryShardContext context) { TestSearchContext testSearchContext = new TestSearchContext(context) { @Override public MapperService mapperService() { @@ -250,13 +219,13 @@ public > IFD getForField(MappedFieldType fieldType } }; - testSearchContext.getQueryShardContext().setTypes(types); return testSearchContext; } @After public void afterTest() { serviceHolder.clientInvocationHandler.delegate = null; + serviceHolderWithNoType.clientInvocationHandler.delegate = null; } /** @@ -280,6 +249,13 @@ protected static QueryShardContext createShardContext(IndexReader reader) { return serviceHolder.createShardContext(reader); } + /** + * @return a new {@link QueryShardContext} based on an index with no type registered + */ + protected static QueryShardContext createShardContextWithNoType() { + return serviceHolderWithNoType.createShardContext(null); + } + /** * @return a new {@link QueryShardContext} based on the base test index and queryParserService */ @@ -332,7 +308,7 @@ private static class ServiceHolder implements Closeable { private final long nowInMillis = randomNonNegativeLong(); ServiceHolder(Settings nodeSettings, Settings indexSettings, - Collection> plugins, AbstractBuilderTestCase testCase) throws IOException { + Collection> plugins, AbstractBuilderTestCase testCase, boolean registerType) throws IOException { Environment env = InternalSettingsPreparer.prepareEnvironment(nodeSettings); PluginsService pluginsService; pluginsService = new PluginsService(nodeSettings, null, env.modulesFile(), env.pluginsFile(), plugins); @@ -379,9 +355,8 @@ public void onRemoval(ShardId shardId, Accountable accountable) { } }); - - for (String type : currentTypes) { - mapperService.merge(type, new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef(type, + if (registerType) { + mapperService.merge("_doc", new CompressedXContent(Strings.toString(PutMappingRequest.buildFromSimplifiedDef("_doc", STRING_FIELD_NAME, "type=text", STRING_FIELD_NAME_2, "type=keyword", STRING_ALIAS_FIELD_NAME, "type=alias,path=" + STRING_FIELD_NAME, @@ -399,12 +374,12 @@ public void onRemoval(ShardId shardId, Accountable accountable) { GEO_SHAPE_FIELD_NAME, "type=geo_shape" ))), MapperService.MergeReason.MAPPING_UPDATE); // also add mappings for two inner field in the object field - mapperService.merge(type, new CompressedXContent("{\"properties\":{\"" + OBJECT_FIELD_NAME + "\":{\"type\":\"object\"," - + "\"properties\":{\"" + DATE_FIELD_NAME + "\":{\"type\":\"date\"},\"" + - INT_FIELD_NAME + "\":{\"type\":\"integer\"}}}}}"), - MapperService.MergeReason.MAPPING_UPDATE); + mapperService.merge("_doc", new CompressedXContent("{\"properties\":{\"" + OBJECT_FIELD_NAME + "\":{\"type\":\"object\"," + + "\"properties\":{\"" + DATE_FIELD_NAME + "\":{\"type\":\"date\"},\"" + + INT_FIELD_NAME + "\":{\"type\":\"integer\"}}}}}"), + MapperService.MergeReason.MAPPING_UPDATE); + testCase.initializeAdditionalMappings(mapperService); } - testCase.initializeAdditionalMappings(mapperService); } @Override @@ -423,5 +398,4 @@ ScriptModule createScriptModule(List scriptPlugins) { return new ScriptModule(Settings.EMPTY, scriptPlugins); } } - } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java index 8895323223e19..f71edfde84f0f 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractQueryTestCase.java @@ -25,29 +25,17 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.spans.SpanBoostQuery; -import org.apache.lucene.util.Accountable; -import org.apache.lucene.util.IOUtils; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.Version; -import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.action.support.PlainActionFuture; -import org.elasticsearch.client.Client; -import org.elasticsearch.cluster.metadata.IndexMetaData; -import org.elasticsearch.common.CheckedConsumer; import org.elasticsearch.common.ParsingException; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.collect.Tuple; -import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.common.io.stream.BytesStreamOutput; import org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput; -import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.Writeable.Reader; -import org.elasticsearch.common.settings.IndexScopedSettings; -import org.elasticsearch.common.settings.Setting; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.common.xcontent.DeprecationHandler; import org.elasticsearch.common.xcontent.NamedXContentRegistry; @@ -60,53 +48,18 @@ import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.env.Environment; -import org.elasticsearch.env.TestEnvironment; -import org.elasticsearch.index.Index; -import org.elasticsearch.index.IndexSettings; -import org.elasticsearch.index.analysis.IndexAnalyzers; -import org.elasticsearch.index.cache.bitset.BitsetFilterCache; -import org.elasticsearch.index.fielddata.IndexFieldData; -import org.elasticsearch.index.fielddata.IndexFieldDataCache; -import org.elasticsearch.index.fielddata.IndexFieldDataService; -import org.elasticsearch.index.mapper.MappedFieldType; -import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryRewriteContext; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.query.Rewriteable; import org.elasticsearch.index.query.support.QueryParsers; -import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.index.similarity.SimilarityService; -import org.elasticsearch.indices.IndicesModule; -import org.elasticsearch.indices.analysis.AnalysisModule; -import org.elasticsearch.indices.breaker.NoneCircuitBreakerService; -import org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache; -import org.elasticsearch.indices.mapper.MapperRegistry; -import org.elasticsearch.node.InternalSettingsPreparer; -import org.elasticsearch.plugins.MapperPlugin; -import org.elasticsearch.plugins.Plugin; -import org.elasticsearch.plugins.PluginsService; -import org.elasticsearch.plugins.ScriptPlugin; -import org.elasticsearch.plugins.SearchPlugin; -import org.elasticsearch.script.ScriptModule; -import org.elasticsearch.script.ScriptService; -import org.elasticsearch.search.SearchModule; import org.elasticsearch.search.internal.SearchContext; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; -import java.io.Closeable; import java.io.IOException; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Proxy; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.Deque; import java.util.HashSet; @@ -115,13 +68,8 @@ import java.util.Locale; import java.util.Map; import java.util.Set; -import java.util.function.Function; -import java.util.stream.Stream; -import static java.util.Collections.emptyList; -import static java.util.stream.Collectors.toList; import static org.elasticsearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder; -import static org.elasticsearch.test.AbstractBuilderTestCase.STRING_ALIAS_FIELD_NAME; import static org.elasticsearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.Matchers.containsString; @@ -129,109 +77,10 @@ import static org.hamcrest.Matchers.greaterThan; import static org.hamcrest.Matchers.instanceOf; -public abstract class AbstractQueryTestCase> extends ESTestCase { - - public static final String STRING_FIELD_NAME = "mapped_string"; - protected static final String STRING_FIELD_NAME_2 = "mapped_string_2"; - protected static final String INT_FIELD_NAME = "mapped_int"; - protected static final String INT_RANGE_FIELD_NAME = "mapped_int_range"; - protected static final String DOUBLE_FIELD_NAME = "mapped_double"; - protected static final String BOOLEAN_FIELD_NAME = "mapped_boolean"; - protected static final String DATE_FIELD_NAME = "mapped_date"; - protected static final String DATE_RANGE_FIELD_NAME = "mapped_date_range"; - protected static final String OBJECT_FIELD_NAME = "mapped_object"; - protected static final String GEO_POINT_FIELD_NAME = "mapped_geo_point"; - protected static final String GEO_SHAPE_FIELD_NAME = "mapped_geo_shape"; - protected static final String[] MAPPED_FIELD_NAMES = new String[]{STRING_FIELD_NAME, INT_FIELD_NAME, INT_RANGE_FIELD_NAME, - DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, DATE_RANGE_FIELD_NAME, OBJECT_FIELD_NAME, GEO_POINT_FIELD_NAME, - GEO_SHAPE_FIELD_NAME}; - private static final String[] MAPPED_LEAF_FIELD_NAMES = new String[]{STRING_FIELD_NAME, INT_FIELD_NAME, INT_RANGE_FIELD_NAME, - DOUBLE_FIELD_NAME, BOOLEAN_FIELD_NAME, DATE_FIELD_NAME, DATE_RANGE_FIELD_NAME, GEO_POINT_FIELD_NAME, }; - private static final int NUMBER_OF_TESTQUERIES = 20; - - protected static Version indexVersionCreated; - - private static ServiceHolder serviceHolder; - private static ServiceHolder serviceHolderWithNoType; - private static int queryNameId = 0; - private static Settings nodeSettings; - private static Index index; - private static Index indexWithNoType; - - protected static Index getIndex() { - return index; - } - protected static Index getIndexWithNoType() { - return indexWithNoType; - } - - protected Collection> getPlugins() { - return emptyList(); - } - - protected void initializeAdditionalMappings(MapperService mapperService) throws IOException { - } - - @BeforeClass - public static void beforeClass() { - nodeSettings = Settings.builder() - .put("node.name", AbstractQueryTestCase.class.toString()) - .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) - .build(); - index = new Index(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLength(10)); - indexWithNoType = new Index(randomAlphaOfLengthBetween(1, 10), randomAlphaOfLength(10)); - } - - protected Settings indexSettings() { - // we have to prefer CURRENT since with the range of versions we support it's rather unlikely to get the current actually. - indexVersionCreated = randomBoolean() ? Version.CURRENT - : VersionUtils.randomVersionBetween(random(), null, Version.CURRENT); - return Settings.builder() - .put(IndexMetaData.SETTING_VERSION_CREATED, indexVersionCreated) - .build(); - } +public abstract class AbstractQueryTestCase> extends AbstractBuilderTestCase { - @AfterClass - public static void afterClass() throws Exception { - IOUtils.close(serviceHolder); - IOUtils.close(serviceHolderWithNoType); - serviceHolder = null; - serviceHolderWithNoType = null; - } - - @Before - public void beforeTest() throws IOException { - if (serviceHolder == null) { - serviceHolder = new ServiceHolder(nodeSettings, indexSettings(), getPlugins(), true, this::initializeAdditionalMappings); - } - serviceHolder.clientInvocationHandler.delegate = this; - if (serviceHolderWithNoType == null) { - serviceHolderWithNoType = new ServiceHolder(nodeSettings, indexSettings(), getPlugins(), false, null); - } - serviceHolderWithNoType.clientInvocationHandler.delegate = this; - } - - private static SearchContext getSearchContext(QueryShardContext context) { - TestSearchContext testSearchContext = new TestSearchContext(context) { - @Override - public MapperService mapperService() { - return serviceHolder.mapperService; // need to build / parse inner hits sort fields - } - - @Override - public > IFD getForField(MappedFieldType fieldType) { - return serviceHolder.indexFieldDataService.getForField(fieldType); // need to build / parse inner hits sort fields - } - - }; - return testSearchContext; - } - - @After - public void afterTest() { - serviceHolder.clientInvocationHandler.delegate = null; - } + private static final int NUMBER_OF_TESTQUERIES = 20; public final QB createTestQueryBuilder() { QB query = doCreateTestQueryBuilder(); @@ -765,24 +614,8 @@ protected QB changeNameOrBoost(QB original) throws IOException { //we use the streaming infra to create a copy of the query provided as argument @SuppressWarnings("unchecked") private QB copyQuery(QB query) throws IOException { - Reader reader = (Reader) serviceHolder.namedWriteableRegistry.getReader(QueryBuilder.class, query.getWriteableName()); - return copyWriteable(query, serviceHolder.namedWriteableRegistry, reader); - } - - /** - * @return a new {@link QueryShardContext} based on the base test index and queryParserService - */ - protected static QueryShardContext createShardContext() { - QueryShardContext context = serviceHolder.createShardContext(); - context.setTypes("_doc"); - return context; - } - - /** - * @return a new {@link QueryShardContext} based on an index with no type registered - */ - protected static QueryShardContext createShardContextWithNoType() { - return serviceHolderWithNoType.createShardContext(); + Reader reader = (Reader) namedWriteableRegistry().getReader(QueryBuilder.class, query.getWriteableName()); + return copyWriteable(query, namedWriteableRegistry(), reader); } /** @@ -949,113 +782,6 @@ protected Query rewrite(Query query) throws IOException { return query; } - @Override - protected NamedXContentRegistry xContentRegistry() { - return serviceHolder.xContentRegistry; - } - - private static class ServiceHolder implements Closeable { - private final IndexFieldDataService indexFieldDataService; - private final SearchModule searchModule; - private final NamedWriteableRegistry namedWriteableRegistry; - private final NamedXContentRegistry xContentRegistry; - private final ClientInvocationHandler clientInvocationHandler = new ClientInvocationHandler(); - private final IndexSettings idxSettings; - private final SimilarityService similarityService; - private final MapperService mapperService; - private final BitsetFilterCache bitsetFilterCache; - private final ScriptService scriptService; - private final Client client; - private final long nowInMillis = randomNonNegativeLong(); - - ServiceHolder(Settings nodeSettings, Settings indexSettings, Collection> plugins, boolean registerType, - CheckedConsumer mapperServiceConsumer) throws IOException { - Environment env = InternalSettingsPreparer.prepareEnvironment(nodeSettings); - PluginsService pluginsService; - pluginsService = new PluginsService(nodeSettings, null, env.modulesFile(), env.pluginsFile(), plugins); - - client = (Client) Proxy.newProxyInstance( - Client.class.getClassLoader(), - new Class[]{Client.class}, - clientInvocationHandler); - ScriptModule scriptModule = createScriptModule(pluginsService.filterPlugins(ScriptPlugin.class)); - List> additionalSettings = pluginsService.getPluginSettings(); - additionalSettings.add(InternalSettingsPlugin.VERSION_CREATED); - SettingsModule settingsModule = new SettingsModule(nodeSettings, additionalSettings, pluginsService.getPluginSettingsFilter()); - searchModule = new SearchModule(nodeSettings, false, pluginsService.filterPlugins(SearchPlugin.class)); - IndicesModule indicesModule = new IndicesModule(pluginsService.filterPlugins(MapperPlugin.class)); - List entries = new ArrayList<>(); - entries.addAll(indicesModule.getNamedWriteables()); - entries.addAll(searchModule.getNamedWriteables()); - namedWriteableRegistry = new NamedWriteableRegistry(entries); - xContentRegistry = new NamedXContentRegistry(Stream.of( - searchModule.getNamedXContents().stream() - ).flatMap(Function.identity()).collect(toList())); - IndexScopedSettings indexScopedSettings = settingsModule.getIndexScopedSettings(); - idxSettings = IndexSettingsModule.newIndexSettings(index, indexSettings, indexScopedSettings); - AnalysisModule analysisModule = new AnalysisModule(TestEnvironment.newEnvironment(nodeSettings), emptyList()); - IndexAnalyzers indexAnalyzers = analysisModule.getAnalysisRegistry().build(idxSettings); - scriptService = scriptModule.getScriptService(); - similarityService = new SimilarityService(idxSettings, null, Collections.emptyMap()); - MapperRegistry mapperRegistry = indicesModule.getMapperRegistry(); - mapperService = new MapperService(idxSettings, indexAnalyzers, xContentRegistry, similarityService, mapperRegistry, - this::createShardContext); - IndicesFieldDataCache indicesFieldDataCache = new IndicesFieldDataCache(nodeSettings, new IndexFieldDataCache.Listener() { - }); - indexFieldDataService = new IndexFieldDataService(idxSettings, indicesFieldDataCache, - new NoneCircuitBreakerService(), mapperService); - bitsetFilterCache = new BitsetFilterCache(idxSettings, new BitsetFilterCache.Listener() { - @Override - public void onCache(ShardId shardId, Accountable accountable) { - - } - - @Override - public void onRemoval(ShardId shardId, Accountable accountable) { - - } - }); - - if (registerType) { - mapperService.merge("_doc", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("_doc", - STRING_FIELD_NAME, "type=text", - STRING_FIELD_NAME_2, "type=keyword", - INT_FIELD_NAME, "type=integer", - INT_RANGE_FIELD_NAME, "type=integer_range", - DOUBLE_FIELD_NAME, "type=double", - BOOLEAN_FIELD_NAME, "type=boolean", - DATE_FIELD_NAME, "type=date", - DATE_RANGE_FIELD_NAME, "type=date_range", - OBJECT_FIELD_NAME, "type=object", - GEO_POINT_FIELD_NAME, "type=geo_point", - GEO_SHAPE_FIELD_NAME, "type=geo_shape" - ).string()), MapperService.MergeReason.MAPPING_UPDATE); - - // also add mappings for two inner field in the object field - mapperService.merge("_doc", new CompressedXContent("{\"properties\":{\"" + OBJECT_FIELD_NAME + "\":{\"type\":\"object\"," - + "\"properties\":{\"" + DATE_FIELD_NAME + "\":{\"type\":\"date\"},\"" + - INT_FIELD_NAME + "\":{\"type\":\"integer\"}}}}}"), MapperService.MergeReason.MAPPING_UPDATE); - mapperServiceConsumer.accept(mapperService); - } - } - - @Override - public void close() throws IOException { - } - - QueryShardContext createShardContext() { - return new QueryShardContext(0, idxSettings, bitsetFilterCache, indexFieldDataService::getForField, mapperService, - similarityService, scriptService, xContentRegistry, namedWriteableRegistry, this.client, null, () -> nowInMillis, null); - } - - ScriptModule createScriptModule(List scriptPlugins) { - if (scriptPlugins == null || scriptPlugins.isEmpty()) { - return newTestScriptModule(); - } - return new ScriptModule(Settings.EMPTY, scriptPlugins); - } - } - protected QueryBuilder rewriteAndFetch(QueryBuilder builder, QueryRewriteContext context) throws IOException { PlainActionFuture future = new PlainActionFuture<>(); Rewriteable.rewriteAndFetch(builder, context, future); From 97529a6c2e06d8116c43ee87eb4ab1f62bc62025 Mon Sep 17 00:00:00 2001 From: Jim Ferenczi Date: Wed, 25 Jul 2018 11:57:43 +0200 Subject: [PATCH 4/4] fix style checks --- .../index/query/FeatureQueryBuilderTests.java | 15 +++++---------- .../query/GeoBoundingBoxQueryBuilderTests.java | 1 - .../index/query/RangeQueryBuilderTests.java | 2 +- .../bucket/histogram/DateHistogramTests.java | 4 ---- .../test/AbstractBuilderTestCase.java | 7 +++++-- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/modules/mapper-extras/src/test/java/org/elasticsearch/index/query/FeatureQueryBuilderTests.java b/modules/mapper-extras/src/test/java/org/elasticsearch/index/query/FeatureQueryBuilderTests.java index dd7b42487fdbc..40da4b53227af 100644 --- a/modules/mapper-extras/src/test/java/org/elasticsearch/index/query/FeatureQueryBuilderTests.java +++ b/modules/mapper-extras/src/test/java/org/elasticsearch/index/query/FeatureQueryBuilderTests.java @@ -45,12 +45,10 @@ public class FeatureQueryBuilderTests extends AbstractQueryTestCase 0); String query = "{\n" + " \"feature\" : {\n" + " \"field\": \"my_feature_field\"\n" + @@ -110,7 +107,6 @@ public void testDefaultScoreFunction() throws IOException { } public void testIllegalField() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"feature\" : {\n" + " \"field\": \"" + STRING_FIELD_NAME + "\"\n" + @@ -121,7 +117,6 @@ public void testIllegalField() throws IOException { } public void testIllegalCombination() throws IOException { - assumeTrue("test runs only when at least a type is registered", getCurrentTypes().length > 0); String query = "{\n" + " \"feature\" : {\n" + " \"field\": \"my_negative_feature_field\",\n" + diff --git a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java index 568410fa8bd85..551bd6488cd2f 100644 --- a/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java @@ -39,7 +39,6 @@ import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.notNullValue; -import static org.hamcrest.Matchers.startsWith; public class GeoBoundingBoxQueryBuilderTests extends AbstractQueryTestCase { /** Randomly generate either NaN or one of the two infinity values. */ diff --git a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java index 312007c0be3bb..6be12cc841a59 100644 --- a/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/RangeQueryBuilderTests.java @@ -159,7 +159,7 @@ protected void doAssertLuceneQuery(RangeQueryBuilder queryBuilder, Query query, query = ((IndexOrDocValuesQuery) query).getIndexQuery(); assertThat(query, instanceOf(PointRangeQuery.class)); MapperService mapperService = context.getQueryShardContext().getMapperService(); - MappedFieldType mappedFieldType = mapperService.fullName(DATE_FIELD_NAME); + MappedFieldType mappedFieldType = mapperService.fullName(expectedFieldName); final Long fromInMillis; final Long toInMillis; // we have to normalize the incoming value into milliseconds since it could be literally anything diff --git a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java index a2b0fa8848ebd..8d67941639fb4 100644 --- a/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java +++ b/server/src/test/java/org/elasticsearch/search/aggregations/bucket/histogram/DateHistogramTests.java @@ -31,11 +31,7 @@ import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.aggregations.BaseAggregationTestCase; import org.elasticsearch.search.aggregations.BucketOrder; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramAggregationBuilder; -import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramInterval; -import org.elasticsearch.search.aggregations.bucket.histogram.ExtendedBoundsTests; import org.joda.time.DateTimeZone; -import org.junit.Assume; import java.io.IOException; import java.util.ArrayList; diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java index a9f8102ae908d..fccec5f784f8b 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractBuilderTestCase.java @@ -307,8 +307,11 @@ private static class ServiceHolder implements Closeable { private final Client client; private final long nowInMillis = randomNonNegativeLong(); - ServiceHolder(Settings nodeSettings, Settings indexSettings, - Collection> plugins, AbstractBuilderTestCase testCase, boolean registerType) throws IOException { + ServiceHolder(Settings nodeSettings, + Settings indexSettings, + Collection> plugins, + AbstractBuilderTestCase testCase, + boolean registerType) throws IOException { Environment env = InternalSettingsPreparer.prepareEnvironment(nodeSettings); PluginsService pluginsService; pluginsService = new PluginsService(nodeSettings, null, env.modulesFile(), env.pluginsFile(), plugins);