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.
This commit is contained in:
Christoph Büscher 2016-04-15 17:47:19 +02:00
parent fbd558382d
commit 4e77adf38e
20 changed files with 112 additions and 107 deletions

View File

@ -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<QB extends AbstractQueryBuilder<QB>>
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<QB extends AbstractQueryBuilder<QB>>
@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<QB extends AbstractQueryBuilder<QB>>
Set<ScriptEngineService> engines = new HashSet<>();
engines.add(mockScriptEngine);
List<ScriptContext.Plugin> 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<QB extends AbstractQueryBuilder<QB>>
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<QB extends AbstractQueryBuilder<QB>>
});
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++) {

View File

@ -366,7 +366,7 @@ public class BoolQueryBuilderTests extends AbstractQueryTestCase<BoolQueryBuilde
if (mustRewrite == false && randomBoolean()) {
boolQueryBuilder.must(new TermsQueryBuilder("foo", "no_rewrite"));
}
QueryBuilder<?> 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<BoolQueryBuilde
public void testRewriteMultipleTimes() throws IOException {
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
boolQueryBuilder.must(new WrapperQueryBuilder(new WrapperQueryBuilder(new MatchAllQueryBuilder().toString()).toString()));
QueryBuilder<?> 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);
}
}

View File

@ -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<BoostingQue
QueryBuilder positive = randomBoolean() ? new MatchAllQueryBuilder() : new WrapperQueryBuilder(new TermQueryBuilder("pos", "bar").toString());
QueryBuilder negative = randomBoolean() ? new MatchAllQueryBuilder() : new WrapperQueryBuilder(new TermQueryBuilder("neg", "bar").toString());
BoostingQueryBuilder qb = new BoostingQueryBuilder(positive, negative);
QueryBuilder<?> 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);
}
}
}

View File

@ -439,8 +439,9 @@ public class GeoBoundingBoxQueryBuilderTests extends AbstractQueryTestCase<GeoBo
}
private void assertGeoBoundingBoxQuery(String query) throws IOException {
Query parsedQuery = parseQuery(query).toQuery(createShardContext());
if (queryShardContext().indexVersionCreated().before(Version.V_2_2_0)) {
QueryShardContext shardContext = createShardContext();
Query parsedQuery = parseQuery(query).toQuery(shardContext);
if (shardContext.indexVersionCreated().before(Version.V_2_2_0)) {
InMemoryGeoBoundingBoxQuery filter = (InMemoryGeoBoundingBoxQuery) parsedQuery;
assertThat(filter.fieldName(), equalTo(GEO_POINT_FIELD_NAME));
assertThat(filter.topLeft().lat(), closeTo(40, 1E-5));
@ -513,13 +514,14 @@ public class GeoBoundingBoxQueryBuilderTests extends AbstractQueryTestCase<GeoBo
public void testIgnoreUnmapped() throws IOException {
final GeoBoundingBoxQueryBuilder queryBuilder = new GeoBoundingBoxQueryBuilder("unmapped").setCorners(1.0, 0.0, 0.0, 1.0);
queryBuilder.ignoreUnmapped(true);
Query query = queryBuilder.toQuery(queryShardContext());
QueryShardContext shardContext = createShardContext();
Query query = queryBuilder.toQuery(shardContext);
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final GeoBoundingBoxQueryBuilder failingQueryBuilder = new GeoBoundingBoxQueryBuilder("unmapped").setCorners(1.0, 0.0, 0.0, 1.0);
failingQueryBuilder.ignoreUnmapped(false);
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(shardContext));
assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]"));
}
}

View File

@ -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<GeoDista
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());
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<GeoDista
public void testIgnoreUnmapped() throws IOException {
final GeoDistanceQueryBuilder queryBuilder = new GeoDistanceQueryBuilder("unmapped").point(0.0, 0.0).distance("20m");
queryBuilder.ignoreUnmapped(true);
Query query = queryBuilder.toQuery(queryShardContext());
QueryShardContext shardContext = createShardContext();
Query query = queryBuilder.toQuery(shardContext);
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final GeoDistanceQueryBuilder failingQueryBuilder = new GeoDistanceQueryBuilder("unmapped").point(0.0, 0.0).distance("20m");
failingQueryBuilder.ignoreUnmapped(false);
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(shardContext));
assertThat(e.getMessage(), containsString("failed to find geo_point field [unmapped]"));
}
}

View File

@ -47,7 +47,7 @@ public class GeoDistanceRangeQueryTests extends AbstractQueryTestCase<GeoDistanc
@Override
protected GeoDistanceRangeQueryBuilder doCreateTestQueryBuilder() {
Version version = queryShardContext().indexVersionCreated();
Version version = createShardContext().indexVersionCreated();
GeoDistanceRangeQueryBuilder builder;
GeoPoint randomPoint = RandomGeoGenerator.randomPointIn(random(), -180.0, -89.9, 180.0, 89.9);
if (randomBoolean()) {
@ -307,7 +307,7 @@ public class GeoDistanceRangeQueryTests extends AbstractQueryTestCase<GeoDistanc
public void testNestedRangeQuery() throws IOException {
// create a nested geo_point type with a subfield named "geohash" (explicit testing for ISSUE #15179)
MapperService mapperService = queryShardContext().getMapperService();
MapperService mapperService = createShardContext().getMapperService();
String nestedMapping =
"{\"nested_doc\" : {\"properties\" : {" +
"\"locations\": {\"properties\": {" +
@ -366,14 +366,14 @@ public class GeoDistanceRangeQueryTests extends AbstractQueryTestCase<GeoDistanc
public void testIgnoreUnmapped() throws IOException {
final GeoDistanceRangeQueryBuilder queryBuilder = new GeoDistanceRangeQueryBuilder("unmapped", new GeoPoint(0.0, 0.0)).from("20m");
queryBuilder.ignoreUnmapped(true);
Query query = queryBuilder.toQuery(queryShardContext());
Query query = queryBuilder.toQuery(createShardContext());
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final GeoDistanceRangeQueryBuilder failingQueryBuilder = new GeoDistanceRangeQueryBuilder("unmapped", new GeoPoint(0.0, 0.0))
.from("20m");
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]"));
}
}

View File

@ -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<GeoPolygo
List<GeoPoint> 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]"));
}
}

View File

@ -273,13 +273,13 @@ public class GeoShapeQueryBuilderTests extends AbstractQueryTestCase<GeoShapeQue
ShapeBuilder shape = RandomShapeGenerator.createShapeWithin(random(), null, shapeType);
final GeoShapeQueryBuilder queryBuilder = new GeoShapeQueryBuilder("unmapped", shape);
queryBuilder.ignoreUnmapped(true);
Query query = queryBuilder.toQuery(queryShardContext());
Query query = queryBuilder.toQuery(createShardContext());
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final GeoShapeQueryBuilder failingQueryBuilder = new GeoShapeQueryBuilder("unmapped", shape);
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_shape field [unmapped]"));
}
}

View File

@ -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<Builder>
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]"));
}

View File

@ -71,7 +71,7 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
@BeforeClass
public static void before() throws Exception {
similarity = randomFrom("classic", "BM25");
MapperService mapperService = queryShardContext().getMapperService();
MapperService mapperService = createShardContext().getMapperService();
mapperService.merge(PARENT_TYPE, new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef(PARENT_TYPE,
STRING_FIELD_NAME, "type=text",
STRING_FIELD_NAME_2, "type=keyword",
@ -95,9 +95,9 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
@Override
protected void setSearchContext(String[] types) {
final MapperService mapperService = queryShardContext().getMapperService();
final MapperService mapperService = createShardContext().getMapperService();
final IndexFieldDataService fieldData = indexFieldDataService();
TestSearchContext testSearchContext = new TestSearchContext(queryShardContext()) {
TestSearchContext testSearchContext = new TestSearchContext(createShardContext()) {
@Override
public MapperService mapperService() {
@ -380,13 +380,13 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase<HasChildQue
public void testIgnoreUnmapped() throws IOException {
final HasChildQueryBuilder queryBuilder = new HasChildQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
queryBuilder.ignoreUnmapped(true);
Query query = queryBuilder.toQuery(queryShardContext());
Query query = queryBuilder.toQuery(createShardContext());
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final HasChildQueryBuilder failingQueryBuilder = new HasChildQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
failingQueryBuilder.ignoreUnmapped(false);
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext()));
assertThat(e.getMessage(), containsString("[" + HasChildQueryBuilder.NAME + "] no mapping found for type [unmapped]"));
}
}

View File

@ -58,7 +58,7 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
@BeforeClass
public static void beforeClass() throws Exception {
MapperService mapperService = queryShardContext().getMapperService();
MapperService mapperService = createShardContext().getMapperService();
mapperService.merge(PARENT_TYPE, new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef(PARENT_TYPE,
STRING_FIELD_NAME, "type=text",
STRING_FIELD_NAME_2, "type=keyword",
@ -84,9 +84,9 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
@Override
protected void setSearchContext(String[] types) {
final MapperService mapperService = queryShardContext().getMapperService();
final MapperService mapperService = createShardContext().getMapperService();
final IndexFieldDataService fieldData = indexFieldDataService();
TestSearchContext testSearchContext = new TestSearchContext(queryShardContext()) {
TestSearchContext testSearchContext = new TestSearchContext(createShardContext()) {
@Override
public MapperService mapperService() {
@ -271,13 +271,13 @@ public class HasParentQueryBuilderTests extends AbstractQueryTestCase<HasParentQ
public void testIgnoreUnmapped() throws IOException {
final HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder("unmapped", new MatchAllQueryBuilder(), false);
queryBuilder.ignoreUnmapped(true);
Query query = queryBuilder.toQuery(queryShardContext());
Query query = queryBuilder.toQuery(createShardContext());
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final HasParentQueryBuilder failingQueryBuilder = new HasParentQueryBuilder("unmapped", new MatchAllQueryBuilder(), false);
failingQueryBuilder.ignoreUnmapped(false);
QueryShardException e = expectThrows(QueryShardException.class, () -> 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"));
}

View File

@ -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<NestedQueryBuilder> {
@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<NestedQueryBu
@Override
protected void setSearchContext(String[] types) {
final MapperService mapperService = queryShardContext().getMapperService();
final MapperService mapperService = createShardContext().getMapperService();
final IndexFieldDataService fieldData = indexFieldDataService();
TestSearchContext testSearchContext = new TestSearchContext(queryShardContext()) {
TestSearchContext testSearchContext = new TestSearchContext(createShardContext()) {
@Override
public MapperService mapperService() {
@ -188,13 +188,13 @@ public class NestedQueryBuilderTests extends AbstractQueryTestCase<NestedQueryBu
public void testIgnoreUnmapped() throws IOException {
final NestedQueryBuilder queryBuilder = new NestedQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
queryBuilder.ignoreUnmapped(true);
Query query = queryBuilder.toQuery(queryShardContext());
Query query = queryBuilder.toQuery(createShardContext());
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final NestedQueryBuilder failingQueryBuilder = new NestedQueryBuilder("unmapped", new MatchAllQueryBuilder(), ScoreMode.None);
failingQueryBuilder.ignoreUnmapped(false);
IllegalStateException e = expectThrows(IllegalStateException.class, () -> 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]"));
}
}

View File

@ -47,7 +47,7 @@ public class ParentIdQueryBuilderTests extends AbstractQueryTestCase<ParentIdQue
@Override
public void setUp() throws Exception {
super.setUp();
MapperService mapperService = queryShardContext().getMapperService();
MapperService mapperService = createShardContext().getMapperService();
mapperService.merge(PARENT_TYPE, new CompressedXContent(PutMappingRequest.buildFromSimplifiedDef(PARENT_TYPE,
STRING_FIELD_NAME, "type=text",
INT_FIELD_NAME, "type=integer",
@ -69,9 +69,9 @@ public class ParentIdQueryBuilderTests extends AbstractQueryTestCase<ParentIdQue
@Override
protected void setSearchContext(String[] types) {
final MapperService mapperService = queryShardContext().getMapperService();
final MapperService mapperService = createShardContext().getMapperService();
final IndexFieldDataService fieldData = indexFieldDataService();
TestSearchContext testSearchContext = new TestSearchContext(queryShardContext()) {
TestSearchContext testSearchContext = new TestSearchContext(createShardContext()) {
@Override
public MapperService mapperService() {
@ -129,13 +129,13 @@ public class ParentIdQueryBuilderTests extends AbstractQueryTestCase<ParentIdQue
public void testIgnoreUnmapped() throws IOException {
final ParentIdQueryBuilder queryBuilder = new ParentIdQueryBuilder("unmapped", "foo");
queryBuilder.ignoreUnmapped(true);
Query query = queryBuilder.toQuery(queryShardContext());
Query query = queryBuilder.toQuery(createShardContext());
assertThat(query, notNullValue());
assertThat(query, instanceOf(MatchNoDocsQuery.class));
final ParentIdQueryBuilder failingQueryBuilder = new ParentIdQueryBuilder("unmapped", "foo");
failingQueryBuilder.ignoreUnmapped(false);
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(queryShardContext()));
QueryShardException e = expectThrows(QueryShardException.class, () -> failingQueryBuilder.toQuery(createShardContext()));
assertThat(e.getMessage(), containsString("[" + ParentIdQueryBuilder.NAME + "] no mapping found for type [unmapped]"));
}

View File

@ -121,12 +121,12 @@ public class PercolatorQueryBuilderTests extends AbstractQueryTestCase<Percolato
public void testMustRewrite() throws IOException {
PercolatorQueryBuilder pqb = doCreateTestQueryBuilder(true);
try {
pqb.toQuery(queryShardContext());
pqb.toQuery(createShardContext());
fail("IllegalStateException expected");
} catch (IllegalStateException e) {
assertThat(e.getMessage(), equalTo("query builder must be rewritten first"));
}
QueryBuilder<?> 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<Percolato
indexedDocumentExists = false;
PercolatorQueryBuilder pqb = doCreateTestQueryBuilder(true);
try {
pqb.rewrite(queryShardContext());
pqb.rewrite(createShardContext());
fail("ResourceNotFoundException expected");
} catch (ResourceNotFoundException e) {
String expectedString = "indexed document [" + indexedDocumentIndex + "/" + indexedDocumentType + "/" +

View File

@ -452,6 +452,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
public void testRewriteDateToMatchAll() throws IOException {
String fieldName = randomAsciiOfLengthBetween(1, 20);
RangeQueryBuilder query = new RangeQueryBuilder(fieldName) {
@Override
protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException {
return Relation.WITHIN;
}
@ -462,7 +463,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
DateTime shardMaxValue = new DateTime(2015, 9, 1, 0, 0, 0, ISOChronology.getInstanceUTC());
query.from(queryFromValue);
query.to(queryToValue);
QueryShardContext queryShardContext = queryShardContext();
QueryShardContext queryShardContext = createShardContext();
QueryBuilder<?> rewritten = query.rewrite(queryShardContext);
assertThat(rewritten, instanceOf(RangeQueryBuilder.class));
RangeQueryBuilder rewrittenRange = (RangeQueryBuilder) rewritten;
@ -474,6 +475,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
public void testRewriteDateToMatchNone() throws IOException {
String fieldName = randomAsciiOfLengthBetween(1, 20);
RangeQueryBuilder query = new RangeQueryBuilder(fieldName) {
@Override
protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException {
return Relation.DISJOINT;
}
@ -482,7 +484,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
DateTime queryToValue = new DateTime(2016, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC());
query.from(queryFromValue);
query.to(queryToValue);
QueryShardContext queryShardContext = queryShardContext();
QueryShardContext queryShardContext = createShardContext();
QueryBuilder<?> rewritten = query.rewrite(queryShardContext);
assertThat(rewritten, instanceOf(MatchNoneQueryBuilder.class));
}
@ -490,6 +492,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
public void testRewriteDateToSame() throws IOException {
String fieldName = randomAsciiOfLengthBetween(1, 20);
RangeQueryBuilder query = new RangeQueryBuilder(fieldName) {
@Override
protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException {
return Relation.INTERSECTS;
}
@ -498,7 +501,7 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
DateTime queryToValue = new DateTime(2016, 1, 1, 0, 0, 0, ISOChronology.getInstanceUTC());
query.from(queryFromValue);
query.to(queryToValue);
QueryShardContext queryShardContext = queryShardContext();
QueryShardContext queryShardContext = createShardContext();
QueryBuilder<?> rewritten = query.rewrite(queryShardContext);
assertThat(rewritten, sameInstance(query));
}
@ -506,11 +509,12 @@ public class RangeQueryBuilderTests extends AbstractQueryTestCase<RangeQueryBuil
public void testRewriteOpenBoundsToSame() throws IOException {
String fieldName = randomAsciiOfLengthBetween(1, 20);
RangeQueryBuilder query = new RangeQueryBuilder(fieldName) {
@Override
protected MappedFieldType.Relation getRelation(QueryRewriteContext queryRewriteContext) throws IOException {
return Relation.INTERSECTS;
}
};
QueryShardContext queryShardContext = queryShardContext();
QueryShardContext queryShardContext = createShardContext();
QueryBuilder<?> rewritten = query.rewrite(queryShardContext);
assertThat(rewritten, sameInstance(query));
}

View File

@ -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<TemplateQue
QueryBuilder<?> 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

View File

@ -295,12 +295,12 @@ public class TermsQueryBuilderTests extends AbstractQueryTestCase<TermsQueryBuil
public void testMustRewrite() throws IOException {
TermsQueryBuilder termsQueryBuilder = new TermsQueryBuilder(STRING_FIELD_NAME, randomTermsLookup());
try {
termsQueryBuilder.toQuery(queryShardContext());
termsQueryBuilder.toQuery(createShardContext());
fail();
} catch (UnsupportedOperationException ex) {
assertEquals("query must be rewritten first", ex.getMessage());
}
assertEquals(termsQueryBuilder.rewrite(queryShardContext()), new TermsQueryBuilder(STRING_FIELD_NAME,
assertEquals(termsQueryBuilder.rewrite(createShardContext()), new TermsQueryBuilder(STRING_FIELD_NAME,
randomTerms.stream().filter(x -> x != null).collect(Collectors.toList()))); // terms lookup removes null values
}
}

View File

@ -35,7 +35,7 @@ public class TypeQueryBuilderTests extends AbstractQueryTestCase<TypeQueryBuilde
@Override
protected void doAssertLuceneQuery(TypeQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
if (queryShardContext().getMapperService().documentMapper(queryBuilder.type()) == null) {
if (createShardContext().getMapperService().documentMapper(queryBuilder.type()) == null) {
assertEquals(new MatchNoDocsQuery(), query);
} else {
assertEquals(new TypeFieldMapper.TypeQuery(new BytesRef(queryBuilder.type())), query);

View File

@ -54,7 +54,7 @@ public class WrapperQueryBuilderTests extends AbstractQueryTestCase<WrapperQuery
@Override
protected void doAssertLuceneQuery(WrapperQueryBuilder queryBuilder, Query query, QueryShardContext context) throws IOException {
QueryBuilder<?> 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<WrapperQuery
TermQueryBuilder tqb = new TermQueryBuilder("foo", "bar");
WrapperQueryBuilder qb = new WrapperQueryBuilder(tqb.toString());
try {
qb.toQuery(queryShardContext());
qb.toQuery(createShardContext());
fail();
} catch (UnsupportedOperationException e) {
assertEquals("this query must be rewritten first", e.getMessage());
}
QueryBuilder<?> 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

View File

@ -613,7 +613,7 @@ public class FunctionScoreQueryBuilderTests extends AbstractQueryTestCase<Functi
public void testRewrite() throws IOException {
FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(
new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString()));
FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(queryShardContext());
FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(createShardContext());
assertNotSame(functionScoreQueryBuilder, rewrite);
assertEquals(rewrite.query(), new TermQueryBuilder("foo", "bar"));
}
@ -627,7 +627,7 @@ public class FunctionScoreQueryBuilderTests extends AbstractQueryTestCase<Functi
new FunctionScoreQueryBuilder.FilterFunctionBuilder[] {
new FunctionScoreQueryBuilder.FilterFunctionBuilder(firstFunction, new RandomScoreFunctionBuilder()),
new FunctionScoreQueryBuilder.FilterFunctionBuilder(secondFunction, new RandomScoreFunctionBuilder()) });
FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(queryShardContext());
FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(createShardContext());
assertNotSame(functionScoreQueryBuilder, rewrite);
assertEquals(rewrite.query(), new TermQueryBuilder("foo", "bar"));
assertEquals(rewrite.filterFunctionBuilders()[0].getFilter(), new TermQueryBuilder("tq", "1"));