Small VS config cleanup (#58294) (#58442)

This commit is contained in:
Mark Tozzi 2020-06-23 10:53:06 -04:00 committed by GitHub
parent 61142a3005
commit 52806a8f89
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 32 additions and 29 deletions

View File

@ -260,7 +260,7 @@ public class DateHistogramValuesSourceBuilder
RoundingValuesSource vs = new RoundingValuesSource(numeric, preparedRounding); RoundingValuesSource vs = new RoundingValuesSource(numeric, preparedRounding);
// is specified in the builder. // is specified in the builder.
final DocValueFormat docValueFormat = format() == null ? DocValueFormat.RAW : config.format(); final DocValueFormat docValueFormat = format() == null ? DocValueFormat.RAW : config.format();
final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; final MappedFieldType fieldType = config.fieldType();
return new CompositeValuesSourceConfig(name, fieldType, vs, docValueFormat, order(), return new CompositeValuesSourceConfig(name, fieldType, vs, docValueFormat, order(),
missingBucket(), config.script() != null); missingBucket(), config.script() != null);
} else { } else {

View File

@ -136,7 +136,7 @@ public class GeoTileGridValuesSourceBuilder extends CompositeValuesSourceBuilder
if (orig instanceof ValuesSource.GeoPoint) { if (orig instanceof ValuesSource.GeoPoint) {
ValuesSource.GeoPoint geoPoint = (ValuesSource.GeoPoint) orig; ValuesSource.GeoPoint geoPoint = (ValuesSource.GeoPoint) orig;
// is specified in the builder. // is specified in the builder.
final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; final MappedFieldType fieldType = config.fieldType();
CellIdSource cellIdSource = new CellIdSource(geoPoint, precision, geoBoundingBox, GeoTileUtils::longEncode); CellIdSource cellIdSource = new CellIdSource(geoPoint, precision, geoBoundingBox, GeoTileUtils::longEncode);
return new CompositeValuesSourceConfig(name, fieldType, cellIdSource, DocValueFormat.GEOTILE, order(), return new CompositeValuesSourceConfig(name, fieldType, cellIdSource, DocValueFormat.GEOTILE, order(),
missingBucket(), script() != null); missingBucket(), script() != null);

View File

@ -118,7 +118,7 @@ public class HistogramValuesSourceBuilder extends CompositeValuesSourceBuilder<H
if (orig instanceof ValuesSource.Numeric) { if (orig instanceof ValuesSource.Numeric) {
ValuesSource.Numeric numeric = (ValuesSource.Numeric) orig; ValuesSource.Numeric numeric = (ValuesSource.Numeric) orig;
final HistogramValuesSource vs = new HistogramValuesSource(numeric, interval); final HistogramValuesSource vs = new HistogramValuesSource(numeric, interval);
final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; final MappedFieldType fieldType = config.fieldType();
return new CompositeValuesSourceConfig(name, fieldType, vs, config.format(), order(), return new CompositeValuesSourceConfig(name, fieldType, vs, config.format(), order(),
missingBucket(), script() != null); missingBucket(), script() != null);
} else { } else {

View File

@ -77,7 +77,7 @@ public class TermsValuesSourceBuilder extends CompositeValuesSourceBuilder<Terms
// This is needed because the after values are parsed even when there are no values to process. // This is needed because the after values are parsed even when there are no values to process.
vs = ValuesSource.Bytes.WithOrdinals.EMPTY; vs = ValuesSource.Bytes.WithOrdinals.EMPTY;
} }
final MappedFieldType fieldType = config.fieldContext() != null ? config.fieldContext().fieldType() : null; final MappedFieldType fieldType = config.fieldType();
final DocValueFormat format; final DocValueFormat format;
if (format() == null && fieldType instanceof DateFieldMapper.DateFieldType) { if (format() == null && fieldType instanceof DateFieldMapper.DateFieldType) {
// defaults to the raw format on date fields (preserve timestamp as longs). // defaults to the raw format on date fields (preserve timestamp as longs).

View File

@ -365,7 +365,7 @@ public abstract class ValuesSourceAggregationBuilder<AB extends ValuesSourceAggr
protected ValuesSourceConfig resolveConfig(QueryShardContext queryShardContext) { protected ValuesSourceConfig resolveConfig(QueryShardContext queryShardContext) {
return ValuesSourceConfig.resolve(queryShardContext, return ValuesSourceConfig.resolve(queryShardContext,
this.userValueTypeHint, field, script, missing, timeZone, format, this.defaultValueSourceType(), this.getType()); this.userValueTypeHint, field, script, missing, timeZone, format, this.defaultValueSourceType());
} }
protected abstract ValuesSourceAggregatorFactory innerBuild(QueryShardContext queryShardContext, protected abstract ValuesSourceAggregatorFactory innerBuild(QueryShardContext queryShardContext,

View File

@ -51,8 +51,6 @@ public class ValuesSourceConfig {
* @param format - The format string to apply to this field. Confusingly, this is used for input parsing as well as output formatting * @param format - The format string to apply to this field. Confusingly, this is used for input parsing as well as output formatting
* See https://github.com/elastic/elasticsearch/issues/47469 * See https://github.com/elastic/elasticsearch/issues/47469
* @param defaultValueSourceType - per-aggregation {@link ValuesSource} of last resort. * @param defaultValueSourceType - per-aggregation {@link ValuesSource} of last resort.
* @param aggregationName - Name of the aggregation, generally from the aggregation builder. This is used as a lookup key in the
* {@link ValuesSourceRegistry}
* @return - An initialized {@link ValuesSourceConfig} that will yield the appropriate {@link ValuesSourceType} * @return - An initialized {@link ValuesSourceConfig} that will yield the appropriate {@link ValuesSourceType}
*/ */
public static ValuesSourceConfig resolve(QueryShardContext context, public static ValuesSourceConfig resolve(QueryShardContext context,
@ -62,8 +60,7 @@ public class ValuesSourceConfig {
Object missing, Object missing,
ZoneId timeZone, ZoneId timeZone,
String format, String format,
ValuesSourceType defaultValueSourceType, ValuesSourceType defaultValueSourceType) {
String aggregationName) {
return internalResolve(context, userValueTypeHint, field, script, missing, timeZone, format, defaultValueSourceType, return internalResolve(context, userValueTypeHint, field, script, missing, timeZone, format, defaultValueSourceType,
ValuesSourceConfig::getMappingFromRegistry ValuesSourceConfig::getMappingFromRegistry
@ -261,7 +258,6 @@ public class ValuesSourceConfig {
private final DocValueFormat format; private final DocValueFormat format;
private final Object missing; private final Object missing;
private final ZoneId timeZone; private final ZoneId timeZone;
private final LongSupplier nowSupplier;
private final ValuesSource valuesSource; private final ValuesSource valuesSource;
private ValuesSourceConfig() { private ValuesSourceConfig() {
@ -290,7 +286,6 @@ public class ValuesSourceConfig {
this.missing = missing; this.missing = missing;
this.timeZone = timeZone; this.timeZone = timeZone;
this.format = format == null ? DocValueFormat.RAW : format; this.format = format == null ? DocValueFormat.RAW : format;
this.nowSupplier = nowSupplier;
if (!valid()) { if (!valid()) {
// TODO: resolve no longer generates invalid configs. Once VSConfig is immutable, we can drop this check // TODO: resolve no longer generates invalid configs. Once VSConfig is immutable, we can drop this check
@ -329,6 +324,14 @@ public class ValuesSourceConfig {
return fieldContext; return fieldContext;
} }
/**
* Convenience method for looking up the mapped field type backing this values source, if it exists.
*/
@Nullable
public MappedFieldType fieldType() {
return fieldContext == null ? null : fieldContext.fieldType();
}
public AggregationScript.LeafFactory script() { public AggregationScript.LeafFactory script() {
return script; return script;
} }

View File

@ -46,7 +46,7 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, null, "bytes", null, null, null, null, CoreValuesSourceType.BYTES, null); context, null, "bytes", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource(); ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedBinaryDocValues values = valuesSource.bytesValues(ctx); SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
@ -68,14 +68,14 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, null, "bytes", null, null, null, null, CoreValuesSourceType.BYTES, null); context, null, "bytes", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource(); ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedBinaryDocValues values = valuesSource.bytesValues(ctx); SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
assertFalse(values.advanceExact(0)); assertFalse(values.advanceExact(0));
config = ValuesSourceConfig.resolve( config = ValuesSourceConfig.resolve(
context, null, "bytes", null, "abc", null, null, CoreValuesSourceType.BYTES, null); context, null, "bytes", null, "abc", null, null, CoreValuesSourceType.BYTES);
valuesSource = (ValuesSource.Bytes) config.getValuesSource(); valuesSource = (ValuesSource.Bytes) config.getValuesSource();
values = valuesSource.bytesValues(ctx); values = valuesSource.bytesValues(ctx);
assertTrue(values.advanceExact(0)); assertTrue(values.advanceExact(0));
@ -94,13 +94,13 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, ValueType.STRING, "bytes", null, null, null, null, CoreValuesSourceType.BYTES, null); context, ValueType.STRING, "bytes", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource(); ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource();
assertNotNull(valuesSource); assertNotNull(valuesSource);
assertFalse(config.hasValues()); assertFalse(config.hasValues());
config = ValuesSourceConfig.resolve( config = ValuesSourceConfig.resolve(
context, ValueType.STRING, "bytes", null, "abc", null, null, CoreValuesSourceType.BYTES, null); context, ValueType.STRING, "bytes", null, "abc", null, null, CoreValuesSourceType.BYTES);
valuesSource = (ValuesSource.Bytes) config.getValuesSource(); valuesSource = (ValuesSource.Bytes) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedBinaryDocValues values = valuesSource.bytesValues(ctx); SortedBinaryDocValues values = valuesSource.bytesValues(ctx);
@ -122,7 +122,7 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, null, "long", null, null, null, null, CoreValuesSourceType.BYTES, null); context, null, "long", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource(); ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedNumericDocValues values = valuesSource.longValues(ctx); SortedNumericDocValues values = valuesSource.longValues(ctx);
@ -144,14 +144,14 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, null, "long", null, null, null, null, CoreValuesSourceType.BYTES, null); context, null, "long", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource(); ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedNumericDocValues values = valuesSource.longValues(ctx); SortedNumericDocValues values = valuesSource.longValues(ctx);
assertFalse(values.advanceExact(0)); assertFalse(values.advanceExact(0));
config = ValuesSourceConfig.resolve( config = ValuesSourceConfig.resolve(
context, null, "long", null, 42, null, null, CoreValuesSourceType.BYTES, null); context, null, "long", null, 42, null, null, CoreValuesSourceType.BYTES);
valuesSource = (ValuesSource.Numeric) config.getValuesSource(); valuesSource = (ValuesSource.Numeric) config.getValuesSource();
values = valuesSource.longValues(ctx); values = valuesSource.longValues(ctx);
assertTrue(values.advanceExact(0)); assertTrue(values.advanceExact(0));
@ -171,13 +171,13 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, ValueType.NUMBER, "long", null, null, null, null, CoreValuesSourceType.BYTES, null); context, ValueType.NUMBER, "long", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource(); ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource();
assertNotNull(valuesSource); assertNotNull(valuesSource);
assertFalse(config.hasValues()); assertFalse(config.hasValues());
config = ValuesSourceConfig.resolve( config = ValuesSourceConfig.resolve(
context, ValueType.NUMBER, "long", null, 42, null, null, CoreValuesSourceType.BYTES, null); context, ValueType.NUMBER, "long", null, 42, null, null, CoreValuesSourceType.BYTES);
valuesSource = (ValuesSource.Numeric) config.getValuesSource(); valuesSource = (ValuesSource.Numeric) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedNumericDocValues values = valuesSource.longValues(ctx); SortedNumericDocValues values = valuesSource.longValues(ctx);
@ -199,7 +199,7 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, null, "bool", null, null, null, null, CoreValuesSourceType.BYTES, null); context, null, "bool", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource(); ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedNumericDocValues values = valuesSource.longValues(ctx); SortedNumericDocValues values = valuesSource.longValues(ctx);
@ -221,14 +221,14 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, null, "bool", null, null, null, null, CoreValuesSourceType.BYTES, null); context, null, "bool", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource(); ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedNumericDocValues values = valuesSource.longValues(ctx); SortedNumericDocValues values = valuesSource.longValues(ctx);
assertFalse(values.advanceExact(0)); assertFalse(values.advanceExact(0));
config = ValuesSourceConfig.resolve( config = ValuesSourceConfig.resolve(
context, null, "bool", null, true, null, null, CoreValuesSourceType.BYTES, null); context, null, "bool", null, true, null, null, CoreValuesSourceType.BYTES);
valuesSource = (ValuesSource.Numeric) config.getValuesSource(); valuesSource = (ValuesSource.Numeric) config.getValuesSource();
values = valuesSource.longValues(ctx); values = valuesSource.longValues(ctx);
assertTrue(values.advanceExact(0)); assertTrue(values.advanceExact(0));
@ -248,13 +248,13 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, ValueType.BOOLEAN, "bool", null, null, null, null, CoreValuesSourceType.BYTES, null); context, ValueType.BOOLEAN, "bool", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource(); ValuesSource.Numeric valuesSource = (ValuesSource.Numeric) config.getValuesSource();
assertNotNull(valuesSource); assertNotNull(valuesSource);
assertFalse(config.hasValues()); assertFalse(config.hasValues());
config = ValuesSourceConfig.resolve( config = ValuesSourceConfig.resolve(
context, ValueType.BOOLEAN, "bool", null, true, null, null, CoreValuesSourceType.BYTES, null); context, ValueType.BOOLEAN, "bool", null, true, null, null, CoreValuesSourceType.BYTES);
valuesSource = (ValuesSource.Numeric) config.getValuesSource(); valuesSource = (ValuesSource.Numeric) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);
SortedNumericDocValues values = valuesSource.longValues(ctx); SortedNumericDocValues values = valuesSource.longValues(ctx);
@ -270,7 +270,7 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, null, TypeFieldMapper.NAME, null, null, null, null, CoreValuesSourceType.BYTES, null); context, null, TypeFieldMapper.NAME, null, null, null, null, CoreValuesSourceType.BYTES);
assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE); assertWarnings(QueryShardContext.TYPES_DEPRECATION_MESSAGE);
} }
} }
@ -286,7 +286,7 @@ public class ValuesSourceConfigTests extends ESSingleNodeTestCase {
try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) { try (Engine.Searcher searcher = indexService.getShard(0).acquireSearcher("test")) {
QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null); QueryShardContext context = indexService.newQueryShardContext(0, searcher, () -> 42L, null);
ValuesSourceConfig config = ValuesSourceConfig.resolve( ValuesSourceConfig config = ValuesSourceConfig.resolve(
context, ValueType.STRING, "alias", null, null, null, null, CoreValuesSourceType.BYTES, null); context, ValueType.STRING, "alias", null, null, null, null, CoreValuesSourceType.BYTES);
ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource(); ValuesSource.Bytes valuesSource = (ValuesSource.Bytes) config.getValuesSource();
LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0); LeafReaderContext ctx = searcher.getIndexReader().leaves().get(0);

View File

@ -61,7 +61,7 @@ public class TopMetricsAggregatorFactory extends AggregatorFactory {
ValuesSourceConfig resolved = ValuesSourceConfig.resolve( ValuesSourceConfig resolved = ValuesSourceConfig.resolve(
searchContext.getQueryShardContext(), ValueType.NUMERIC, searchContext.getQueryShardContext(), ValueType.NUMERIC,
config.getFieldName(), config.getScript(), config.getMissing(), config.getTimeZone(), null, config.getFieldName(), config.getScript(), config.getMissing(), config.getTimeZone(), null,
CoreValuesSourceType.NUMERIC, TopMetricsAggregationBuilder.NAME); CoreValuesSourceType.NUMERIC);
return new TopMetricsAggregator.MetricSource(config.getFieldName(), resolved.format(), return new TopMetricsAggregator.MetricSource(config.getFieldName(), resolved.format(),
(ValuesSource.Numeric) resolved.getValuesSource()); (ValuesSource.Numeric) resolved.getValuesSource());
}).collect(toList()); }).collect(toList());