diff --git a/server/src/main/java/org/elasticsearch/index/cache/bitset/BitsetFilterCache.java b/server/src/main/java/org/elasticsearch/index/cache/bitset/BitsetFilterCache.java index 0eecd2f743c5..c8c0e7aeac40 100644 --- a/server/src/main/java/org/elasticsearch/index/cache/bitset/BitsetFilterCache.java +++ b/server/src/main/java/org/elasticsearch/index/cache/bitset/BitsetFilterCache.java @@ -47,8 +47,8 @@ import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.IndexWarmer; import org.elasticsearch.index.IndexWarmer.TerminationHandle; -import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.MapperService; +import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.ObjectMapper; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.shard.ShardId; @@ -236,12 +236,10 @@ public IndexWarmer.TerminationHandle warmReader(final IndexShard indexShard, fin final Set warmUp = new HashSet<>(); final MapperService mapperService = indexShard.mapperService(); - DocumentMapper docMapper = mapperService.documentMapper(); - if (docMapper != null) { - if (docMapper.hasNestedObjects()) { - warmUp.add(Queries.newNonNestedFilter(indexSettings.getIndexVersionCreated())); - docMapper.getNestedParentMappers().stream().map(ObjectMapper::nestedTypeFilter).forEach(warmUp::add); - } + MappingLookup lookup = mapperService.mappingLookup(); + if (lookup.hasNested()) { + warmUp.add(Queries.newNonNestedFilter(indexSettings.getIndexVersionCreated())); + lookup.getNestedParentMappers().stream().map(ObjectMapper::nestedTypeFilter).forEach(warmUp::add); } final CountDownLatch latch = new CountDownLatch(reader.leaves().size() * warmUp.size()); diff --git a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java index 087275a58da5..17176dce392c 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/DocumentMapper.java @@ -34,10 +34,8 @@ import org.elasticsearch.index.mapper.MapperService.MergeReason; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; -import java.util.List; import java.util.Map; import java.util.Objects; import java.util.function.Function; @@ -179,10 +177,6 @@ public IndexFieldMapper IndexFieldMapper() { return metadataMapper(IndexFieldMapper.class); } - public boolean hasNestedObjects() { - return mappers().hasNested(); - } - public MappingLookup mappers() { return this.mappingLookup; } @@ -207,91 +201,6 @@ public ParsedDocument createNoopTombstoneDoc(String index, String reason) throws return parsedDoc; } - /** - * Given an object path, checks to see if any of its parents are non-nested objects - */ - public boolean hasNonNestedParent(String path) { - ObjectMapper mapper = mappers().objectMappers().get(path); - if (mapper == null) { - return false; - } - while (mapper != null) { - if (mapper.nested().isNested() == false) { - return true; - } - if (path.contains(".") == false) { - return false; - } - path = path.substring(0, path.lastIndexOf(".")); - mapper = mappers().objectMappers().get(path); - } - return false; - } - - /** - * Returns all nested object mappers - */ - public List getNestedMappers() { - List childMappers = new ArrayList<>(); - for (ObjectMapper mapper : mappers().objectMappers().values()) { - if (mapper.nested().isNested() == false) { - continue; - } - childMappers.add(mapper); - } - return childMappers; - } - - /** - * Returns all nested object mappers which contain further nested object mappers - * - * Used by BitSetProducerWarmer - */ - public List getNestedParentMappers() { - List parents = new ArrayList<>(); - for (ObjectMapper mapper : mappers().objectMappers().values()) { - String nestedParentPath = getNestedParent(mapper.fullPath()); - if (nestedParentPath == null) { - continue; - } - ObjectMapper parent = mappers().objectMappers().get(nestedParentPath); - if (parent.nested().isNested()) { - parents.add(parent); - } - } - return parents; - } - - /** - * Given a nested object path, returns the path to its nested parent - * - * In particular, if a nested field `foo` contains an object field - * `bar.baz`, then calling this method with `foo.bar.baz` will return - * the path `foo`, skipping over the object-but-not-nested `foo.bar` - */ - public String getNestedParent(String path) { - ObjectMapper mapper = mappers().objectMappers().get(path); - if (mapper == null) { - return null; - } - if (path.contains(".") == false) { - return null; - } - do { - path = path.substring(0, path.lastIndexOf(".")); - mapper = mappers().objectMappers().get(path); - if (mapper == null) { - return null; - } - if (mapper.nested().isNested()) { - return path; - } - if (path.contains(".") == false) { - return null; - } - } while(true); - } - public DocumentMapper merge(Mapping mapping, MergeReason reason) { Mapping merged = this.mapping().merge(mapping, reason); return new DocumentMapper(mappingLookup.getIndexSettings(), mappingLookup.getIndexAnalyzers(), documentParser, merged); @@ -305,7 +214,7 @@ public void validate(IndexSettings settings, boolean checkLimits) { + "required for partitioned index [" + settings.getIndex().getName() + "]"); } } - if (settings.getIndexSortConfig().hasIndexSort() && hasNestedObjects()) { + if (settings.getIndexSortConfig().hasIndexSort() && mappers().hasNested()) { throw new IllegalArgumentException("cannot have nested fields when index sort is activated"); } if (checkLimits) { @@ -327,7 +236,7 @@ public String toString() { ", documentParser=" + documentParser + ", mappingLookup=" + mappingLookup + ", objectMappers=" + mappers().objectMappers() + - ", hasNestedObjects=" + hasNestedObjects() + + ", hasNestedObjects=" + mappingLookup.hasNested() + ", deleteTombstoneMetadataFieldMappers=" + Arrays.toString(deleteTombstoneMetadataFieldMappers) + ", noopTombstoneMetadataFieldMappers=" + Arrays.toString(noopTombstoneMetadataFieldMappers) + '}'; diff --git a/server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java b/server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java index 53237d74ffb2..efe70aad071e 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/MappingLookup.java @@ -341,4 +341,89 @@ IndexSettings getIndexSettings() { IndexAnalyzers getIndexAnalyzers() { return indexAnalyzers; } + + /** + * Given an object path, checks to see if any of its parents are non-nested objects + */ + public boolean hasNonNestedParent(String path) { + ObjectMapper mapper = objectMappers().get(path); + if (mapper == null) { + return false; + } + while (mapper != null) { + if (mapper.nested().isNested() == false) { + return true; + } + if (path.contains(".") == false) { + return false; + } + path = path.substring(0, path.lastIndexOf(".")); + mapper = objectMappers().get(path); + } + return false; + } + + /** + * Returns all nested object mappers + */ + public List getNestedMappers() { + List childMappers = new ArrayList<>(); + for (ObjectMapper mapper : objectMappers().values()) { + if (mapper.nested().isNested() == false) { + continue; + } + childMappers.add(mapper); + } + return childMappers; + } + + /** + * Returns all nested object mappers which contain further nested object mappers + * + * Used by BitSetProducerWarmer + */ + public List getNestedParentMappers() { + List parents = new ArrayList<>(); + for (ObjectMapper mapper : objectMappers().values()) { + String nestedParentPath = getNestedParent(mapper.fullPath()); + if (nestedParentPath == null) { + continue; + } + ObjectMapper parent = objectMappers().get(nestedParentPath); + if (parent.nested().isNested()) { + parents.add(parent); + } + } + return parents; + } + + /** + * Given a nested object path, returns the path to its nested parent + * + * In particular, if a nested field `foo` contains an object field + * `bar.baz`, then calling this method with `foo.bar.baz` will return + * the path `foo`, skipping over the object-but-not-nested `foo.bar` + */ + public String getNestedParent(String path) { + ObjectMapper mapper = objectMappers().get(path); + if (mapper == null) { + return null; + } + if (path.contains(".") == false) { + return null; + } + do { + path = path.substring(0, path.lastIndexOf(".")); + mapper = objectMappers().get(path); + if (mapper == null) { + return null; + } + if (mapper.nested().isNested()) { + return path; + } + if (path.contains(".") == false) { + return null; + } + } while(true); + } } diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 5deae8880be3..9a75e70c4278 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -67,6 +67,7 @@ import org.elasticsearch.script.ScriptContext; import org.elasticsearch.script.ScriptFactory; import org.elasticsearch.script.ScriptService; +import org.elasticsearch.search.NestedDocuments; import org.elasticsearch.search.aggregations.support.ValuesSourceRegistry; import org.elasticsearch.search.lookup.SearchLookup; import org.elasticsearch.transport.RemoteClusterAware; @@ -677,4 +678,8 @@ private static Map parseRuntimeMappings(Map childObjectFilters = new HashMap<>(); private final Map childObjectMappers = new HashMap<>(); private final BitSetProducer parentDocumentFilter; - private final MapperService mapperService; - private final Version version; + private final MappingLookup mappingLookup; + private final Version indexVersionCreated; /** * Create a new NestedDocuments object for an index - * @param mapperService the index's MapperService + * @param mappingLookup the index's mapping * @param filterProducer a function to build BitSetProducers from filter queries */ - public NestedDocuments(MapperService mapperService, Function filterProducer) { - this.mapperService = mapperService; - this.version = mapperService.getIndexSettings().getIndexVersionCreated(); - if (mapperService.hasNested() == false) { + public NestedDocuments(MappingLookup mappingLookup, Version indexVersionCreated, Function filterProducer) { + this.mappingLookup = mappingLookup; + this.indexVersionCreated = indexVersionCreated; + if (mappingLookup.hasNested() == false) { this.parentDocumentFilter = null; } else { - this.parentDocumentFilter = filterProducer.apply(Queries.newNonNestedFilter(version)); - for (ObjectMapper mapper : mapperService.documentMapper().getNestedParentMappers()) { + this.parentDocumentFilter = filterProducer.apply(Queries.newNonNestedFilter(indexVersionCreated)); + for (ObjectMapper mapper : mappingLookup.getNestedParentMappers()) { parentObjectFilters.put(mapper.name(), filterProducer.apply(mapper.nestedTypeFilter())); } - for (ObjectMapper mapper : mapperService.documentMapper().getNestedMappers()) { + for (ObjectMapper mapper : mappingLookup.getNestedMappers()) { childObjectFilters.put(mapper.name(), null); childObjectMappers.put(mapper.name(), mapper); } @@ -101,7 +101,7 @@ private Weight getNestedChildWeight(LeafReaderContext ctx, String path) throws I * Given an object path, returns whether or not any of its parents are plain objects */ public boolean hasNonNestedParent(String path) { - return mapperService.documentMapper().hasNonNestedParent(path); + return mappingLookup.hasNonNestedParent(path); } private class HasNestedDocuments implements LeafNestedDocuments { @@ -188,7 +188,7 @@ private SearchHit.NestedIdentity loadNestedIdentity() throws IOException { int parentNameLength; String path = findObjectPath(doc); while (path != null) { - String parent = mapperService.documentMapper().getNestedParent(path); + String parent = mappingLookup.getNestedParent(path); // We have to pull a new scorer for each document here, because we advance from // the last parent which will be behind the doc Scorer childScorer = getNestedChildWeight(ctx, path).scorer(ctx); @@ -208,7 +208,7 @@ private SearchHit.NestedIdentity loadNestedIdentity() throws IOException { } int offset = 0; DocIdSetIterator childIt = childScorer.iterator(); - if (version.onOrAfter(Version.V_6_5_0)) { + if (indexVersionCreated.onOrAfter(Version.V_6_5_0)) { /* * Starts from the previous parent and finds the offset of the * nestedSubDocID within the nested children. Nested documents diff --git a/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index 962e90624ea1..4f2614d506bf 100644 --- a/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/server/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -113,7 +113,7 @@ public void execute(SearchContext context) { SearchHit[] hits = new SearchHit[context.docIdsToLoadSize()]; List processors = getProcessors(context.shardTarget(), fetchContext); - NestedDocuments nestedDocuments = context.getNestedDocuments(); + NestedDocuments nestedDocuments = context.getQueryShardContext().getNestedDocuments(); int currentReaderIndex = -1; LeafReaderContext currentReaderContext = null; diff --git a/server/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java b/server/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java index b4ae043bdb20..2c787f114991 100644 --- a/server/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java +++ b/server/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java @@ -29,7 +29,6 @@ import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.shard.IndexShard; -import org.elasticsearch.search.NestedDocuments; import org.elasticsearch.search.SearchExtBuilder; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.search.aggregations.SearchContextAggregations; @@ -390,11 +389,6 @@ public FetchSearchResult fetchResult() { return in.fetchResult(); } - @Override - public NestedDocuments getNestedDocuments() { - return in.getNestedDocuments(); - } - @Override public FetchPhase fetchPhase() { return in.fetchPhase(); diff --git a/server/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/server/src/main/java/org/elasticsearch/search/internal/SearchContext.java index 59e9aabb6481..a27b268dec37 100644 --- a/server/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/server/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -32,7 +32,6 @@ import org.elasticsearch.index.query.ParsedQuery; import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.shard.IndexShard; -import org.elasticsearch.search.NestedDocuments; import org.elasticsearch.search.RescoreDocIds; import org.elasticsearch.search.SearchExtBuilder; import org.elasticsearch.search.SearchShardTarget; @@ -311,8 +310,6 @@ public final void assignRescoreDocIds(RescoreDocIds rescoreDocIds) { public abstract QuerySearchResult queryResult(); - public abstract NestedDocuments getNestedDocuments(); - public abstract FetchPhase fetchPhase(); public abstract FetchSearchResult fetchResult(); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/NestedDocumentsTests.java b/server/src/test/java/org/elasticsearch/index/mapper/NestedDocumentsTests.java index 16280eea7ce9..ff9f4fc48e4e 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/NestedDocumentsTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/NestedDocumentsTests.java @@ -20,6 +20,7 @@ package org.elasticsearch.index.mapper; import org.apache.lucene.search.join.QueryBitSetProducer; +import org.elasticsearch.Version; import org.elasticsearch.search.LeafNestedDocuments; import org.elasticsearch.search.NestedDocuments; import org.elasticsearch.search.SearchHit; @@ -56,7 +57,7 @@ public void testSimpleNestedHierarchy() throws IOException { })); withLuceneIndex(mapperService, iw -> iw.addDocuments(doc.docs()), reader -> { - NestedDocuments nested = new NestedDocuments(mapperService, QueryBitSetProducer::new); + NestedDocuments nested = new NestedDocuments(mapperService.mappingLookup(), Version.CURRENT, QueryBitSetProducer::new); LeafNestedDocuments leaf = nested.getLeafNestedDocuments(reader.leaves().get(0)); assertNotNull(leaf.advance(0)); @@ -153,7 +154,7 @@ public void testMultiLevelNestedHierarchy() throws IOException { })); withLuceneIndex(mapperService, iw -> iw.addDocuments(doc.docs()), reader -> { - NestedDocuments nested = new NestedDocuments(mapperService, QueryBitSetProducer::new); + NestedDocuments nested = new NestedDocuments(mapperService.mappingLookup(), Version.CURRENT, QueryBitSetProducer::new); LeafNestedDocuments leaf = nested.getLeafNestedDocuments(reader.leaves().get(0)); assertNotNull(leaf.advance(0)); @@ -264,7 +265,7 @@ public void testNestedObjectWithinNonNestedObject() throws IOException { })); withLuceneIndex(mapperService, iw -> iw.addDocuments(doc.docs()), reader -> { - NestedDocuments nested = new NestedDocuments(mapperService, QueryBitSetProducer::new); + NestedDocuments nested = new NestedDocuments(mapperService.mappingLookup(), Version.CURRENT, QueryBitSetProducer::new); LeafNestedDocuments leaf = nested.getLeafNestedDocuments(reader.leaves().get(0)); assertNotNull(leaf.advance(0)); diff --git a/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java b/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java index 9464b4b04e82..390a6354b8d2 100644 --- a/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java +++ b/server/src/test/java/org/elasticsearch/index/mapper/NestedObjectMapperTests.java @@ -66,7 +66,7 @@ public void testSingleNested() throws Exception { DocumentMapper docMapper = createDocumentMapper(mapping(b -> b.startObject("nested1").field("type", "nested").endObject())); - assertThat(docMapper.hasNestedObjects(), equalTo(true)); + assertThat(docMapper.mappers().hasNested(), equalTo(true)); ObjectMapper nested1Mapper = docMapper.mappers().objectMappers().get("nested1"); assertThat(nested1Mapper.nested().isNested(), equalTo(true)); @@ -122,7 +122,7 @@ public void testMultiNested() throws Exception { b.endObject(); })); - assertThat(docMapper.hasNestedObjects(), equalTo(true)); + assertThat(docMapper.mappers().hasNested(), equalTo(true)); ObjectMapper nested1Mapper = docMapper.mappers().objectMappers().get("nested1"); assertThat(nested1Mapper.nested().isNested(), equalTo(true)); assertThat(nested1Mapper.nested().isIncludeInParent(), equalTo(false)); @@ -192,7 +192,7 @@ public void testMultiObjectAndNested1() throws Exception { b.endObject(); })); - assertThat(docMapper.hasNestedObjects(), equalTo(true)); + assertThat(docMapper.mappers().hasNested(), equalTo(true)); ObjectMapper nested1Mapper = docMapper.mappers().objectMappers().get("nested1"); assertThat(nested1Mapper.nested().isNested(), equalTo(true)); assertThat(nested1Mapper.nested().isIncludeInParent(), equalTo(false)); @@ -263,7 +263,7 @@ public void testMultiObjectAndNested2() throws Exception { b.endObject(); })); - assertThat(docMapper.hasNestedObjects(), equalTo(true)); + assertThat(docMapper.mappers().hasNested(), equalTo(true)); ObjectMapper nested1Mapper = docMapper.mappers().objectMappers().get("nested1"); assertThat(nested1Mapper.nested().isNested(), equalTo(true)); assertThat(nested1Mapper.nested().isIncludeInParent(), equalTo(true)); @@ -333,11 +333,11 @@ public void testMultiRootAndNested1() throws Exception { b.endObject(); })); - assertEquals("nested1", docMapper.getNestedParent("nested1.nested2")); - assertNull(docMapper.getNestedParent("nonexistent")); - assertNull(docMapper.getNestedParent("nested1")); + assertEquals("nested1", docMapper.mappers().getNestedParent("nested1.nested2")); + assertNull(docMapper.mappers().getNestedParent("nonexistent")); + assertNull(docMapper.mappers().getNestedParent("nested1")); - assertThat(docMapper.hasNestedObjects(), equalTo(true)); + assertThat(docMapper.mappers().hasNested(), equalTo(true)); ObjectMapper nested1Mapper = docMapper.mappers().objectMappers().get("nested1"); assertThat(nested1Mapper.nested().isNested(), equalTo(true)); assertThat(nested1Mapper.nested().isIncludeInParent(), equalTo(false)); @@ -539,7 +539,7 @@ public void testNestedArrayStrict() throws Exception { b.endObject(); })); - assertThat(docMapper.hasNestedObjects(), equalTo(true)); + assertThat(docMapper.mappers().hasNested(), equalTo(true)); ObjectMapper nested1Mapper = docMapper.mappers().objectMappers().get("nested1"); assertThat(nested1Mapper.nested().isNested(), equalTo(true)); assertThat(nested1Mapper.dynamic(), equalTo(Dynamic.STRICT)); @@ -617,7 +617,7 @@ public void testParentObjectMapperAreNested() throws Exception { } b.endObject(); })); - assertFalse(mapperService.documentMapper().hasNonNestedParent("comments.messages")); + assertFalse(mapperService.documentMapper().mappers().hasNonNestedParent("comments.messages")); mapperService = createMapperService(mapping(b -> { b.startObject("comments"); @@ -631,7 +631,7 @@ public void testParentObjectMapperAreNested() throws Exception { } b.endObject(); })); - assertTrue(mapperService.documentMapper().hasNonNestedParent("comments.messages")); + assertTrue(mapperService.documentMapper().mappers().hasNonNestedParent("comments.messages")); } public void testLimitNestedDocsDefaultSettings() throws Exception { @@ -783,7 +783,7 @@ public void testReorderParentBWC() throws IOException { Version bwcVersion = VersionUtils.randomVersionBetween(random(), Version.V_6_0_0, Version.V_6_4_0); for (Version version : new Version[]{Version.V_6_5_0, bwcVersion}) { DocumentMapper docMapper = createDocumentMapper(version, mapping); - assertThat(docMapper.hasNestedObjects(), equalTo(true)); + assertThat(docMapper.mappers().hasNested(), equalTo(true)); ObjectMapper nested1Mapper = docMapper.mappers().objectMappers().get("nested1"); assertThat(nested1Mapper.nested().isNested(), equalTo(true)); @@ -830,7 +830,7 @@ public void testReorderParent() throws IOException { DocumentMapper docMapper = createDocumentMapper(version, mapping(b -> b.startObject("nested1").field("type", "nested").endObject())); - assertThat(docMapper.hasNestedObjects(), equalTo(true)); + assertThat(docMapper.mappers().hasNested(), equalTo(true)); ObjectMapper nested1Mapper = docMapper.mappers().objectMappers().get("nested1"); assertThat(nested1Mapper.nested().isNested(), equalTo(true)); diff --git a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java index 2cb3d3abe27f..31859f6e8803 100644 --- a/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/search/aggregations/AggregatorTestCase.java @@ -86,7 +86,6 @@ import org.elasticsearch.index.mapper.KeywordFieldMapper; import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.index.mapper.Mapper; -import org.elasticsearch.index.mapper.MapperService; import org.elasticsearch.index.mapper.Mapping; import org.elasticsearch.index.mapper.MappingLookup; import org.elasticsearch.index.mapper.MockFieldMapper; @@ -149,7 +148,9 @@ import static org.elasticsearch.test.InternalAggregationTestCase.DEFAULT_MAX_BUCKETS; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; import static org.mockito.Mockito.when; /** @@ -363,15 +364,29 @@ public boolean shouldCache(Query query) { throw new RuntimeException(e); } when(ctx.fetchPhase()).thenReturn(new FetchPhase(Arrays.asList(new FetchSourcePhase(), new FetchDocValuesPhase()))); - when(ctx.getQueryShardContext()).thenReturn(queryShardContext); + + /* + * Use a QueryShardContext that doesn't contain nested documents so we + * don't try to fetch them which would require mocking a whole menagerie + * of stuff. + */ + QueryShardContext subQSC = spy(queryShardContext); + MappingLookup disableNestedLookup = new MappingLookup( + Mapping.EMPTY, + org.elasticsearch.common.collect.Set.of(), + org.elasticsearch.common.collect.Set.of(), + org.elasticsearch.common.collect.Set.of(), + null, + null, + null + ); + doReturn(new NestedDocuments(disableNestedLookup, Version.CURRENT, bitsetFilterCache::getBitSetProducer)).when(subQSC) + .getNestedDocuments(); + when(ctx.getQueryShardContext()).thenReturn(subQSC); + IndexShard indexShard = mock(IndexShard.class); when(indexShard.shardId()).thenReturn(new ShardId("test", "test", 0)); when(ctx.indexShard()).thenReturn(indexShard); - MapperService mapperService = mock(MapperService.class); - when(mapperService.hasNested()).thenReturn(false); - when(mapperService.getIndexSettings()).thenReturn(indexSettings); - NestedDocuments nested = new NestedDocuments(mapperService, bitsetFilterCache::getBitSetProducer); - when(ctx.getNestedDocuments()).thenReturn(nested); return new SubSearchContext(ctx); } 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 29b64da0122c..f7898132d294 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/TestSearchContext.java +++ b/test/framework/src/main/java/org/elasticsearch/test/TestSearchContext.java @@ -31,7 +31,6 @@ import org.elasticsearch.index.query.QueryShardContext; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.shard.ShardId; -import org.elasticsearch.search.NestedDocuments; import org.elasticsearch.search.SearchExtBuilder; import org.elasticsearch.search.SearchShardTarget; import org.elasticsearch.search.aggregations.SearchContextAggregations; @@ -498,11 +497,6 @@ public FetchSearchResult fetchResult() { return null; } - @Override - public NestedDocuments getNestedDocuments() { - return new NestedDocuments(indexService.mapperService(), bitsetFilterCache()::getBitSetProducer); - } - @Override public FetchPhase fetchPhase() { return null;