From 4e77adf38e3bdec774cffee351e3c31f8b9cf25a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20B=C3=BCscher?= Date: Fri, 15 Apr 2016 17:47:19 +0200 Subject: [PATCH] Clean up creation of shard context in AbstractQueryTestCase The queryShardContext we create during setup was sometimes accessed directly, sometimes by making a copy through the createShardContext() helper. This should be the default. Also making sure that strict parsing is switched on via IndexSettings in the test testup. --- .../index/query/AbstractQueryTestCase.java | 25 +++++++++-------- .../index/query/BoolQueryBuilderTests.java | 8 +++--- .../query/BoostingQueryBuilderTests.java | 5 ++-- .../GeoBoundingBoxQueryBuilderTests.java | 10 ++++--- .../query/GeoDistanceQueryBuilderTests.java | 12 ++++---- .../query/GeoDistanceRangeQueryTests.java | 8 +++--- .../query/GeoPolygonQueryBuilderTests.java | 7 ++--- .../query/GeoShapeQueryBuilderTests.java | 4 +-- .../query/GeohashCellQueryBuilderTests.java | 7 ++--- .../query/HasChildQueryBuilderTests.java | 10 +++---- .../query/HasParentQueryBuilderTests.java | 10 +++---- .../index/query/NestedQueryBuilderTests.java | 20 ++++++------- .../query/ParentIdQueryBuilderTests.java | 10 +++---- .../query/PercolatorQueryBuilderTests.java | 6 ++-- .../index/query/RangeQueryBuilderTests.java | 28 +++++++++++-------- .../query/TemplateQueryBuilderTests.java | 13 ++++----- .../index/query/TermsQueryBuilderTests.java | 4 +-- .../index/query/TypeQueryBuilderTests.java | 12 ++++---- .../index/query/WrapperQueryBuilderTests.java | 16 ++++++----- .../FunctionScoreQueryBuilderTests.java | 4 +-- 20 files changed, 112 insertions(+), 107 deletions(-) diff --git a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java index 0f373d3664d..471d1d7baca 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java @@ -120,7 +120,6 @@ import java.util.HashSet; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.Objects; import java.util.Set; import java.util.concurrent.ExecutionException; @@ -158,11 +157,6 @@ public abstract class AbstractQueryTestCase> private static int queryNameId = 0; private static SearchModule searchModule; - - protected static QueryShardContext queryShardContext() { - return queryShardContext; - } - protected static IndexFieldDataService indexFieldDataService() { return indexFieldDataService; } @@ -200,13 +194,15 @@ public abstract class AbstractQueryTestCase> @BeforeClass public static void init() throws IOException { // 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(), Version.V_2_0_0_beta1, Version.CURRENT); + indexVersionCreated = randomBoolean() ? Version.CURRENT + : VersionUtils.randomVersionBetween(random(), Version.V_2_0_0_beta1, Version.CURRENT); Settings settings = Settings.builder() .put("node.name", AbstractQueryTestCase.class.toString()) .put(Environment.PATH_HOME_SETTING.getKey(), createTempDir()) .put(ScriptService.SCRIPT_AUTO_RELOAD_ENABLED_SETTING.getKey(), false) .build(); Settings indexSettings = Settings.builder() + .put(ParseFieldMatcher.PARSE_STRICT, true) .put(IndexMetaData.SETTING_VERSION_CREATED, indexVersionCreated).build(); final ThreadPool threadPool = new ThreadPool(settings); index = new Index(randomAsciiOfLengthBetween(1, 10), "_na_"); @@ -236,14 +232,16 @@ public abstract class AbstractQueryTestCase> Set engines = new HashSet<>(); engines.add(mockScriptEngine); List customContexts = new ArrayList<>(); - ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections.singletonList(new ScriptEngineRegistry.ScriptEngineRegistration(MockScriptEngine.class, MockScriptEngine.TYPES))); + ScriptEngineRegistry scriptEngineRegistry = new ScriptEngineRegistry(Collections + .singletonList(new ScriptEngineRegistry.ScriptEngineRegistration(MockScriptEngine.class, MockScriptEngine.TYPES))); bind(ScriptEngineRegistry.class).toInstance(scriptEngineRegistry); ScriptContextRegistry scriptContextRegistry = new ScriptContextRegistry(customContexts); bind(ScriptContextRegistry.class).toInstance(scriptContextRegistry); ScriptSettings scriptSettings = new ScriptSettings(scriptEngineRegistry, scriptContextRegistry); bind(ScriptSettings.class).toInstance(scriptSettings); try { - ScriptService scriptService = new ScriptService(settings, new Environment(settings), engines, null, scriptEngineRegistry, scriptContextRegistry, scriptSettings); + ScriptService scriptService = new ScriptService(settings, new Environment(settings), engines, null, + scriptEngineRegistry, scriptContextRegistry, scriptSettings); bind(ScriptService.class).toInstance(scriptService); } catch (IOException e) { throw new IllegalStateException("error while binding ScriptService", e); @@ -285,10 +283,12 @@ public abstract class AbstractQueryTestCase> ScriptService scriptService = injector.getInstance(ScriptService.class); SimilarityService similarityService = new SimilarityService(idxSettings, Collections.emptyMap()); MapperRegistry mapperRegistry = injector.getInstance(MapperRegistry.class); - MapperService mapperService = new MapperService(idxSettings, analysisService, similarityService, mapperRegistry, () -> queryShardContext); + MapperService mapperService = new MapperService(idxSettings, analysisService, similarityService, mapperRegistry, + () -> queryShardContext); IndicesFieldDataCache indicesFieldDataCache = new IndicesFieldDataCache(settings, new IndexFieldDataCache.Listener() { }); - indexFieldDataService = new IndexFieldDataService(idxSettings, indicesFieldDataCache, injector.getInstance(CircuitBreakerService.class), mapperService); + indexFieldDataService = new IndexFieldDataService(idxSettings, indicesFieldDataCache, + injector.getInstance(CircuitBreakerService.class), mapperService); BitsetFilterCache bitsetFilterCache = new BitsetFilterCache(idxSettings, new BitsetFilterCache.Listener() { @Override public void onCache(ShardId shardId, Accountable accountable) { @@ -302,7 +302,8 @@ public abstract class AbstractQueryTestCase> }); PercolatorQueryCache percolatorQueryCache = new PercolatorQueryCache(idxSettings, () -> queryShardContext); indicesQueriesRegistry = injector.getInstance(IndicesQueriesRegistry.class); - queryShardContext = new QueryShardContext(idxSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, scriptService, indicesQueriesRegistry, percolatorQueryCache, null); + queryShardContext = new QueryShardContext(idxSettings, bitsetFilterCache, indexFieldDataService, mapperService, similarityService, + scriptService, indicesQueriesRegistry, percolatorQueryCache, null); //create some random type with some default field, those types will stick around for all of the subclasses currentTypes = new String[randomIntBetween(0, 5)]; for (int i = 0; i < currentTypes.length; i++) { diff --git a/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java index 4ed593c6ff7..b6b97f6fbf2 100644 --- a/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java @@ -366,7 +366,7 @@ public class BoolQueryBuilderTests extends AbstractQueryTestCase rewritten = boolQueryBuilder.rewrite(queryShardContext()); + QueryBuilder rewritten = boolQueryBuilder.rewrite(createShardContext()); if (mustRewrite == false && boolQueryBuilder.must().isEmpty()) { // if it's empty we rewrite to match all assertEquals(rewritten, new MatchAllQueryBuilder()); @@ -398,15 +398,15 @@ public class BoolQueryBuilderTests extends AbstractQueryTestCase rewritten = boolQueryBuilder.rewrite(queryShardContext()); + QueryBuilder rewritten = boolQueryBuilder.rewrite(createShardContext()); BoolQueryBuilder expected = new BoolQueryBuilder(); expected.must(new WrapperQueryBuilder(new MatchAllQueryBuilder().toString())); assertEquals(expected, rewritten); expected = new BoolQueryBuilder(); expected.must(new MatchAllQueryBuilder()); - QueryBuilder rewrittenAgain = rewritten.rewrite(queryShardContext()); + QueryBuilder rewrittenAgain = rewritten.rewrite(createShardContext()); assertEquals(rewrittenAgain, expected); - assertEquals(QueryBuilder.rewriteQuery(boolQueryBuilder, queryShardContext()), expected); + assertEquals(QueryBuilder.rewriteQuery(boolQueryBuilder, createShardContext()), expected); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/BoostingQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/BoostingQueryBuilderTests.java index 414ab1f1131..27c888aa9dd 100644 --- a/core/src/test/java/org/elasticsearch/index/query/BoostingQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/BoostingQueryBuilderTests.java @@ -20,7 +20,6 @@ package org.elasticsearch.index.query; import org.apache.lucene.queries.BoostingQuery; -import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import java.io.IOException; @@ -109,12 +108,12 @@ public class BoostingQueryBuilderTests extends AbstractQueryTestCase rewrite = qb.rewrite(queryShardContext()); + QueryBuilder rewrite = qb.rewrite(createShardContext()); if (positive instanceof MatchAllQueryBuilder && negative instanceof MatchAllQueryBuilder) { assertSame(rewrite, qb); } else { assertNotSame(rewrite, qb); - assertEquals(new BoostingQueryBuilder(positive.rewrite(queryShardContext()), negative.rewrite(queryShardContext())), rewrite); + assertEquals(new BoostingQueryBuilder(positive.rewrite(createShardContext()), negative.rewrite(createShardContext())), rewrite); } } } diff --git a/core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java index 734495189b9..558d8b0e3f1 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeoBoundingBoxQueryBuilderTests.java @@ -439,8 +439,9 @@ public class GeoBoundingBoxQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(shardContext)); assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]")); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java index dff2b667d25..13036ce168d 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeoDistanceQueryBuilderTests.java @@ -19,11 +19,9 @@ package org.elasticsearch.index.query; -import org.locationtech.spatial4j.shape.Point; -import org.apache.lucene.spatial.geopoint.search.GeoPointDistanceQuery; - import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; +import org.apache.lucene.spatial.geopoint.search.GeoPointDistanceQuery; import org.apache.lucene.spatial.util.GeoEncodingUtils; import org.elasticsearch.Version; import org.elasticsearch.common.geo.GeoDistance; @@ -31,6 +29,7 @@ import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.search.geo.GeoDistanceRangeQuery; import org.elasticsearch.test.geo.RandomShapeGenerator; +import org.locationtech.spatial4j.shape.Point; import java.io.IOException; @@ -384,7 +383,7 @@ public class GeoDistanceQueryBuilderTests extends AbstractQueryTestCase 0); Query parsedQuery = parseQuery(query).toQuery(createShardContext()); - Version version = queryShardContext().indexVersionCreated(); + Version version = createShardContext().indexVersionCreated(); if (version.before(Version.V_2_2_0)) { GeoDistanceRangeQuery q = (GeoDistanceRangeQuery) parsedQuery; assertThat(q.fieldName(), equalTo(GEO_POINT_FIELD_NAME)); @@ -430,13 +429,14 @@ public class GeoDistanceQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(shardContext)); assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]")); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/GeoDistanceRangeQueryTests.java b/core/src/test/java/org/elasticsearch/index/query/GeoDistanceRangeQueryTests.java index ab50922e201..d9aef7270b3 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeoDistanceRangeQueryTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeoDistanceRangeQueryTests.java @@ -47,7 +47,7 @@ public class GeoDistanceRangeQueryTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]")); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java index dd91a0e5ead..55b7f24770c 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeoPolygonQueryBuilderTests.java @@ -19,8 +19,6 @@ package org.elasticsearch.index.query; -import org.locationtech.spatial4j.shape.jts.JtsGeometry; - import com.vividsolutions.jts.geom.Coordinate; import org.apache.lucene.search.MatchNoDocsQuery; @@ -36,6 +34,7 @@ import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.index.search.geo.GeoPolygonQuery; import org.elasticsearch.test.geo.RandomShapeGenerator; import org.elasticsearch.test.geo.RandomShapeGenerator.ShapeType; +import org.locationtech.spatial4j.shape.jts.JtsGeometry; import java.io.IOException; import java.util.ArrayList; @@ -364,13 +363,13 @@ public class GeoPolygonQueryBuilderTests extends AbstractQueryTestCase polygon = randomPolygon(randomIntBetween(4, 50)); final GeoPolygonQueryBuilder queryBuilder = new GeoPolygonQueryBuilder("unmapped", polygon); queryBuilder.ignoreUnmapped(true); - Query query = queryBuilder.toQuery(queryShardContext()); + Query query = queryBuilder.toQuery(createShardContext()); assertThat(query, notNullValue()); assertThat(query, instanceOf(MatchNoDocsQuery.class)); final GeoPolygonQueryBuilder failingQueryBuilder = new GeoPolygonQueryBuilder("unmapped", polygon); failingQueryBuilder.ignoreUnmapped(false); - QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]")); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java index b335cfabb6c..08f988c2a0b 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeoShapeQueryBuilderTests.java @@ -273,13 +273,13 @@ public class GeoShapeQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("failed to find geo_shape field [unmapped]")); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/GeohashCellQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/GeohashCellQueryBuilderTests.java index ff61a9b7cf1..1a7f4e362ac 100644 --- a/core/src/test/java/org/elasticsearch/index/query/GeohashCellQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/GeohashCellQueryBuilderTests.java @@ -19,8 +19,6 @@ package org.elasticsearch.index.query; -import org.locationtech.spatial4j.shape.Point; - import org.apache.lucene.index.Term; import org.apache.lucene.queries.TermsQuery; import org.apache.lucene.search.MatchNoDocsQuery; @@ -32,6 +30,7 @@ import org.elasticsearch.index.mapper.geo.BaseGeoPointFieldMapper; import org.elasticsearch.index.mapper.geo.GeoPointFieldMapper; import org.elasticsearch.index.query.GeohashCellQuery.Builder; import org.elasticsearch.test.geo.RandomShapeGenerator; +import org.locationtech.spatial4j.shape.Point; import java.io.IOException; @@ -163,13 +162,13 @@ public class GeohashCellQueryBuilderTests extends AbstractQueryTestCase public void testIgnoreUnmapped() throws IOException { final GeohashCellQuery.Builder queryBuilder = new GeohashCellQuery.Builder("unmapped", "c"); queryBuilder.ignoreUnmapped(true); - Query query = queryBuilder.toQuery(queryShardContext()); + Query query = queryBuilder.toQuery(createShardContext()); assertThat(query, notNullValue()); assertThat(query, instanceOf(MatchNoDocsQuery.class)); final GeohashCellQuery.Builder failingQueryBuilder = new GeohashCellQuery.Builder("unmapped", "c"); failingQueryBuilder.ignoreUnmapped(false); - QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("failed to parse [" + GeohashCellQuery.NAME + "] query. missing [" + BaseGeoPointFieldMapper.CONTENT_TYPE + "] field [unmapped]")); } diff --git a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java index 08a207940e0..1b8238a8cac 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java @@ -71,7 +71,7 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("[" + HasChildQueryBuilder.NAME + "] no mapping found for type [unmapped]")); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java index 7cd87f93fae..5f956581fc7 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasParentQueryBuilderTests.java @@ -58,7 +58,7 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("[" + HasParentQueryBuilder.NAME + "] query configured 'parent_type' [unmapped] is not a valid type")); } diff --git a/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java index 4c679879ad5..c8a3914fa13 100644 --- a/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/NestedQueryBuilderTests.java @@ -29,26 +29,26 @@ import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest; import org.elasticsearch.common.compress.CompressedXContent; import org.elasticsearch.index.fielddata.IndexFieldDataService; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.query.support.InnerHitBuilder; +import org.elasticsearch.search.fetch.innerhits.InnerHitsContext; import org.elasticsearch.search.internal.SearchContext; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.test.TestSearchContext; -import org.elasticsearch.index.query.support.InnerHitBuilder; -import org.elasticsearch.search.fetch.innerhits.InnerHitsContext; import java.io.IOException; -import static org.hamcrest.CoreMatchers.instanceOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.containsString; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.CoreMatchers.notNullValue; public class NestedQueryBuilderTests extends AbstractQueryTestCase { @Override public void setUp() throws Exception { super.setUp(); - MapperService mapperService = queryShardContext().getMapperService(); + MapperService mapperService = createShardContext().getMapperService(); mapperService.merge("nested_doc", new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef("nested_doc", STRING_FIELD_NAME, "type=text", INT_FIELD_NAME, "type=integer", @@ -63,9 +63,9 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); + IllegalStateException e = expectThrows(IllegalStateException.class, () -> failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("[" + NestedQueryBuilder.NAME + "] failed to find nested object under path [unmapped]")); } } diff --git a/core/src/test/java/org/elasticsearch/index/query/ParentIdQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/ParentIdQueryBuilderTests.java index 20e8c1efa62..b6ded9ada8a 100644 --- a/core/src/test/java/org/elasticsearch/index/query/ParentIdQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/ParentIdQueryBuilderTests.java @@ -47,7 +47,7 @@ public class ParentIdQueryBuilderTests extends AbstractQueryTestCase failingQueryBuilder.toQuery(queryShardContext())); + QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext())); assertThat(e.getMessage(), containsString("[" + ParentIdQueryBuilder.NAME + "] no mapping found for type [unmapped]")); } diff --git a/core/src/test/java/org/elasticsearch/index/query/PercolatorQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/PercolatorQueryBuilderTests.java index b4e0775f0ed..4009d37a0a3 100644 --- a/core/src/test/java/org/elasticsearch/index/query/PercolatorQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/PercolatorQueryBuilderTests.java @@ -121,12 +121,12 @@ public class PercolatorQueryBuilderTests extends AbstractQueryTestCase rewrite = pqb.rewrite(queryShardContext()); + QueryBuilder rewrite = pqb.rewrite(createShardContext()); PercolatorQueryBuilder geoShapeQueryBuilder = new PercolatorQueryBuilder(pqb.getDocumentType(), documentSource); assertEquals(geoShapeQueryBuilder, rewrite); } @@ -135,7 +135,7 @@ public class PercolatorQueryBuilderTests extends AbstractQueryTestCase rewritten = query.rewrite(queryShardContext); assertThat(rewritten, instanceOf(RangeQueryBuilder.class)); RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten; @@ -474,6 +475,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase rewritten = query.rewrite(queryShardContext); assertThat(rewritten, instanceOf(MatchNoneQueryBuilder.class)); } @@ -490,6 +492,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase rewritten = query.rewrite(queryShardContext); assertThat(rewritten, sameInstance(query)); } @@ -506,11 +509,12 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase rewritten = query.rewrite(queryShardContext); assertThat(rewritten, sameInstance(query)); } diff --git a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTests.java index 9426ef1f631..a6dc42a266e 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/TemplateQueryBuilderTests.java @@ -24,7 +24,6 @@ import org.apache.lucene.search.MatchAllDocsQuery; import org.apache.lucene.search.Query; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.script.Script.ScriptParseException; import org.elasticsearch.script.ScriptService.ScriptType; @@ -124,35 +123,35 @@ public class TemplateQueryBuilderTests extends AbstractQueryTestCase builder = new TemplateQueryBuilder(new Template(query, ScriptType.INLINE, "mockscript", XContentType.JSON, Collections.emptyMap())); try { - builder.toQuery(queryShardContext()); + builder.toQuery(createShardContext()); fail(); } catch (UnsupportedOperationException ex) { assertEquals("this query must be rewritten first", ex.getMessage()); } - assertEquals(new MatchAllQueryBuilder(), builder.rewrite(queryShardContext())); + assertEquals(new MatchAllQueryBuilder(), builder.rewrite(createShardContext())); } public void testRewriteWithInnerName() throws IOException { final String query = "{ \"match_all\" : {\"_name\" : \"foobar\"}}"; QueryBuilder builder = new TemplateQueryBuilder(new Template(query, ScriptType.INLINE, "mockscript", XContentType.JSON, Collections.emptyMap())); - assertEquals(new MatchAllQueryBuilder().queryName("foobar"), builder.rewrite(queryShardContext())); + assertEquals(new MatchAllQueryBuilder().queryName("foobar"), builder.rewrite(createShardContext())); builder = new TemplateQueryBuilder(new Template(query, ScriptType.INLINE, "mockscript", XContentType.JSON, Collections.emptyMap())).queryName("outer"); assertEquals(new BoolQueryBuilder().must(new MatchAllQueryBuilder().queryName("foobar")).queryName("outer"), - builder.rewrite(queryShardContext())); + builder.rewrite(createShardContext())); } public void testRewriteWithInnerBoost() throws IOException { final TermQueryBuilder query = new TermQueryBuilder("foo", "bar").boost(2); QueryBuilder builder = new TemplateQueryBuilder(new Template(query.toString(), ScriptType.INLINE, "mockscript", XContentType.JSON, Collections.emptyMap())); - assertEquals(query, builder.rewrite(queryShardContext())); + assertEquals(query, builder.rewrite(createShardContext())); builder = new TemplateQueryBuilder(new Template(query.toString(), ScriptType.INLINE, "mockscript", XContentType.JSON, Collections.emptyMap())).boost(3); - assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(queryShardContext())); + assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(createShardContext())); } @Override diff --git a/core/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java index fb571dd63fd..108714e4981 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/TermsQueryBuilderTests.java @@ -295,12 +295,12 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase x != null).collect(Collectors.toList()))); // terms lookup removes null values } } diff --git a/core/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java index ce8377439fc..db9a4d8e7cd 100644 --- a/core/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/TypeQueryBuilderTests.java @@ -35,7 +35,7 @@ public class TypeQueryBuilderTests extends AbstractQueryTestCase innerQuery = queryBuilder.rewrite(queryShardContext()); + QueryBuilder innerQuery = queryBuilder.rewrite(createShardContext()); Query expected = rewrite(innerQuery.toQuery(context)); assertEquals(rewrite(query), expected); } @@ -133,29 +133,31 @@ public class WrapperQueryBuilderTests extends AbstractQueryTestCase rewrite = qb.rewrite(queryShardContext()); + QueryBuilder rewrite = qb.rewrite(createShardContext()); assertEquals(tqb, rewrite); } public void testRewriteWithInnerName() throws IOException { QueryBuilder builder = new WrapperQueryBuilder("{ \"match_all\" : {\"_name\" : \"foobar\"}}"); - assertEquals(new MatchAllQueryBuilder().queryName("foobar"), builder.rewrite(queryShardContext())); + QueryShardContext shardContext = createShardContext(); + assertEquals(new MatchAllQueryBuilder().queryName("foobar"), builder.rewrite(shardContext)); builder = new WrapperQueryBuilder("{ \"match_all\" : {\"_name\" : \"foobar\"}}").queryName("outer"); assertEquals(new BoolQueryBuilder().must(new MatchAllQueryBuilder().queryName("foobar")).queryName("outer"), - builder.rewrite(queryShardContext())); + builder.rewrite(shardContext)); } public void testRewriteWithInnerBoost() throws IOException { final TermQueryBuilder query = new TermQueryBuilder("foo", "bar").boost(2); QueryBuilder builder = new WrapperQueryBuilder(query.toString()); - assertEquals(query, builder.rewrite(queryShardContext())); + QueryShardContext shardContext = createShardContext(); + assertEquals(query, builder.rewrite(shardContext)); builder = new WrapperQueryBuilder(query.toString()).boost(3); - assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(queryShardContext())); + assertEquals(new BoolQueryBuilder().must(query).boost(3), builder.rewrite(shardContext)); } @Override diff --git a/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java b/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java index ed5ee94c8ef..31b1b4ecd11 100644 --- a/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/functionscore/FunctionScoreQueryBuilderTests.java @@ -613,7 +613,7 @@ public class FunctionScoreQueryBuilderTests extends AbstractQueryTestCase