From 96bcb47fc9260d46bed5aeeecbdd7e71cf0b5c6b Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Mon, 8 Feb 2016 12:09:15 +0100 Subject: [PATCH] Detach QueryShardContext from IndexShard and remove obsolete threadlocals IndexShard currently holds an arbitraritly used `getQueryShardContext` that comes out of a ThreadLocal. It's usage is undefined and arbitraty since there is also such a method with different semantics on `IndexService` This commit removes the threadLocal on IndexShard as well as on the context itself. It's types are now a member and the QueryShardContext lifecycle is managed byt SearchContext which passes the types on from the SearchRequest. --- .../query/TransportValidateQueryAction.java | 3 +- .../explain/TransportExplainAction.java | 2 +- .../metadata/MetaDataCreateIndexService.java | 3 +- .../metadata/MetaDataIndexAliasesService.java | 2 +- .../org/elasticsearch/index/IndexService.java | 7 +- .../elasticsearch/index/SearchSlowLog.java | 4 +- .../index/mapper/DocumentMapperParser.java | 1 - .../index/query/HasChildQueryBuilder.java | 7 +- .../index/query/HasParentQueryBuilder.java | 6 +- .../index/query/QueryShardContext.java | 22 ++--- .../support/NestedInnerQueryParseSupport.java | 2 +- .../elasticsearch/index/shard/IndexShard.java | 27 +----- .../percolator/PercolateContext.java | 46 ++++------- .../percolator/PercolateDocumentParser.java | 9 +- .../percolator/PercolatorService.java | 9 +- .../elasticsearch/search/SearchService.java | 7 +- .../search/aggregations/AggregationPhase.java | 2 +- .../bucket/filter/FilterParser.java | 2 +- .../bucket/filters/FiltersParser.java | 4 +- .../SignificantTermsParametersParser.java | 2 +- .../innerhits/InnerHitsParseElement.java | 2 +- .../highlight/HighlighterParseElement.java | 2 +- .../search/internal/DefaultSearchContext.java | 25 +++--- .../internal/FilteredSearchContext.java | 15 ++-- .../search/internal/SearchContext.java | 8 +- .../search/lookup/DocLookup.java | 4 + .../search/query/PostFilterParseElement.java | 4 +- .../search/query/QueryParseElement.java | 4 +- .../search/rescore/RescoreParseElement.java | 4 +- .../suggest/phrase/PhraseSuggester.java | 12 ++- .../phrase/PhraseSuggestionContext.java | 1 - .../org/elasticsearch/ESExceptionTests.java | 2 +- .../ExceptionSerializationTests.java | 2 +- .../index/IndexServiceTests.java | 25 +++--- .../mapper/date/SimpleDateMappingTests.java | 4 +- .../SimpleExternalMappingTests.java | 6 +- .../internal/FieldNamesFieldMapperTests.java | 4 +- .../index/query/AbstractQueryTestCase.java | 5 +- .../query/HasChildQueryBuilderTests.java | 13 +-- .../query/HasParentQueryBuilderTests.java | 11 +-- .../index/query/NestedQueryBuilderTests.java | 82 +++++++++---------- .../query/ParentIdQueryBuilderTests.java | 4 +- .../query/plugin/CustomQueryParserIT.java | 2 +- .../index/search/MultiMatchQueryTests.java | 2 +- .../percolator/PercolatorIT.java | 7 +- .../geogrid/GeoHashGridParserTests.java | 12 +-- .../SignificanceHeuristicTests.java | 5 ++ .../search/fetch/FieldDataFieldsTests.java | 6 +- .../search/query/QueryPhaseTests.java | 6 +- .../search/sort/SortParserTests.java | 2 +- .../murmur3/Murmur3FieldMapperTests.java | 6 +- .../index/mapper/size/SizeMappingTests.java | 4 +- .../elasticsearch/test/TestSearchContext.java | 31 +++---- 53 files changed, 215 insertions(+), 274 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java b/core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java index 72cbe37c919..00f34f486e8 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/validate/query/TransportValidateQueryAction.java @@ -164,7 +164,8 @@ public class TransportValidateQueryAction extends TransportBroadcastAction queryShardContextSupplier; diff --git a/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java b/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java index 3589215d5f8..c84883fe737 100644 --- a/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java +++ b/core/src/main/java/org/elasticsearch/index/query/HasChildQueryBuilder.java @@ -26,6 +26,7 @@ import org.apache.lucene.search.MatchNoDocsQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.join.JoinUtil; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.common.Strings; import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamOutput; import org.elasticsearch.common.lucene.search.Queries; @@ -205,13 +206,15 @@ public class HasChildQueryBuilder extends AbstractQueryBuilder typesContext = new ThreadLocal<>(); private final MapperService mapperService; private final ScriptService scriptService; private final SimilarityService similarityService; @@ -82,23 +82,14 @@ public class QueryShardContext { private final IndexFieldDataService indexFieldDataService; private final IndexSettings indexSettings; private final Client client; + private String[] types = Strings.EMPTY_ARRAY; - public static void setTypes(String[] types) { - typesContext.set(types); + public void setTypes(String... types) { + this.types = types; } - public static String[] getTypes() { - return typesContext.get(); - } - - public static String[] setTypesWithPrevious(String... types) { - String[] old = typesContext.get(); - setTypes(types); - return old; - } - - public static void removeTypes() { - typesContext.remove(); + public String[] getTypes() { + return types; } private final Map namedQueries = new HashMap<>(); @@ -126,6 +117,7 @@ public class QueryShardContext { public QueryShardContext(QueryShardContext source) { this(source.indexSettings, source.client, source.bitsetFilterCache, source.indexFieldDataService, source.mapperService, source.similarityService, source.scriptService, source.indicesQueriesRegistry); + this.types = source.getTypes(); } diff --git a/core/src/main/java/org/elasticsearch/index/query/support/NestedInnerQueryParseSupport.java b/core/src/main/java/org/elasticsearch/index/query/support/NestedInnerQueryParseSupport.java index 8504ff5ccec..890961dd2a2 100644 --- a/core/src/main/java/org/elasticsearch/index/query/support/NestedInnerQueryParseSupport.java +++ b/core/src/main/java/org/elasticsearch/index/query/support/NestedInnerQueryParseSupport.java @@ -61,7 +61,7 @@ public class NestedInnerQueryParseSupport { private ObjectMapper parentObjectMapper; public NestedInnerQueryParseSupport(XContentParser parser, SearchContext searchContext) { - shardContext = searchContext.indexShard().getQueryShardContext(); + shardContext = searchContext.getQueryShardContext(); parseContext = shardContext.parseContext(); shardContext.reset(parser); diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java index 705d4d5aa5a..0ca8ea6926f 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -27,7 +27,6 @@ import org.apache.lucene.index.Term; import org.apache.lucene.search.QueryCachingPolicy; import org.apache.lucene.search.UsageTrackingQueryCachingPolicy; import org.apache.lucene.store.AlreadyClosedException; -import org.apache.lucene.util.CloseableThreadLocal; import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.ThreadInterruptedException; import org.elasticsearch.ElasticsearchException; @@ -158,7 +157,6 @@ public class IndexShard extends AbstractIndexShardComponent { private final IndicesQueryCache indicesQueryCache; private final IndexEventListener indexEventListener; private final IndexSettings idxSettings; - private final NodeServicesProvider provider; /** How many bytes we are currently moving to disk, via either IndexWriter.flush or refresh. IndexingMemoryController polls this * across all shards to decide if throttling is necessary because moving bytes to disk is falling behind vs incoming documents @@ -253,9 +251,9 @@ public class IndexShard extends AbstractIndexShardComponent { this.engineConfig = newEngineConfig(translogConfig, cachingPolicy); this.suspendableRefContainer = new SuspendableRefContainer(); - this.provider = provider; this.searcherWrapper = indexSearcherWrapper; - this.percolatorQueriesRegistry = new PercolatorQueriesRegistry(shardId, indexSettings, newQueryShardContext()); + QueryShardContext queryShardContext = new QueryShardContext(idxSettings, provider.getClient(), indexCache.bitsetFilterCache(), indexFieldDataService, mapperService, similarityService, provider.getScriptService(), provider.getIndicesQueriesRegistry()); + this.percolatorQueriesRegistry = new PercolatorQueriesRegistry(shardId, indexSettings, queryShardContext); } public Store store() { @@ -814,7 +812,7 @@ public class IndexShard extends AbstractIndexShardComponent { engine.flushAndClose(); } } finally { // playing safe here and close the engine even if the above succeeds - close can be called multiple times - IOUtils.close(engine, percolatorQueriesRegistry, queryShardContextCache); + IOUtils.close(engine, percolatorQueriesRegistry); } } } @@ -1499,25 +1497,6 @@ public class IndexShard extends AbstractIndexShardComponent { } } - private CloseableThreadLocal queryShardContextCache = new CloseableThreadLocal() { - // TODO We should get rid of this threadlocal but I think it should be a sep change - @Override - protected QueryShardContext initialValue() { - return newQueryShardContext(); - } - }; - - private QueryShardContext newQueryShardContext() { - return new QueryShardContext(idxSettings, provider.getClient(), indexCache.bitsetFilterCache(), indexFieldDataService, mapperService, similarityService, provider.getScriptService(), provider.getIndicesQueriesRegistry()); - } - - /** - * Returns a threadlocal QueryShardContext for this shard. - */ - public QueryShardContext getQueryShardContext() { - return queryShardContextCache.get(); - } - EngineFactory getEngineFactory() { return engineFactory; } diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java index 2ce3236a527..4c7f22e3922 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.percolator; -import com.carrotsearch.hppc.ObjectObjectAssociativeContainer; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexableField; import org.apache.lucene.index.LeafReaderContext; @@ -32,7 +31,6 @@ import org.elasticsearch.action.percolate.PercolateShardRequest; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.cache.recycler.PageCacheRecycler; import org.elasticsearch.common.ParseFieldMatcher; -import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.lease.Releasables; import org.elasticsearch.common.text.Text; import org.elasticsearch.common.util.BigArrays; @@ -46,6 +44,7 @@ import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ParsedDocument; import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.ParsedQuery; +import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.ScriptService; @@ -73,11 +72,11 @@ import org.elasticsearch.search.query.QuerySearchResult; import org.elasticsearch.search.rescore.RescoreSearchContext; import org.elasticsearch.search.suggest.SuggestionSearchContext; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; /** */ @@ -99,14 +98,11 @@ public class PercolateContext extends SearchContext { private final long originNanoTime = System.nanoTime(); private final long startTime; private final boolean onlyCount; - private String[] types; - private Engine.Searcher docSearcher; private Engine.Searcher engineSearcher; private ContextIndexSearcher searcher; private SearchContextHighlight highlight; - private SearchLookup searchLookup; private ParsedQuery parsedQuery; private Query query; private Query percolateQuery; @@ -115,7 +111,9 @@ public class PercolateContext extends SearchContext { private QuerySearchResult querySearchResult; private Sort sort; private final Map subPhaseContexts = new HashMap<>(); + private final QueryShardContext queryShardContext; private final Map, Collector> queryCollectors = new HashMap<>(); + private SearchLookup searchLookup; public PercolateContext(PercolateShardRequest request, SearchShardTarget searchShardTarget, IndexShard indexShard, IndexService indexService, PageCacheRecycler pageCacheRecycler, @@ -126,7 +124,6 @@ public class PercolateContext extends SearchContext { this.fieldDataService = indexService.fieldData(); this.mapperService = indexService.mapperService(); this.searchShardTarget = searchShardTarget; - this.types = new String[]{request.documentType()}; this.pageCacheRecycler = pageCacheRecycler; this.bigArrays = bigArrays.withCircuitBreaking(); this.querySearchResult = new QuerySearchResult(0, searchShardTarget); @@ -137,6 +134,8 @@ public class PercolateContext extends SearchContext { this.aliasFilter = aliasFilter; this.startTime = request.getStartTime(); this.onlyCount = request.onlyCount(); + queryShardContext = indexService.newQueryShardContext(); + queryShardContext.setTypes(request.documentType()); } // for testing: @@ -154,6 +153,7 @@ public class PercolateContext extends SearchContext { this.startTime = 0; this.numberOfShards = 0; this.onlyCount = true; + queryShardContext = new QueryShardContext(mapperService.getIndexSettings(), null, null, null, mapperService, null, null, null); } public IndexSearcher docSearcher() { @@ -162,10 +162,10 @@ public class PercolateContext extends SearchContext { public void initialize(Engine.Searcher docSearcher, ParsedDocument parsedDocument) { this.docSearcher = docSearcher; - IndexReader indexReader = docSearcher.reader(); LeafReaderContext atomicReaderContext = indexReader.leaves().get(0); - LeafSearchLookup leafLookup = lookup().getLeafSearchLookup(atomicReaderContext); + this.searchLookup = new SearchLookup(mapperService(), fieldData(), queryShardContext.getTypes()); + LeafSearchLookup leafLookup = searchLookup.getLeafSearchLookup(atomicReaderContext); leafLookup.setDocument(0); leafLookup.source().setSource(parsedDocument.source()); @@ -232,10 +232,10 @@ public class PercolateContext extends SearchContext { @Override public SearchLookup lookup() { - if (searchLookup == null) { - searchLookup = new SearchLookup(mapperService(), fieldData(), types); - } - return searchLookup; + // we cache this since it's really just a single document lookup - check the init method for details + assert searchLookup != null : "context is not initialized"; + assert Arrays.equals(searchLookup.doc().getTypes(), getQueryShardContext().getTypes()) : "types mismatch - can't return lookup"; + return this.searchLookup; } @Override @@ -265,16 +265,6 @@ public class PercolateContext extends SearchContext { return query; } - @Override - public String[] types() { - return types; - } - - public void types(String[] types) { - this.types = types; - searchLookup = new SearchLookup(mapperService(), fieldData(), types); - } - @Override public IndexFieldDataService fieldData() { return fieldDataService; @@ -341,11 +331,6 @@ public class PercolateContext extends SearchContext { return numberOfShards; } - @Override - public boolean hasTypes() { - throw new UnsupportedOperationException(); - } - @Override public float queryBoost() { throw new UnsupportedOperationException(); @@ -684,6 +669,11 @@ public class PercolateContext extends SearchContext { return queryCollectors; } + @Override + public QueryShardContext getQueryShardContext() { + return queryShardContext; + } + @Override public Profilers getProfilers() { throw new UnsupportedOperationException(); diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolateDocumentParser.java b/core/src/main/java/org/elasticsearch/percolator/PercolateDocumentParser.java index 946d30edcc4..2192876266a 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolateDocumentParser.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolateDocumentParser.java @@ -23,8 +23,6 @@ import org.apache.lucene.search.ConstantScoreQuery; import org.apache.lucene.search.Query; import org.elasticsearch.ElasticsearchParseException; import org.elasticsearch.action.percolate.PercolateShardRequest; -import org.elasticsearch.cluster.action.index.MappingUpdatedAction; -import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.io.stream.BytesStreamOutput; @@ -35,7 +33,6 @@ import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.mapper.DocumentMapperForType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.ParsedDocument; -import org.elasticsearch.index.mapper.SourceToParse; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.search.SearchParseElement; import org.elasticsearch.search.aggregations.AggregationPhase; @@ -78,8 +75,8 @@ public class PercolateDocumentParser { // Some queries (function_score query when for decay functions) rely on a SearchContext being set: // We switch types because this context needs to be in the context of the percolate queries in the shard and // not the in memory percolate doc - String[] previousTypes = context.types(); - context.types(new String[]{PercolatorService.TYPE_NAME}); + String[] previousTypes = context.getQueryShardContext().getTypes(); + context.getQueryShardContext().setTypes(PercolatorService.TYPE_NAME); try (XContentParser parser = XContentFactory.xContent(source).createParser(source);) { String currentFieldName = null; XContentParser.Token token; @@ -176,7 +173,7 @@ public class PercolateDocumentParser { } catch (Throwable e) { throw new ElasticsearchParseException("failed to parse request", e); } finally { - context.types(previousTypes); + context.getQueryShardContext().setTypes(previousTypes); } if (request.docSource() != null && request.docSource().length() != 0) { diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolatorService.java b/core/src/main/java/org/elasticsearch/percolator/PercolatorService.java index 95cdff02ad9..c36de9c6e4c 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolatorService.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolatorService.java @@ -169,8 +169,8 @@ public class PercolatorService extends AbstractComponent implements Releasable { } public PercolateShardResponse percolate(PercolateShardRequest request) throws IOException { - IndexService percolateIndexService = indicesService.indexServiceSafe(request.shardId().getIndex()); - IndexShard indexShard = percolateIndexService.getShard(request.shardId().id()); + final IndexService percolateIndexService = indicesService.indexServiceSafe(request.shardId().getIndex()); + final IndexShard indexShard = percolateIndexService.getShard(request.shardId().id()); indexShard.readAllowed(); // check if we can read the shard... PercolatorQueriesRegistry percolateQueryRegistry = indexShard.percolateRegistry(); percolateQueryRegistry.prePercolate(); @@ -183,7 +183,7 @@ public class PercolatorService extends AbstractComponent implements Releasable { indexShard.shardId().getIndex().getName(), request.indices() ); - Query aliasFilter = percolateIndexService.aliasFilter(indexShard.getQueryShardContext(), filteringAliases); + Query aliasFilter = percolateIndexService.aliasFilter(percolateIndexService.newQueryShardContext(), filteringAliases); SearchShardTarget searchShardTarget = new SearchShardTarget(clusterService.localNode().id(), request.shardId().getIndex(), request.shardId().id()); final PercolateContext context = new PercolateContext( @@ -191,8 +191,7 @@ public class PercolatorService extends AbstractComponent implements Releasable { ); SearchContext.setCurrent(context); try { - ParsedDocument parsedDocument = percolateDocumentParser.parse(request, context, percolateIndexService.mapperService(), percolateIndexService.getQueryShardContext()); - + ParsedDocument parsedDocument = percolateDocumentParser.parse(request, context, percolateIndexService.mapperService(), context.getQueryShardContext()); if (context.searcher().getIndexReader().maxDoc() == 0) { return new PercolateShardResponse(Lucene.EMPTY_TOP_DOCS, Collections.emptyMap(), Collections.emptyMap(), context); } diff --git a/core/src/main/java/org/elasticsearch/search/SearchService.java b/core/src/main/java/org/elasticsearch/search/SearchService.java index 0da838a799b..e0b30a2e346 100644 --- a/core/src/main/java/org/elasticsearch/search/SearchService.java +++ b/core/src/main/java/org/elasticsearch/search/SearchService.java @@ -646,8 +646,7 @@ public class SearchService extends AbstractLifecycleComponent imp if (source == null) { return; } - final IndexShard indexShard = context.indexShard(); - QueryShardContext queryShardContext = indexShard.getQueryShardContext(); + QueryShardContext queryShardContext = context.getQueryShardContext(); context.from(source.from()); context.size(source.size()); ObjectFloatHashMap indexBoostMap = source.indexBoost(); @@ -751,7 +750,7 @@ public class SearchService extends AbstractLifecycleComponent imp if (source.rescores() != null) { try { for (RescoreBuilder rescore : source.rescores()) { - context.addRescore(rescore.build(context.indexShard().getQueryShardContext())); + context.addRescore(rescore.build(context.getQueryShardContext())); } } catch (IOException e) { throw new SearchContextException(context, "failed to create RescoreSearchContext", e); @@ -776,7 +775,7 @@ public class SearchService extends AbstractLifecycleComponent imp if (source.highlighter() != null) { HighlightBuilder highlightBuilder = source.highlighter(); try { - context.highlight(highlightBuilder.build(context.indexShard().getQueryShardContext())); + context.highlight(highlightBuilder.build(context.getQueryShardContext())); } catch (IOException e) { throw new SearchContextException(context, "failed to create SearchContextHighlighter", e); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java b/core/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java index 0681996e3ec..050ffa61ebe 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/AggregationPhase.java @@ -124,7 +124,7 @@ public class AggregationPhase implements SearchPhase { if (!globals.isEmpty()) { BucketCollector globalsCollector = BucketCollector.wrap(globals); Query query = Queries.newMatchAllQuery(); - Query searchFilter = context.searchFilter(context.types()); + Query searchFilter = context.searchFilter(context.getQueryShardContext().getTypes()); if (searchFilter != null) { BooleanQuery filtered = new BooleanQuery.Builder() diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterParser.java index 48702dab230..d532dc4c922 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filter/FilterParser.java @@ -39,7 +39,7 @@ public class FilterParser implements Aggregator.Parser { @Override public AggregatorFactory parse(String aggregationName, XContentParser parser, SearchContext context) throws IOException { - ParsedQuery filter = context.indexShard().getQueryShardContext().parseInnerFilter(parser); + ParsedQuery filter = context.getQueryShardContext().parseInnerFilter(parser); return new FilterAggregator.Factory(aggregationName, filter == null ? new MatchAllDocsQuery() : filter.query()); } diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersParser.java index 8ed37078012..b7e5e6ff35c 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/filters/FiltersParser.java @@ -82,7 +82,7 @@ public class FiltersParser implements Aggregator.Parser { if (token == XContentParser.Token.FIELD_NAME) { key = parser.currentName(); } else { - ParsedQuery filter = context.indexShard().getQueryShardContext().parseInnerFilter(parser); + ParsedQuery filter = context.getQueryShardContext().parseInnerFilter(parser); filters.add(new FiltersAggregator.KeyedFilter(key, filter == null ? Queries.newMatchAllQuery() : filter.query())); } } @@ -95,7 +95,7 @@ public class FiltersParser implements Aggregator.Parser { keyed = false; int idx = 0; while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - ParsedQuery filter = context.indexShard().getQueryShardContext().parseInnerFilter(parser); + ParsedQuery filter = context.getQueryShardContext().parseInnerFilter(parser); filters.add(new FiltersAggregator.KeyedFilter(String.valueOf(idx), filter == null ? Queries.newMatchAllQuery() : filter.query())); idx++; diff --git a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsParametersParser.java b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsParametersParser.java index 020229867f6..3019765e385 100644 --- a/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsParametersParser.java +++ b/core/src/main/java/org/elasticsearch/search/aggregations/bucket/significant/SignificantTermsParametersParser.java @@ -66,7 +66,7 @@ public class SignificantTermsParametersParser extends AbstractTermsParametersPar if (significanceHeuristicParser != null) { significanceHeuristic = significanceHeuristicParser.parse(parser, context.parseFieldMatcher(), context); } else if (context.parseFieldMatcher().match(currentFieldName, BACKGROUND_FILTER)) { - filter = context.indexShard().getQueryShardContext().parseInnerFilter(parser).query(); + filter = context.getQueryShardContext().parseInnerFilter(parser).query(); } else { throw new SearchParseException(context, "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].", parser.getTokenLocation()); diff --git a/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java b/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java index d813dea1deb..077268ac52f 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/innerhits/InnerHitsParseElement.java @@ -59,7 +59,7 @@ public class InnerHitsParseElement implements SearchParseElement { @Override public void parse(XContentParser parser, SearchContext searchContext) throws Exception { - QueryShardContext context = searchContext.indexShard().getQueryShardContext(); + QueryShardContext context = searchContext.getQueryShardContext(); context.reset(parser); Map topLevelInnerHits = parseInnerHits(parser, context, searchContext); if (topLevelInnerHits != null) { diff --git a/core/src/main/java/org/elasticsearch/search/highlight/HighlighterParseElement.java b/core/src/main/java/org/elasticsearch/search/highlight/HighlighterParseElement.java index 38534ba0ff3..b774acad2ff 100644 --- a/core/src/main/java/org/elasticsearch/search/highlight/HighlighterParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/highlight/HighlighterParseElement.java @@ -54,7 +54,7 @@ public class HighlighterParseElement implements SearchParseElement { @Override public void parse(XContentParser parser, SearchContext context) throws Exception { try { - context.highlight(parse(parser, context.indexShard().getQueryShardContext())); + context.highlight(parse(parser, context.getQueryShardContext())); } catch (IllegalArgumentException ex) { throw new SearchParseException(context, "Error while trying to parse Highlighter element in request", parser.getTokenLocation()); } diff --git a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index 51e9ea4f5d1..32aa45f00f3 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -29,7 +29,6 @@ import org.apache.lucene.search.Query; import org.apache.lucene.search.Sort; import org.apache.lucene.search.FieldDoc; import org.apache.lucene.util.BytesRef; -import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.util.Counter; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.cache.recycler.PageCacheRecycler; @@ -53,6 +52,7 @@ import org.elasticsearch.index.mapper.internal.TypeFieldMapper; import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.AbstractQueryBuilder; import org.elasticsearch.index.query.ParsedQuery; +import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.ScriptService; @@ -62,7 +62,6 @@ import org.elasticsearch.search.dfs.DfsSearchResult; import org.elasticsearch.search.fetch.FetchSearchResult; import org.elasticsearch.search.fetch.FetchSubPhase; import org.elasticsearch.search.fetch.FetchSubPhaseContext; -import org.elasticsearch.search.fetch.innerhits.InnerHitsContext; import org.elasticsearch.search.fetch.script.ScriptFieldsContext; import org.elasticsearch.search.fetch.source.FetchSourceContext; import org.elasticsearch.search.highlight.SearchContextHighlight; @@ -150,6 +149,7 @@ public class DefaultSearchContext extends SearchContext { private final Map subPhaseContexts = new HashMap<>(); private final Map, Collector> queryCollectors = new HashMap<>(); + private final QueryShardContext queryShardContext; public DefaultSearchContext(long id, ShardSearchRequest request, SearchShardTarget shardTarget, Engine.Searcher engineSearcher, IndexService indexService, IndexShard indexShard, @@ -175,6 +175,8 @@ public class DefaultSearchContext extends SearchContext { this.searcher = new ContextIndexSearcher(engineSearcher, indexService.cache().query(), indexShard.getQueryCachingPolicy()); this.timeEstimateCounter = timeEstimateCounter; this.timeoutInMillis = timeout.millis(); + queryShardContext = indexService.newQueryShardContext(); + queryShardContext.setTypes(request.types()); } @Override @@ -206,7 +208,7 @@ public class DefaultSearchContext extends SearchContext { } // initialize the filtering alias based on the provided filters - aliasFilter = indexService.aliasFilter(indexShard.getQueryShardContext(), request.filteringAliases()); + aliasFilter = indexService.aliasFilter(queryShardContext, request.filteringAliases()); if (query() == null) { parsedQuery(ParsedQuery.parsedMatchAllQuery()); @@ -223,7 +225,7 @@ public class DefaultSearchContext extends SearchContext { } private ParsedQuery buildFilteredQuery() { - Query searchFilter = searchFilter(types()); + Query searchFilter = searchFilter(queryShardContext.getTypes()); if (searchFilter == null) { return originalQuery; } @@ -312,16 +314,6 @@ public class DefaultSearchContext extends SearchContext { return request.numberOfShards(); } - @Override - public boolean hasTypes() { - return request.types() != null && request.types().length > 0; - } - - @Override - public String[] types() { - return request.types(); - } - @Override public float queryBoost() { return queryBoost; @@ -765,6 +757,11 @@ public class DefaultSearchContext extends SearchContext { return queryCollectors; } + @Override + public QueryShardContext getQueryShardContext() { + return queryShardContext; + } + @Override public Profilers getProfilers() { return profilers; diff --git a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java index 449e3207ad5..c47d32b9388 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java @@ -35,6 +35,7 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.ParsedQuery; +import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.ScriptService; @@ -117,16 +118,6 @@ public abstract class FilteredSearchContext extends SearchContext { return in.numberOfShards(); } - @Override - public boolean hasTypes() { - return in.hasTypes(); - } - - @Override - public String[] types() { - return in.types(); - } - @Override public float queryBoost() { return in.queryBoost(); @@ -525,4 +516,8 @@ public abstract class FilteredSearchContext extends SearchContext { @Override public Map, Collector> queryCollectors() { return in.queryCollectors();} + @Override + public QueryShardContext getQueryShardContext() { + return in.getQueryShardContext(); + } } diff --git a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java index a93cde1a8a2..7bef5679511 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -72,12 +72,10 @@ public abstract class SearchContext implements Releasable { public static void setCurrent(SearchContext value) { current.set(value); - QueryShardContext.setTypes(value.types()); } public static void removeCurrent() { current.remove(); - QueryShardContext.removeTypes(); } public static SearchContext current() { @@ -134,10 +132,6 @@ public abstract class SearchContext implements Releasable { public abstract int numberOfShards(); - public abstract boolean hasTypes(); - - public abstract String[] types(); - public abstract float queryBoost(); public abstract SearchContext queryBoost(float queryBoost); @@ -379,4 +373,6 @@ public abstract class SearchContext implements Releasable { CONTEXT } + public abstract QueryShardContext getQueryShardContext(); + } diff --git a/core/src/main/java/org/elasticsearch/search/lookup/DocLookup.java b/core/src/main/java/org/elasticsearch/search/lookup/DocLookup.java index b32a6aea203..3eea11a9f5c 100644 --- a/core/src/main/java/org/elasticsearch/search/lookup/DocLookup.java +++ b/core/src/main/java/org/elasticsearch/search/lookup/DocLookup.java @@ -51,4 +51,8 @@ public class DocLookup { public LeafDocLookup getLeafDocLookup(LeafReaderContext context) { return new LeafDocLookup(mapperService, fieldDataService, types, context); } + + public String[] getTypes() { + return types; + } } diff --git a/core/src/main/java/org/elasticsearch/search/query/PostFilterParseElement.java b/core/src/main/java/org/elasticsearch/search/query/PostFilterParseElement.java index 3f3093b6996..6995b6ff8a7 100644 --- a/core/src/main/java/org/elasticsearch/search/query/PostFilterParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/query/PostFilterParseElement.java @@ -30,9 +30,9 @@ public class PostFilterParseElement implements SearchParseElement { @Override public void parse(XContentParser parser, SearchContext context) throws Exception { - ParsedQuery postFilter = context.indexShard().getQueryShardContext().parseInnerFilter(parser); + ParsedQuery postFilter = context.getQueryShardContext().parseInnerFilter(parser); if (postFilter != null) { context.parsedPostFilter(postFilter); } } -} \ No newline at end of file +} diff --git a/core/src/main/java/org/elasticsearch/search/query/QueryParseElement.java b/core/src/main/java/org/elasticsearch/search/query/QueryParseElement.java index 12a080acdea..094a29cd6b1 100644 --- a/core/src/main/java/org/elasticsearch/search/query/QueryParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/query/QueryParseElement.java @@ -30,6 +30,6 @@ public class QueryParseElement implements SearchParseElement { @Override public void parse(XContentParser parser, SearchContext context) throws Exception { - context.parsedQuery(context.indexShard().getQueryShardContext().parse(parser)); + context.parsedQuery(context.getQueryShardContext().parse(parser)); } -} \ No newline at end of file +} diff --git a/core/src/main/java/org/elasticsearch/search/rescore/RescoreParseElement.java b/core/src/main/java/org/elasticsearch/search/rescore/RescoreParseElement.java index 149db6cec2c..702d3666692 100644 --- a/core/src/main/java/org/elasticsearch/search/rescore/RescoreParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/rescore/RescoreParseElement.java @@ -36,10 +36,10 @@ public class RescoreParseElement implements SearchParseElement { public void parse(XContentParser parser, SearchContext context) throws Exception { if (parser.currentToken() == XContentParser.Token.START_ARRAY) { while (parser.nextToken() != XContentParser.Token.END_ARRAY) { - context.addRescore(parseSingleRescoreContext(parser, context.indexShard().getQueryShardContext())); + context.addRescore(parseSingleRescoreContext(parser, context.getQueryShardContext())); } } else { - context.addRescore(parseSingleRescoreContext(parser, context.indexShard().getQueryShardContext())); + context.addRescore(parseSingleRescoreContext(parser, context.getQueryShardContext())); } } diff --git a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggester.java b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggester.java index c7fa6fae302..7838eacd960 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggester.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggester.java @@ -33,7 +33,6 @@ import org.elasticsearch.common.lucene.Lucene; import org.elasticsearch.common.text.Text; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.query.ParsedQuery; -import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.indices.IndicesService; import org.elasticsearch.script.CompiledScript; import org.elasticsearch.script.ExecutableScript; @@ -65,7 +64,7 @@ public final class PhraseSuggester extends Suggester { /* * More Ideas: * - add ability to find whitespace problems -> we can build a poor mans decompounder with our index based on a automaton? - * - add ability to build different error models maybe based on a confusion matrix? + * - add ability to build different error models maybe based on a confusion matrix? * - try to combine a token with its subsequent token to find / detect word splits (optional) * - for this to work we need some way to defined the position length of a candidate * - phonetic filters could be interesting here too for candidate selection @@ -84,8 +83,8 @@ public final class PhraseSuggester extends Suggester { DirectSpellChecker directSpellChecker = SuggestUtils.getDirectSpellChecker(generator); Terms terms = MultiFields.getTerms(indexReader, generator.field()); if (terms != null) { - gens.add(new DirectCandidateGenerator(directSpellChecker, generator.field(), generator.suggestMode(), - indexReader, realWordErrorLikelihood, generator.size(), generator.preFilter(), generator.postFilter(), terms)); + gens.add(new DirectCandidateGenerator(directSpellChecker, generator.field(), generator.suggestMode(), + indexReader, realWordErrorLikelihood, generator.size(), generator.preFilter(), generator.postFilter(), terms)); } } final String suggestField = suggestion.getField(); @@ -119,8 +118,7 @@ public final class PhraseSuggester extends Suggester { final ExecutableScript executable = scriptService.executable(collateScript, vars); final BytesReference querySource = (BytesReference) executable.run(); IndexService indexService = indicesService.indexService(suggestion.getIndex()); - IndexShard shard = indexService.getShard(suggestion.getShard()); - final ParsedQuery parsedQuery = shard.getQueryShardContext().parse(querySource); + final ParsedQuery parsedQuery = indexService.newQueryShardContext().parse(querySource); collateMatch = Lucene.exists(searcher, parsedQuery.query()); } if (!collateMatch && !collatePrune) { @@ -152,7 +150,7 @@ public final class PhraseSuggester extends Suggester { ScriptService scriptService() { return scriptService; } - + @Override public SuggestContextParser getContextParser() { return new PhraseSuggestParser(this); diff --git a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionContext.java b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionContext.java index 04d06abbfd0..736b297fab7 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionContext.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/phrase/PhraseSuggestionContext.java @@ -42,7 +42,6 @@ class PhraseSuggestionContext extends SuggestionContext { private BytesRef preTag; private BytesRef postTag; private CompiledScript collateQueryScript; - private CompiledScript collateFilterScript; private Map collateScriptParams = new HashMap<>(1); private WordScorer.WordScorerFactory scorer; diff --git a/core/src/test/java/org/elasticsearch/ESExceptionTests.java b/core/src/test/java/org/elasticsearch/ESExceptionTests.java index 75a69cd3e55..ad3f0632019 100644 --- a/core/src/test/java/org/elasticsearch/ESExceptionTests.java +++ b/core/src/test/java/org/elasticsearch/ESExceptionTests.java @@ -194,7 +194,7 @@ public class ESExceptionTests extends ESTestCase { public void testToXContent() throws IOException { { - ElasticsearchException ex = new SearchParseException(new TestSearchContext(), "foo", new XContentLocation(1,0)); + ElasticsearchException ex = new SearchParseException(new TestSearchContext(null), "foo", new XContentLocation(1,0)); XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); ex.toXContent(builder, PARAMS); diff --git a/core/src/test/java/org/elasticsearch/ExceptionSerializationTests.java b/core/src/test/java/org/elasticsearch/ExceptionSerializationTests.java index 57b1fdac645..606ff09a60f 100644 --- a/core/src/test/java/org/elasticsearch/ExceptionSerializationTests.java +++ b/core/src/test/java/org/elasticsearch/ExceptionSerializationTests.java @@ -439,7 +439,7 @@ public class ExceptionSerializationTests extends ESTestCase { } public void testSearchParseException() throws IOException { - SearchContext ctx = new TestSearchContext(); + SearchContext ctx = new TestSearchContext(null); SearchParseException ex = serialize(new SearchParseException(ctx, "foo", new XContentLocation(66, 666))); assertEquals("foo", ex.getMessage()); assertEquals(66, ex.getLineNumber()); diff --git a/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java b/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java index 252fa9d3f4e..3a4020e4103 100644 --- a/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java +++ b/core/src/test/java/org/elasticsearch/index/IndexServiceTests.java @@ -39,7 +39,6 @@ import org.elasticsearch.test.ESSingleNodeTestCase; import org.elasticsearch.threadpool.ThreadPool; import java.io.IOException; -import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; @@ -87,17 +86,17 @@ public class IndexServiceTests extends ESSingleNodeTestCase { assertThat(indexService.getMetaData().getAliases().containsKey("dogs"), equalTo(true)); assertThat(indexService.getMetaData().getAliases().containsKey("turtles"), equalTo(false)); - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "cats").toString(), equalTo("animal:cat")); - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "cats", "dogs").toString(), equalTo("animal:cat animal:dog")); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "cats").toString(), equalTo("animal:cat")); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "cats", "dogs").toString(), equalTo("animal:cat animal:dog")); // Non-filtering alias should turn off all filters because filters are ORed - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "all"), nullValue()); - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "cats", "all"), nullValue()); - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "all", "cats"), nullValue()); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "all"), nullValue()); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "cats", "all"), nullValue()); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "all", "cats"), nullValue()); add(indexService, "cats", filter(termQuery("animal", "feline"))); add(indexService, "dogs", filter(termQuery("animal", "canine"))); - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "dogs", "cats").toString(), equalTo("animal:canine animal:feline")); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "dogs", "cats").toString(), equalTo("animal:canine animal:feline")); } public void testAliasFilters() throws Exception { @@ -107,14 +106,14 @@ public class IndexServiceTests extends ESSingleNodeTestCase { add(indexService, "cats", filter(termQuery("animal", "cat"))); add(indexService, "dogs", filter(termQuery("animal", "dog"))); - assertThat(indexService.aliasFilter(shard.getQueryShardContext()), nullValue()); - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "dogs").toString(), equalTo("animal:dog")); - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "dogs", "cats").toString(), equalTo("animal:dog animal:cat")); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext()), nullValue()); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "dogs").toString(), equalTo("animal:dog")); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "dogs", "cats").toString(), equalTo("animal:dog animal:cat")); add(indexService, "cats", filter(termQuery("animal", "feline"))); add(indexService, "dogs", filter(termQuery("animal", "canine"))); - assertThat(indexService.aliasFilter(shard.getQueryShardContext(), "dogs", "cats").toString(), equalTo("animal:canine animal:feline")); + assertThat(indexService.aliasFilter(indexService.newQueryShardContext(), "dogs", "cats").toString(), equalTo("animal:canine animal:feline")); } public void testRemovedAliasFilter() throws Exception { @@ -124,7 +123,7 @@ public class IndexServiceTests extends ESSingleNodeTestCase { add(indexService, "cats", filter(termQuery("animal", "cat"))); remove(indexService, "cats"); try { - indexService.aliasFilter(shard.getQueryShardContext(), "cats"); + indexService.aliasFilter(indexService.newQueryShardContext(), "cats"); fail("Expected InvalidAliasNameException"); } catch (InvalidAliasNameException e) { assertThat(e.getMessage(), containsString("Invalid alias name [cats]")); @@ -139,7 +138,7 @@ public class IndexServiceTests extends ESSingleNodeTestCase { add(indexService, "dogs", filter(termQuery("animal", "dog"))); try { - indexService.aliasFilter(shard.getQueryShardContext(), "unknown"); + indexService.aliasFilter(indexService.newQueryShardContext(), "unknown"); fail(); } catch (InvalidAliasNameException e) { // all is well diff --git a/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java index f9531c3aff3..b6a7dee23ba 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/date/SimpleDateMappingTests.java @@ -260,7 +260,7 @@ public class SimpleDateMappingTests extends ESSingleNodeTestCase { NumericRangeQuery rangeQuery; try { - SearchContext.setCurrent(new TestSearchContext()); + SearchContext.setCurrent(new TestSearchContext(null)); rangeQuery = (NumericRangeQuery) defaultMapper.mappers().smartNameFieldMapper("date_field").fieldType().rangeQuery("10:00:00", "11:00:00", true, true).rewrite(null); } finally { SearchContext.removeCurrent(); @@ -286,7 +286,7 @@ public class SimpleDateMappingTests extends ESSingleNodeTestCase { NumericRangeQuery rangeQuery; try { - SearchContext.setCurrent(new TestSearchContext()); + SearchContext.setCurrent(new TestSearchContext(null)); rangeQuery = (NumericRangeQuery) defaultMapper.mappers().smartNameFieldMapper("date_field").fieldType().rangeQuery("Jan 02 10:00:00", "Jan 02 11:00:00", true, true).rewrite(null); } finally { SearchContext.removeCurrent(); diff --git a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/SimpleExternalMappingTests.java b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/SimpleExternalMappingTests.java index 96c099df6d3..bf92991e039 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/SimpleExternalMappingTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/externalvalues/SimpleExternalMappingTests.java @@ -63,7 +63,7 @@ public class SimpleExternalMappingTests extends ESSingleNodeTestCase { Collections.singletonMap(ExternalMetadataMapper.CONTENT_TYPE, new ExternalMetadataMapper.TypeParser())); DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), - indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); DocumentMapper documentMapper = parser.parse("type", new CompressedXContent( XContentFactory.jsonBuilder().startObject().startObject("type") .startObject(ExternalMetadataMapper.CONTENT_TYPE) @@ -109,7 +109,7 @@ public class SimpleExternalMappingTests extends ESSingleNodeTestCase { MapperRegistry mapperRegistry = new MapperRegistry(mapperParsers, Collections.emptyMap()); DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), - indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); DocumentMapper documentMapper = parser.parse("type", new CompressedXContent( XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") @@ -168,7 +168,7 @@ public class SimpleExternalMappingTests extends ESSingleNodeTestCase { MapperRegistry mapperRegistry = new MapperRegistry(mapperParsers, Collections.emptyMap()); DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), - indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); DocumentMapper documentMapper = parser.parse("type", new CompressedXContent( XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties") diff --git a/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapperTests.java b/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapperTests.java index e23ea15b64f..2646d944711 100644 --- a/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapperTests.java +++ b/core/src/test/java/org/elasticsearch/index/mapper/internal/FieldNamesFieldMapperTests.java @@ -236,9 +236,9 @@ public class FieldNamesFieldMapperTests extends ESSingleNodeTestCase { IndicesModule indicesModule = new IndicesModule(); indicesModule.registerMetadataMapper("_dummy", new DummyMetadataFieldMapper.TypeParser()); final MapperRegistry mapperRegistry = indicesModule.getMapperRegistry(); - MapperService mapperService = new MapperService(indexService.getIndexSettings(), indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + MapperService mapperService = new MapperService(indexService.getIndexSettings(), indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); DocumentMapperParser parser = new DocumentMapperParser(indexService.getIndexSettings(), mapperService, - indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string(); DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping)); ParsedDocument parsedDocument = mapper.parse("index", "type", "id", new BytesArray("{}")); 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 2435a34080d..e8f20cb855c 100644 --- a/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java +++ b/core/src/test/java/org/elasticsearch/index/query/AbstractQueryTestCase.java @@ -332,15 +332,14 @@ public abstract class AbstractQueryTestCase> } protected void setSearchContext(String[] types) { - TestSearchContext testSearchContext = new TestSearchContext(); - testSearchContext.setTypes(types); + TestSearchContext testSearchContext = new TestSearchContext(queryShardContext); + testSearchContext.getQueryShardContext().setTypes(types); SearchContext.setCurrent(testSearchContext); } @After public void afterTest() { clientInvocationHandler.delegate = null; - QueryShardContext.removeTypes(); SearchContext.removeCurrent(); } 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 8a2792eaf44..fb618868417 100644 --- a/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java +++ b/core/src/test/java/org/elasticsearch/index/query/HasChildQueryBuilderTests.java @@ -84,7 +84,7 @@ public class HasChildQueryBuilderTests extends AbstractQueryTestCase() { - @Override - public int compare(PercolateResponse.Match a, PercolateResponse.Match b) { - return a.getId().compareTo(b.getId()); - } - }); + Arrays.sort(matches, (a, b) -> a.getId().compareTo(b.getId())); assertThat(matches[0].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the lazy dog")); assertThat(matches[1].getHighlightFields().get("field1").fragments()[0].string(), equalTo("The quick brown fox jumps over the lazy dog")); diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java index cd7dadd7eeb..ce26728b00b 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/geogrid/GeoHashGridParserTests.java @@ -27,7 +27,7 @@ import org.elasticsearch.test.TestSearchContext; public class GeoHashGridParserTests extends ESTestCase { public void testParseValidFromInts() throws Exception { - SearchContext searchContext = new TestSearchContext(); + SearchContext searchContext = new TestSearchContext(null); int precision = randomIntBetween(1, 12); XContentParser stParser = JsonXContent.jsonXContent.createParser( "{\"field\":\"my_loc\", \"precision\":" + precision + ", \"size\": 500, \"shard_size\": 550}"); @@ -37,7 +37,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseValidFromStrings() throws Exception { - SearchContext searchContext = new TestSearchContext(); + SearchContext searchContext = new TestSearchContext(null); int precision = randomIntBetween(1, 12); XContentParser stParser = JsonXContent.jsonXContent.createParser( "{\"field\":\"my_loc\", \"precision\":\"" + precision + "\", \"size\": \"500\", \"shard_size\": \"550\"}"); @@ -47,7 +47,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnNonIntPrecision() throws Exception { - SearchContext searchContext = new TestSearchContext(); + SearchContext searchContext = new TestSearchContext(null); XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":\"2.0\"}"); GeoHashGridParser parser = new GeoHashGridParser(); try { @@ -59,7 +59,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnBooleanPrecision() throws Exception { - SearchContext searchContext = new TestSearchContext(); + SearchContext searchContext = new TestSearchContext(null); XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":false}"); GeoHashGridParser parser = new GeoHashGridParser(); try { @@ -71,7 +71,7 @@ public class GeoHashGridParserTests extends ESTestCase { } public void testParseErrorOnPrecisionOutOfRange() throws Exception { - SearchContext searchContext = new TestSearchContext(); + SearchContext searchContext = new TestSearchContext(null); XContentParser stParser = JsonXContent.jsonXContent.createParser("{\"field\":\"my_loc\", \"precision\":\"13\"}"); GeoHashGridParser parser = new GeoHashGridParser(); try { @@ -81,4 +81,4 @@ public class GeoHashGridParserTests extends ESTestCase { assertEquals("Invalid geohash aggregation precision of 13. Must be between 1 and 12.", ex.getMessage()); } } -} \ No newline at end of file +} diff --git a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java index 59c110e7744..0a094c3b2d8 100644 --- a/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java +++ b/core/src/test/java/org/elasticsearch/search/aggregations/bucket/significant/SignificanceHeuristicTests.java @@ -69,6 +69,11 @@ import static org.hamcrest.Matchers.lessThanOrEqualTo; */ public class SignificanceHeuristicTests extends ESTestCase { static class SignificantTermsTestSearchContext extends TestSearchContext { + + public SignificantTermsTestSearchContext() { + super(null); + } + @Override public int numberOfShards() { return 1; diff --git a/core/src/test/java/org/elasticsearch/search/fetch/FieldDataFieldsTests.java b/core/src/test/java/org/elasticsearch/search/fetch/FieldDataFieldsTests.java index 7405ceef5a5..ba7f70c63a7 100644 --- a/core/src/test/java/org/elasticsearch/search/fetch/FieldDataFieldsTests.java +++ b/core/src/test/java/org/elasticsearch/search/fetch/FieldDataFieldsTests.java @@ -40,7 +40,7 @@ public class FieldDataFieldsTests extends ESTestCase { parser.nextToken(); parser.nextToken(); parser.nextToken(); - SearchContext context = new TestSearchContext(); + SearchContext context = new TestSearchContext(null); parseElement.parse(parser, context); } @@ -52,7 +52,7 @@ public class FieldDataFieldsTests extends ESTestCase { parser.nextToken(); parser.nextToken(); parser.nextToken(); - SearchContext context = new TestSearchContext(); + SearchContext context = new TestSearchContext(null); parseElement.parse(parser, context); } @@ -69,7 +69,7 @@ public class FieldDataFieldsTests extends ESTestCase { parser.nextToken(); parser.nextToken(); parser.nextToken(); - SearchContext context = new TestSearchContext(); + SearchContext context = new TestSearchContext(null); try { parseElement.parse(parser, context); fail("Expected IllegalStateException"); diff --git a/core/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java b/core/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java index 098825a90b1..9643f248d3e 100644 --- a/core/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java +++ b/core/src/test/java/org/elasticsearch/search/query/QueryPhaseTests.java @@ -51,7 +51,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class QueryPhaseTests extends ESTestCase { private void countTestCase(Query query, IndexReader reader, boolean shouldCollect) throws Exception { - TestSearchContext context = new TestSearchContext(); + TestSearchContext context = new TestSearchContext(null); context.parsedQuery(new ParsedQuery(query)); context.setSize(0); @@ -120,7 +120,7 @@ public class QueryPhaseTests extends ESTestCase { } public void testPostFilterDisablesCountOptimization() throws Exception { - TestSearchContext context = new TestSearchContext(); + TestSearchContext context = new TestSearchContext(null); context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery())); context.setSize(0); @@ -143,7 +143,7 @@ public class QueryPhaseTests extends ESTestCase { } public void testMinScoreDisablesCountOptimization() throws Exception { - TestSearchContext context = new TestSearchContext(); + TestSearchContext context = new TestSearchContext(null); context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery())); context.setSize(0); diff --git a/core/src/test/java/org/elasticsearch/search/sort/SortParserTests.java b/core/src/test/java/org/elasticsearch/search/sort/SortParserTests.java index e39fd81b28d..cbd7b5468b2 100644 --- a/core/src/test/java/org/elasticsearch/search/sort/SortParserTests.java +++ b/core/src/test/java/org/elasticsearch/search/sort/SortParserTests.java @@ -36,7 +36,7 @@ public class SortParserTests extends ESSingleNodeTestCase { mapping.startObject().startObject("type").startObject("properties").startObject("location").field("type", "geo_point").endObject().endObject().endObject().endObject(); IndexService indexService = createIndex("testidx", Settings.settingsBuilder().build(), "type", mapping); TestSearchContext context = (TestSearchContext) createSearchContext(indexService); - context.setTypes("type"); + context.getQueryShardContext().setTypes("type"); XContentBuilder sortBuilder = jsonBuilder(); sortBuilder.startObject(); diff --git a/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java b/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java index 1da604c4184..072c0db3e59 100644 --- a/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java +++ b/plugins/mapper-murmur3/src/test/java/org/elasticsearch/index/mapper/murmur3/Murmur3FieldMapperTests.java @@ -60,7 +60,7 @@ public class Murmur3FieldMapperTests extends ESSingleNodeTestCase { Collections.singletonMap(Murmur3FieldMapper.CONTENT_TYPE, new Murmur3FieldMapper.TypeParser()), Collections.emptyMap()); parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), - indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); } public void testDefaults() throws Exception { @@ -136,7 +136,7 @@ public class Murmur3FieldMapperTests extends ESSingleNodeTestCase { Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); indexService = createIndex("test_bwc", settings); parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), - indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("properties").startObject("field") .field("type", "murmur3") @@ -152,7 +152,7 @@ public class Murmur3FieldMapperTests extends ESSingleNodeTestCase { Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_1_4_2.id).build(); indexService = createIndex("test_bwc", settings); parser = new DocumentMapperParser(indexService.getIndexSettings(), indexService.mapperService(), - indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); String mapping = XContentFactory.jsonBuilder().startObject().startObject("type") .startObject("properties").startObject("field") .field("type", "murmur3") diff --git a/plugins/mapper-size/src/test/java/org/elasticsearch/index/mapper/size/SizeMappingTests.java b/plugins/mapper-size/src/test/java/org/elasticsearch/index/mapper/size/SizeMappingTests.java index a44dddda3ed..d6b64df9e5d 100644 --- a/plugins/mapper-size/src/test/java/org/elasticsearch/index/mapper/size/SizeMappingTests.java +++ b/plugins/mapper-size/src/test/java/org/elasticsearch/index/mapper/size/SizeMappingTests.java @@ -66,7 +66,7 @@ public class SizeMappingTests extends ESSingleNodeTestCase { Map metadataMappers = new HashMap<>(); IndicesModule indices = new IndicesModule(); indices.registerMetadataMapper(SizeFieldMapper.NAME, new SizeFieldMapper.TypeParser()); - mapperService = new MapperService(indexService.getIndexSettings(), indexService.analysisService(), indexService.similarityService(), indices.getMapperRegistry(), indexService::getQueryShardContext); + mapperService = new MapperService(indexService.getIndexSettings(), indexService.analysisService(), indexService.similarityService(), indices.getMapperRegistry(), indexService::newQueryShardContext); parser = mapperService.documentMapperParser(); } @@ -98,7 +98,7 @@ public class SizeMappingTests extends ESSingleNodeTestCase { Collections.emptyMap(), Collections.singletonMap(SizeFieldMapper.NAME, new SizeFieldMapper.TypeParser())); parser = new DocumentMapperParser(indexService.getIndexSettings(), mapperService, - indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::getQueryShardContext); + indexService.analysisService(), indexService.similarityService(), mapperRegistry, indexService::newQueryShardContext); DocumentMapper docMapper = parser.parse("type", new CompressedXContent(mapping)); BytesReference source = XContentFactory.jsonBuilder() diff --git a/test/framework/src/main/java/org/elasticsearch/test/TestSearchContext.java b/test/framework/src/main/java/org/elasticsearch/test/TestSearchContext.java index 814e0c0184f..303203c6c8a 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/TestSearchContext.java +++ b/test/framework/src/main/java/org/elasticsearch/test/TestSearchContext.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.test; -import com.carrotsearch.hppc.ObjectObjectAssociativeContainer; import org.apache.lucene.search.Collector; import org.apache.lucene.search.FieldDoc; import org.apache.lucene.search.Query; @@ -27,7 +26,6 @@ import org.apache.lucene.util.Counter; import org.elasticsearch.action.search.SearchType; import org.elasticsearch.cache.recycler.PageCacheRecycler; import org.elasticsearch.common.ParseFieldMatcher; -import org.elasticsearch.common.collect.ImmutableOpenMap; import org.elasticsearch.common.util.BigArrays; import org.elasticsearch.index.IndexService; import org.elasticsearch.index.analysis.AnalysisService; @@ -38,6 +36,7 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.object.ObjectMapper; import org.elasticsearch.index.query.ParsedQuery; +import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.similarity.SimilarityService; import org.elasticsearch.script.ScriptService; @@ -47,7 +46,6 @@ import org.elasticsearch.search.dfs.DfsSearchResult; import org.elasticsearch.search.fetch.FetchSearchResult; import org.elasticsearch.search.fetch.FetchSubPhase; import org.elasticsearch.search.fetch.FetchSubPhaseContext; -import org.elasticsearch.search.fetch.innerhits.InnerHitsContext; import org.elasticsearch.search.fetch.script.ScriptFieldsContext; import org.elasticsearch.search.fetch.source.FetchSourceContext; import org.elasticsearch.search.highlight.SearchContextHighlight; @@ -62,11 +60,9 @@ import org.elasticsearch.search.rescore.RescoreSearchContext; import org.elasticsearch.search.suggest.SuggestionSearchContext; import org.elasticsearch.threadpool.ThreadPool; -import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Set; public class TestSearchContext extends SearchContext { @@ -80,6 +76,7 @@ public class TestSearchContext extends SearchContext { final IndexShard indexShard; final Counter timeEstimateCounter = Counter.newCounter(); final QuerySearchResult queryResult = new QuerySearchResult(); + final QueryShardContext queryShardContext; ScriptService scriptService; ParsedQuery originalQuery; ParsedQuery postFilter; @@ -89,7 +86,6 @@ public class TestSearchContext extends SearchContext { ContextIndexSearcher searcher; int size; private int terminateAfter = DEFAULT_TERMINATE_AFTER; - private String[] types; private SearchContextAggregations aggregations; private final long originNanoTime = System.nanoTime(); @@ -105,9 +101,10 @@ public class TestSearchContext extends SearchContext { this.threadPool = threadPool; this.indexShard = indexService.getShardOrNull(0); this.scriptService = scriptService; + queryShardContext = indexService.newQueryShardContext(); } - public TestSearchContext() { + public TestSearchContext(QueryShardContext queryShardContext) { super(ParseFieldMatcher.STRICT); this.pageCacheRecycler = null; this.bigArrays = null; @@ -117,10 +114,7 @@ public class TestSearchContext extends SearchContext { this.fixedBitSetFilterCache = null; this.indexShard = null; scriptService = null; - } - - public void setTypes(String... types) { - this.types = types; + this.queryShardContext = queryShardContext; } @Override @@ -167,16 +161,6 @@ public class TestSearchContext extends SearchContext { return 1; } - @Override - public boolean hasTypes() { - return false; - } - - @Override - public String[] types() { - return new String[0]; - } - @Override public float queryBoost() { return 0; @@ -590,4 +574,9 @@ public class TestSearchContext extends SearchContext { @Override public Map, Collector> queryCollectors() {return queryCollectors;} + @Override + public QueryShardContext getQueryShardContext() { + return queryShardContext; + } + }