|
48 | 48 | import org.elasticsearch.index.cache.bitset.BitsetFilterCache.Listener;
|
49 | 49 | import org.elasticsearch.index.cache.query.DisabledQueryCache;
|
50 | 50 | import org.elasticsearch.index.engine.Engine;
|
| 51 | +import org.elasticsearch.index.fielddata.IndexFieldData; |
51 | 52 | import org.elasticsearch.index.fielddata.IndexFieldDataCache;
|
52 | 53 | import org.elasticsearch.index.fielddata.IndexFieldDataService;
|
53 | 54 | import org.elasticsearch.index.mapper.ContentPath;
|
|
58 | 59 | import org.elasticsearch.index.mapper.ObjectMapper;
|
59 | 60 | import org.elasticsearch.index.mapper.ObjectMapper.Nested;
|
60 | 61 | import org.elasticsearch.index.query.QueryShardContext;
|
61 |
| -import org.elasticsearch.index.query.support.NestedScope; |
62 | 62 | import org.elasticsearch.index.shard.IndexShard;
|
63 | 63 | import org.elasticsearch.index.shard.ShardId;
|
64 | 64 | import org.elasticsearch.indices.breaker.CircuitBreakerService;
|
|
83 | 83 | import java.util.Arrays;
|
84 | 84 | import java.util.Collections;
|
85 | 85 | import java.util.HashMap;
|
86 |
| -import java.util.HashSet; |
87 | 86 | import java.util.List;
|
88 | 87 | import java.util.Map;
|
89 | 88 | import java.util.Objects;
|
| 89 | +import java.util.function.BiFunction; |
90 | 90 | import java.util.function.Function;
|
91 | 91 | import java.util.stream.Collectors;
|
92 | 92 |
|
@@ -157,8 +157,8 @@ protected AggregatorFactory<?> createAggregatorFactory(Query query,
|
157 | 157 | SearchLookup searchLookup = new SearchLookup(mapperService, ifds::getForField, new String[]{TYPE_NAME});
|
158 | 158 | when(searchContext.lookup()).thenReturn(searchLookup);
|
159 | 159 |
|
160 |
| - QueryShardContext queryShardContext = queryShardContextMock(mapperService); |
161 |
| - when(queryShardContext.getIndexSettings()).thenReturn(indexSettings); |
| 160 | + QueryShardContext queryShardContext = queryShardContextMock(mapperService, indexSettings, circuitBreakerService); |
| 161 | + |
162 | 162 | when(searchContext.getQueryShardContext()).thenReturn(queryShardContext);
|
163 | 163 | Map<String, MappedFieldType> fieldNameToType = new HashMap<>();
|
164 | 164 | fieldNameToType.putAll(Arrays.stream(fieldTypes)
|
@@ -189,16 +189,11 @@ private void registerFieldTypes(QueryShardContext queryShardContext,
|
189 | 189 | String fieldName = entry.getKey();
|
190 | 190 | MappedFieldType fieldType = entry.getValue();
|
191 | 191 |
|
192 |
| - when(queryShardContext.fieldMapper(fieldName)).thenReturn(fieldType); |
| 192 | + when(mapperService.fullName(fieldName)).thenReturn(fieldType); |
193 | 193 | when(searchContext.smartNameFieldType(fieldName)).thenReturn(fieldType);
|
194 | 194 | }
|
195 | 195 |
|
196 |
| - for (MappedFieldType fieldType : new HashSet<>(fieldNameToType.values())) { |
197 |
| - when(queryShardContext.getForField(fieldType)).then(invocation -> |
198 |
| - fieldType.fielddataBuilder(mapperService.getIndexSettings().getIndex().getName()) |
199 |
| - .build(mapperService.getIndexSettings(), fieldType, |
200 |
| - new IndexFieldDataCache.None(), circuitBreakerService, mapperService)); |
201 |
| - } |
| 196 | + |
202 | 197 | }
|
203 | 198 |
|
204 | 199 | protected <A extends Aggregator> A createAggregator(AggregationBuilder aggregationBuilder,
|
@@ -304,12 +299,31 @@ protected MapperService mapperServiceMock() {
|
304 | 299 | /**
|
305 | 300 | * sub-tests that need a more complex mock can overwrite this
|
306 | 301 | */
|
307 |
| - protected QueryShardContext queryShardContextMock(MapperService mapperService) { |
308 |
| - QueryShardContext queryShardContext = mock(QueryShardContext.class); |
309 |
| - when(queryShardContext.getMapperService()).thenReturn(mapperService); |
310 |
| - NestedScope nestedScope = new NestedScope(); |
311 |
| - when(queryShardContext.nestedScope()).thenReturn(nestedScope); |
312 |
| - return queryShardContext; |
| 302 | + protected QueryShardContext queryShardContextMock(MapperService mapperService, IndexSettings indexSettings, |
| 303 | + CircuitBreakerService circuitBreakerService) { |
| 304 | + |
| 305 | + return new QueryShardContext(0, indexSettings, null, null, |
| 306 | + getIndexFieldDataLookup(mapperService, circuitBreakerService), |
| 307 | + mapperService, null, getMockScriptService(), xContentRegistry(), |
| 308 | + writableRegistry(), null, null, System::currentTimeMillis, null); |
| 309 | + } |
| 310 | + |
| 311 | + /** |
| 312 | + * Sub-tests that need a more complex index field data provider can override this |
| 313 | + */ |
| 314 | + protected BiFunction<MappedFieldType, String, IndexFieldData<?>> getIndexFieldDataLookup(MapperService mapperService, |
| 315 | + CircuitBreakerService circuitBreakerService) { |
| 316 | + return (fieldType, s) -> fieldType.fielddataBuilder(mapperService.getIndexSettings().getIndex().getName()) |
| 317 | + .build(mapperService.getIndexSettings(), fieldType, |
| 318 | + new IndexFieldDataCache.None(), circuitBreakerService, mapperService); |
| 319 | + |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * Sub-tests that need scripting can override this method to provide a script service and pre-baked scripts |
| 324 | + */ |
| 325 | + protected ScriptService getMockScriptService() { |
| 326 | + return null; |
313 | 327 | }
|
314 | 328 |
|
315 | 329 | protected <A extends InternalAggregation, C extends Aggregator> A search(IndexSearcher searcher,
|
|
0 commit comments