Skip to content

Commit 5eeb3c5

Browse files
authored
Replace the SearchContext with QueryShardContext when building aggregator factories (#46527)
This commit replaces the `SearchContext` with the `QueryShardContext` when building aggregator factories. Aggregator factories are part of the `SearchContext` so they shouldn't require a `SearchContext` to create them. The main changes here are the signatures of `AggregationBuilder#build` that now takes a `QueryShardContext` and `AggregatorFactory#createInternal` that passes the `SearchContext` to build the `Aggregator`. Relates #46523
1 parent 4207fcf commit 5eeb3c5

File tree

130 files changed

+1294
-911
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+1294
-911
lines changed

modules/aggs-matrix-stats/src/main/java/org/elasticsearch/search/aggregations/matrix/stats/MatrixStatsAggregationBuilder.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.common.io.stream.StreamOutput;
2323
import org.elasticsearch.common.xcontent.ToXContent;
2424
import org.elasticsearch.common.xcontent.XContentBuilder;
25+
import org.elasticsearch.index.query.QueryShardContext;
2526
import org.elasticsearch.search.MultiValueMode;
2627
import org.elasticsearch.search.aggregations.AggregationBuilder;
2728
import org.elasticsearch.search.aggregations.AggregatorFactories;
@@ -32,7 +33,6 @@
3233
import org.elasticsearch.search.aggregations.support.ValuesSource.Numeric;
3334
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
3435
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
35-
import org.elasticsearch.search.internal.SearchContext;
3636

3737
import java.io.IOException;
3838
import java.util.Map;
@@ -80,9 +80,11 @@ public MultiValueMode multiValueMode() {
8080
}
8181

8282
@Override
83-
protected MatrixStatsAggregatorFactory innerBuild(SearchContext context, Map<String, ValuesSourceConfig<Numeric>> configs,
84-
AggregatorFactory parent, AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
85-
return new MatrixStatsAggregatorFactory(name, configs, multiValueMode, context, parent, subFactoriesBuilder, metaData);
83+
protected MatrixStatsAggregatorFactory innerBuild(QueryShardContext queryShardContext,
84+
Map<String, ValuesSourceConfig<Numeric>> configs,
85+
AggregatorFactory parent,
86+
AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
87+
return new MatrixStatsAggregatorFactory(name, configs, multiValueMode, queryShardContext, parent, subFactoriesBuilder, metaData);
8688
}
8789

8890
@Override

modules/aggs-matrix-stats/src/main/java/org/elasticsearch/search/aggregations/matrix/stats/MatrixStatsAggregatorFactory.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919
package org.elasticsearch.search.aggregations.matrix.stats;
2020

21+
import org.elasticsearch.index.query.QueryShardContext;
2122
import org.elasticsearch.search.MultiValueMode;
2223
import org.elasticsearch.search.aggregations.Aggregator;
2324
import org.elasticsearch.search.aggregations.AggregatorFactories;
@@ -37,23 +38,32 @@ final class MatrixStatsAggregatorFactory extends ArrayValuesSourceAggregatorFact
3738
private final MultiValueMode multiValueMode;
3839

3940
MatrixStatsAggregatorFactory(String name,
40-
Map<String, ValuesSourceConfig<ValuesSource.Numeric>> configs, MultiValueMode multiValueMode,
41-
SearchContext context, AggregatorFactory parent, AggregatorFactories.Builder subFactoriesBuilder,
42-
Map<String, Object> metaData) throws IOException {
43-
super(name, configs, context, parent, subFactoriesBuilder, metaData);
41+
Map<String, ValuesSourceConfig<ValuesSource.Numeric>> configs,
42+
MultiValueMode multiValueMode,
43+
QueryShardContext queryShardContext,
44+
AggregatorFactory parent,
45+
AggregatorFactories.Builder subFactoriesBuilder,
46+
Map<String, Object> metaData) throws IOException {
47+
super(name, configs, queryShardContext, parent, subFactoriesBuilder, metaData);
4448
this.multiValueMode = multiValueMode;
4549
}
4650

4751
@Override
48-
protected Aggregator createUnmapped(Aggregator parent, List<PipelineAggregator> pipelineAggregators, Map<String, Object> metaData)
52+
protected Aggregator createUnmapped(SearchContext searchContext,
53+
Aggregator parent,
54+
List<PipelineAggregator> pipelineAggregators,
55+
Map<String, Object> metaData)
4956
throws IOException {
50-
return new MatrixStatsAggregator(name, null, context, parent, multiValueMode, pipelineAggregators, metaData);
57+
return new MatrixStatsAggregator(name, null, searchContext, parent, multiValueMode, pipelineAggregators, metaData);
5158
}
5259

5360
@Override
54-
protected Aggregator doCreateInternal(Map<String, ValuesSource.Numeric> valuesSources, Aggregator parent,
55-
boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators,
56-
Map<String, Object> metaData) throws IOException {
57-
return new MatrixStatsAggregator(name, valuesSources, context, parent, multiValueMode, pipelineAggregators, metaData);
61+
protected Aggregator doCreateInternal(Map<String, ValuesSource.Numeric> valuesSources,
62+
SearchContext searchContext,
63+
Aggregator parent,
64+
boolean collectsFromSingleBucket,
65+
List<PipelineAggregator> pipelineAggregators,
66+
Map<String, Object> metaData) throws IOException {
67+
return new MatrixStatsAggregator(name, valuesSources, searchContext, parent, multiValueMode, pipelineAggregators, metaData);
5868
}
5969
}

modules/aggs-matrix-stats/src/main/java/org/elasticsearch/search/aggregations/support/ArrayValuesSourceAggregationBuilder.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
import org.elasticsearch.index.fielddata.IndexGeoPointFieldData;
2828
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
2929
import org.elasticsearch.index.mapper.MappedFieldType;
30+
import org.elasticsearch.index.query.QueryShardContext;
3031
import org.elasticsearch.script.Script;
3132
import org.elasticsearch.search.DocValueFormat;
3233
import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
3334
import org.elasticsearch.search.aggregations.AggregationInitializationException;
3435
import org.elasticsearch.search.aggregations.AggregatorFactories;
3536
import org.elasticsearch.search.aggregations.AggregatorFactories.Builder;
3637
import org.elasticsearch.search.aggregations.AggregatorFactory;
37-
import org.elasticsearch.search.internal.SearchContext;
3838

3939
import java.io.IOException;
4040
import java.util.ArrayList;
@@ -239,28 +239,28 @@ public Map<String, Object> missingMap() {
239239
}
240240

241241
@Override
242-
protected final ArrayValuesSourceAggregatorFactory<VS> doBuild(SearchContext context, AggregatorFactory parent,
243-
AggregatorFactories.Builder subFactoriesBuilder) throws IOException {
244-
Map<String, ValuesSourceConfig<VS>> configs = resolveConfig(context);
245-
ArrayValuesSourceAggregatorFactory<VS> factory = innerBuild(context, configs, parent, subFactoriesBuilder);
242+
protected final ArrayValuesSourceAggregatorFactory<VS> doBuild(QueryShardContext queryShardContext, AggregatorFactory parent,
243+
Builder subFactoriesBuilder) throws IOException {
244+
Map<String, ValuesSourceConfig<VS>> configs = resolveConfig(queryShardContext);
245+
ArrayValuesSourceAggregatorFactory<VS> factory = innerBuild(queryShardContext, configs, parent, subFactoriesBuilder);
246246
return factory;
247247
}
248248

249-
protected Map<String, ValuesSourceConfig<VS>> resolveConfig(SearchContext context) {
249+
protected Map<String, ValuesSourceConfig<VS>> resolveConfig(QueryShardContext queryShardContext) {
250250
HashMap<String, ValuesSourceConfig<VS>> configs = new HashMap<>();
251251
for (String field : fields) {
252-
ValuesSourceConfig<VS> config = config(context, field, null);
252+
ValuesSourceConfig<VS> config = config(queryShardContext, field, null);
253253
configs.put(field, config);
254254
}
255255
return configs;
256256
}
257257

258-
protected abstract ArrayValuesSourceAggregatorFactory<VS> innerBuild(SearchContext context,
258+
protected abstract ArrayValuesSourceAggregatorFactory<VS> innerBuild(QueryShardContext queryShardContext,
259259
Map<String, ValuesSourceConfig<VS>> configs,
260260
AggregatorFactory parent,
261261
AggregatorFactories.Builder subFactoriesBuilder) throws IOException;
262262

263-
public ValuesSourceConfig<VS> config(SearchContext context, String field, Script script) {
263+
public ValuesSourceConfig<VS> config(QueryShardContext queryShardContext, String field, Script script) {
264264

265265
ValueType valueType = this.valueType != null ? this.valueType : targetValueType;
266266

@@ -282,7 +282,7 @@ public ValuesSourceConfig<VS> config(SearchContext context, String field, Script
282282
return config.format(resolveFormat(format, valueType));
283283
}
284284

285-
MappedFieldType fieldType = context.smartNameFieldType(field);
285+
MappedFieldType fieldType = queryShardContext.getMapperService().fullName(field);
286286
if (fieldType == null) {
287287
ValuesSourceType valuesSourceType = valueType != null ? valueType.getValuesSourceType() : this.valuesSourceType;
288288
ValuesSourceConfig<VS> config = new ValuesSourceConfig<>(valuesSourceType);
@@ -291,7 +291,7 @@ public ValuesSourceConfig<VS> config(SearchContext context, String field, Script
291291
return config.unmapped(true);
292292
}
293293

294-
IndexFieldData<?> indexFieldData = context.getForField(fieldType);
294+
IndexFieldData<?> indexFieldData = queryShardContext.getForField(fieldType);
295295

296296
ValuesSourceConfig<VS> config;
297297
if (valuesSourceType == ValuesSourceType.ANY) {

modules/aggs-matrix-stats/src/main/java/org/elasticsearch/search/aggregations/support/ArrayValuesSourceAggregatorFactory.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.search.aggregations.support;
2121

22+
import org.elasticsearch.index.query.QueryShardContext;
2223
import org.elasticsearch.search.aggregations.Aggregator;
2324
import org.elasticsearch.search.aggregations.AggregatorFactories;
2425
import org.elasticsearch.search.aggregations.AggregatorFactory;
@@ -36,35 +37,44 @@ public abstract class ArrayValuesSourceAggregatorFactory<VS extends ValuesSource
3637
protected Map<String, ValuesSourceConfig<VS>> configs;
3738

3839
public ArrayValuesSourceAggregatorFactory(String name, Map<String, ValuesSourceConfig<VS>> configs,
39-
SearchContext context, AggregatorFactory parent,
40+
QueryShardContext queryShardContext, AggregatorFactory parent,
4041
AggregatorFactories.Builder subFactoriesBuilder,
4142
Map<String, Object> metaData) throws IOException {
42-
super(name, context, parent, subFactoriesBuilder, metaData);
43+
super(name, queryShardContext, parent, subFactoriesBuilder, metaData);
4344
this.configs = configs;
4445
}
4546

4647
@Override
47-
public Aggregator createInternal(Aggregator parent, boolean collectsFromSingleBucket, List<PipelineAggregator> pipelineAggregators,
48-
Map<String, Object> metaData) throws IOException {
48+
public Aggregator createInternal(SearchContext searchContext,
49+
Aggregator parent,
50+
boolean collectsFromSingleBucket,
51+
List<PipelineAggregator> pipelineAggregators,
52+
Map<String, Object> metaData) throws IOException {
4953
HashMap<String, VS> valuesSources = new HashMap<>();
5054

5155
for (Map.Entry<String, ValuesSourceConfig<VS>> config : configs.entrySet()) {
52-
VS vs = config.getValue().toValuesSource(context.getQueryShardContext());
56+
VS vs = config.getValue().toValuesSource(queryShardContext);
5357
if (vs != null) {
5458
valuesSources.put(config.getKey(), vs);
5559
}
5660
}
5761
if (valuesSources.isEmpty()) {
58-
return createUnmapped(parent, pipelineAggregators, metaData);
62+
return createUnmapped(searchContext, parent, pipelineAggregators, metaData);
5963
}
60-
return doCreateInternal(valuesSources, parent, collectsFromSingleBucket, pipelineAggregators, metaData);
64+
return doCreateInternal(valuesSources, searchContext, parent,
65+
collectsFromSingleBucket, pipelineAggregators, metaData);
6166
}
6267

63-
protected abstract Aggregator createUnmapped(Aggregator parent, List<PipelineAggregator> pipelineAggregators,
64-
Map<String, Object> metaData) throws IOException;
68+
protected abstract Aggregator createUnmapped(SearchContext searchContext,
69+
Aggregator parent,
70+
List<PipelineAggregator> pipelineAggregators,
71+
Map<String, Object> metaData) throws IOException;
6572

66-
protected abstract Aggregator doCreateInternal(Map<String, VS> valuesSources, Aggregator parent, boolean collectsFromSingleBucket,
67-
List<PipelineAggregator> pipelineAggregators,
68-
Map<String, Object> metaData) throws IOException;
73+
protected abstract Aggregator doCreateInternal(Map<String, VS> valuesSources,
74+
SearchContext searchContext,
75+
Aggregator parent,
76+
boolean collectsFromSingleBucket,
77+
List<PipelineAggregator> pipelineAggregators,
78+
Map<String, Object> metaData) throws IOException;
6979

7080
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/action/PainlessExecuteAction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -551,9 +551,9 @@ private static Response prepareRamIndex(Request request,
551551
ParsedDocument parsedDocument = indexService.mapperService().documentMapper().parse(sourceToParse);
552552
indexWriter.addDocuments(parsedDocument.docs());
553553
try (IndexReader indexReader = DirectoryReader.open(indexWriter)) {
554-
final long absoluteStartMillis = System.currentTimeMillis();
555554
final IndexSearcher searcher = new IndexSearcher(indexReader);
556555
searcher.setQueryCache(null);
556+
final long absoluteStartMillis = System.currentTimeMillis();
557557
QueryShardContext context =
558558
indexService.newQueryShardContext(0, searcher, () -> absoluteStartMillis, null);
559559
return handler.apply(context, indexReader.leaves().get(0));

modules/parent-join/src/main/java/org/elasticsearch/join/aggregations/ChildrenAggregationBuilder.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.elasticsearch.common.xcontent.XContentParser;
2828
import org.elasticsearch.index.fielddata.plain.SortedSetDVOrdinalsIndexFieldData;
2929
import org.elasticsearch.index.mapper.MappedFieldType;
30+
import org.elasticsearch.index.query.QueryShardContext;
3031
import org.elasticsearch.join.mapper.ParentIdFieldMapper;
3132
import org.elasticsearch.join.mapper.ParentJoinFieldMapper;
3233
import org.elasticsearch.search.aggregations.AggregationBuilder;
@@ -39,7 +40,6 @@
3940
import org.elasticsearch.search.aggregations.support.ValuesSourceAggregatorFactory;
4041
import org.elasticsearch.search.aggregations.support.ValuesSourceConfig;
4142
import org.elasticsearch.search.aggregations.support.ValuesSourceType;
42-
import org.elasticsearch.search.internal.SearchContext;
4343

4444
import java.io.IOException;
4545
import java.util.Map;
@@ -95,29 +95,29 @@ protected void innerWriteTo(StreamOutput out) throws IOException {
9595
}
9696

9797
@Override
98-
protected ValuesSourceAggregatorFactory<WithOrdinals> innerBuild(SearchContext context,
99-
ValuesSourceConfig<WithOrdinals> config,
100-
AggregatorFactory parent,
101-
Builder subFactoriesBuilder) throws IOException {
102-
return new ChildrenAggregatorFactory(name, config, childFilter, parentFilter, context, parent,
98+
protected ValuesSourceAggregatorFactory<WithOrdinals> innerBuild(QueryShardContext queryShardContext,
99+
ValuesSourceConfig<WithOrdinals> config,
100+
AggregatorFactory parent,
101+
Builder subFactoriesBuilder) throws IOException {
102+
return new ChildrenAggregatorFactory(name, config, childFilter, parentFilter, queryShardContext, parent,
103103
subFactoriesBuilder, metaData);
104104
}
105105

106106
@Override
107-
protected ValuesSourceConfig<WithOrdinals> resolveConfig(SearchContext context) {
107+
protected ValuesSourceConfig<WithOrdinals> resolveConfig(QueryShardContext queryShardContext) {
108108
ValuesSourceConfig<WithOrdinals> config = new ValuesSourceConfig<>(ValuesSourceType.BYTES);
109-
joinFieldResolveConfig(context, config);
109+
joinFieldResolveConfig(queryShardContext, config);
110110
return config;
111111
}
112112

113-
private void joinFieldResolveConfig(SearchContext context, ValuesSourceConfig<WithOrdinals> config) {
114-
ParentJoinFieldMapper parentJoinFieldMapper = ParentJoinFieldMapper.getMapper(context.mapperService());
113+
private void joinFieldResolveConfig(QueryShardContext queryShardContext, ValuesSourceConfig<WithOrdinals> config) {
114+
ParentJoinFieldMapper parentJoinFieldMapper = ParentJoinFieldMapper.getMapper(queryShardContext.getMapperService());
115115
ParentIdFieldMapper parentIdFieldMapper = parentJoinFieldMapper.getParentIdFieldMapper(childType, false);
116116
if (parentIdFieldMapper != null) {
117117
parentFilter = parentIdFieldMapper.getParentFilter();
118118
childFilter = parentIdFieldMapper.getChildFilter(childType);
119119
MappedFieldType fieldType = parentIdFieldMapper.fieldType();
120-
final SortedSetDVOrdinalsIndexFieldData fieldData = context.getForField(fieldType);
120+
final SortedSetDVOrdinalsIndexFieldData fieldData = queryShardContext.getForField(fieldType);
121121
config.fieldContext(new FieldContext(fieldType.name(), fieldData, fieldType));
122122
} else {
123123
config.unmapped(true);

0 commit comments

Comments
 (0)