Skip to content

Allow setting default_metric for numerics #132626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ private static BlockLoader numericBlockLoader(WhereAndBaseName w, NumberFieldMap
false,
null,
null,
null,
false
).blockLoader(new MappedFieldType.BlockLoaderContext() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ public class ScriptScoreBenchmark {
private final ScriptModule scriptModule = new ScriptModule(Settings.EMPTY, pluginsService.filterPlugins(ScriptPlugin.class).toList());

private final Map<String, MappedFieldType> fieldTypes = Map.ofEntries(
Map.entry("n", new NumberFieldType("n", NumberType.LONG, false, false, true, true, null, Map.of(), null, false, null, null, false))
Map.entry(
"n",
new NumberFieldType("n", NumberType.LONG, false, false, true, true, null, Map.of(), null, false, null, null, null, false)
)
);
private final IndexFieldDataCache fieldDataCache = new IndexFieldDataCache.None();
private final CircuitBreakerService breakerService = new NoneCircuitBreakerService();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public class DataStreamFeatures implements FeatureSpecification {

public static final NodeFeature FAILURE_STORE_IN_LOG_DATA_STREAMS = new NodeFeature("logs_data_streams.failure_store.enabled");

public static final NodeFeature DOWNSAMPLE_DEFAULT_METRIC_FOR_NUMERICS_FIX = new NodeFeature(
"data_stream.downsample.default_metric_for_numerics_fix"
);

@Override
public Set<NodeFeature> getFeatures() {
return Set.of(DataStream.DATA_STREAM_FAILURE_STORE_FEATURE);
Expand All @@ -41,7 +45,8 @@ public Set<NodeFeature> getTestFeatures() {
DATA_STREAM_FAILURE_STORE_TSDB_FIX,
DOWNSAMPLE_AGGREGATE_DEFAULT_METRIC_FIX,
LOGS_STREAM_FEATURE,
FAILURE_STORE_IN_LOG_DATA_STREAMS
FAILURE_STORE_IN_LOG_DATA_STREAMS,
DOWNSAMPLE_DEFAULT_METRIC_FOR_NUMERICS_FIX
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ static class TokenCountFieldType extends NumberFieldMapper.NumberFieldType {
false,
null,
null,
null,
isSyntheticSource
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public SizeFieldMapper build() {

private static class SizeFieldType extends NumberFieldType {
SizeFieldType() {
super(NAME, NumberType.INTEGER, true, true, true, false, null, Collections.emptyMap(), null, false, null, null, false);
super(NAME, NumberType.INTEGER, true, true, true, false, null, Collections.emptyMap(), null, false, null, null, null, false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import org.elasticsearch.index.fielddata.SourceValueFetcherSortedNumericIndexFieldData;
import org.elasticsearch.index.fielddata.plain.SortedDoublesIndexFieldData;
import org.elasticsearch.index.fielddata.plain.SortedNumericIndexFieldData;
import org.elasticsearch.index.mapper.TimeSeriesParams.GaugeAggs;
import org.elasticsearch.index.mapper.TimeSeriesParams.MetricType;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.script.DoubleFieldScript;
Expand Down Expand Up @@ -122,6 +123,8 @@ public static final class Builder extends FieldMapper.DimensionBuilder {
*/
private final Parameter<MetricType> metric;

private final Parameter<GaugeAggs> defaultMetric;

private final Parameter<Map<String, String>> meta = Parameter.metaParam();

private final ScriptCompiler scriptCompiler;
Expand Down Expand Up @@ -223,6 +226,20 @@ public Builder(
}
}).precludesParameters(dimension);

this.defaultMetric = TimeSeriesParams.defaultMetric(m -> toType(m).defaultMetric).addValidator(s -> {
if (s != null && metric.getValue() != MetricType.GAUGE) {
throw new IllegalArgumentException(
"Field ["
+ TimeSeriesParams.TIME_SERIES_METRIC_PARAM
+ "] must be set to ["
+ MetricType.GAUGE
+ "] to allow setting of ["
+ TimeSeriesParams.TIME_SERIES_DEFAULT_METRIC_PARAM
+ "]"
);
}
});

this.script.precludesParameters(ignoreMalformed, coerce, nullValue);
addScriptValidation(script, indexed, hasDocValues);

Expand Down Expand Up @@ -278,7 +295,8 @@ protected Parameter<?>[] getParameters() {
onScriptErrorParam,
meta,
dimension,
metric };
metric,
defaultMetric };
}

@Override
Expand Down Expand Up @@ -1843,6 +1861,7 @@ public static class NumberFieldType extends SimpleMappedFieldType {
private final FieldValues<Number> scriptValues;
private final boolean isDimension;
private final MetricType metricType;
private final GaugeAggs defaultMetric;
private final IndexMode indexMode;
private final boolean isSyntheticSource;

Expand All @@ -1858,6 +1877,7 @@ public NumberFieldType(
FieldValues<Number> script,
boolean isDimension,
MetricType metricType,
GaugeAggs defaultMetric,
IndexMode indexMode,
boolean isSyntheticSource
) {
Expand All @@ -1868,6 +1888,7 @@ public NumberFieldType(
this.scriptValues = script;
this.isDimension = isDimension;
this.metricType = metricType;
this.defaultMetric = defaultMetric;
this.indexMode = indexMode;
this.isSyntheticSource = isSyntheticSource;
}
Expand All @@ -1885,6 +1906,7 @@ public NumberFieldType(
builder.scriptValues(),
builder.dimension.getValue(),
builder.metric.getValue(),
builder.defaultMetric.getValue(),
builder.indexMode,
isSyntheticSource
);
Expand All @@ -1895,7 +1917,7 @@ public NumberFieldType(String name, NumberType type) {
}

public NumberFieldType(String name, NumberType type, boolean isIndexed) {
this(name, type, isIndexed, false, true, true, null, Collections.emptyMap(), null, false, null, null, false);
this(name, type, isIndexed, false, true, true, null, Collections.emptyMap(), null, false, null, null, null, false);
}

@Override
Expand Down Expand Up @@ -2092,6 +2114,7 @@ public MetricType getMetricType() {
private final ScriptCompiler scriptCompiler;
private final Script script;
private final MetricType metricType;
private final GaugeAggs defaultMetric;
private boolean allowMultipleValues;
private final IndexVersion indexCreatedVersion;
private final boolean isSyntheticSource;
Expand Down Expand Up @@ -2123,6 +2146,7 @@ private NumberFieldMapper(
this.scriptCompiler = builder.scriptCompiler;
this.script = builder.script.getValue();
this.metricType = builder.metric.getValue();
this.defaultMetric = builder.defaultMetric.getValue();
this.allowMultipleValues = builder.allowMultipleValues;
this.indexCreatedVersion = builder.indexCreatedVersion;
this.isSyntheticSource = isSyntheticSource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@

import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.Function;

/**
Expand All @@ -21,6 +23,7 @@ public final class TimeSeriesParams {

public static final String TIME_SERIES_METRIC_PARAM = "time_series_metric";
public static final String TIME_SERIES_DIMENSION_PARAM = "time_series_dimension";
public static final String TIME_SERIES_DEFAULT_METRIC_PARAM = "default_metric";

private TimeSeriesParams() {}

Expand All @@ -33,7 +36,7 @@ private TimeSeriesParams() {}
* single number metrics, and `false` for the POSITION metric.
*/
public enum MetricType {
GAUGE(new String[] { "max", "min", "value_count", "sum" }),
GAUGE(GaugeAggs.allValuesAsString().toArray(String[]::new)),
COUNTER(new String[] { "last_value" }),
POSITION(new String[] {}, false);

Expand Down Expand Up @@ -74,6 +77,22 @@ public static MetricType fromString(String value) {
}
}

public enum GaugeAggs {
MAX,
MIN,
SUM,
VALUE_COUNT;

@Override
public final String toString() {
return name().toLowerCase(Locale.ROOT);
}

private static List<String> allValuesAsString() {
return Arrays.stream(GaugeAggs.values()).map(GaugeAggs::toString).toList();
}
}

public static FieldMapper.Parameter<MetricType> metricParam(Function<FieldMapper, MetricType> initializer, MetricType... values) {
assert values.length > 0;
EnumSet<MetricType> acceptedValues = EnumSet.noneOf(MetricType.class);
Expand All @@ -88,6 +107,17 @@ public static FieldMapper.Parameter<MetricType> metricParam(Function<FieldMapper
).acceptsNull();
}

public static FieldMapper.Parameter<GaugeAggs> defaultMetric(Function<FieldMapper, GaugeAggs> initializer) {
return FieldMapper.Parameter.restrictedEnumParam(
TIME_SERIES_DEFAULT_METRIC_PARAM,
false,
initializer,
(GaugeAggs) null,
GaugeAggs.class,
Set.of(GaugeAggs.values())
).acceptsNull();
}

public static FieldMapper.Parameter<Boolean> dimensionParam(Function<FieldMapper, Boolean> initializer) {
return FieldMapper.Parameter.boolParam(TIME_SERIES_DIMENSION_PARAM, false, initializer, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ public void testRequireDocValuesOnLongs() {
false,
null,
null,
null,
false
)
);
Expand All @@ -358,6 +359,7 @@ public void testRequireDocValuesOnDoubles() {
false,
null,
null,
null,
false
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ private static MappedFieldType unsearchable() {
false,
null,
null,
null,
false
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private ValuesSourceConfig getVSConfig(
false,
null,
null,
null,
false
);
return ValuesSourceConfig.resolveFieldOnly(ft, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,7 @@ public void testDocValuesFieldExistsForNumber() throws IOException {
false,
null,
null,
null,
false
);
docValuesFieldExistsTestCase(new ExistsQueryBuilder("f"), ft, true, i -> {
Expand All @@ -1519,6 +1520,7 @@ public void testDocValuesFieldExistsForNumberWithoutData() throws IOException {
false,
null,
null,
null,
false
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ public void testUnboundedRanges() throws IOException {
false,
null,
null,
null,
false
)
)
Expand Down Expand Up @@ -428,6 +429,7 @@ public void testNotFitIntoDouble() throws IOException {
false,
null,
null,
null,
false
);

Expand Down Expand Up @@ -710,6 +712,7 @@ private void testCase(
false,
null,
null,
null,
false
);
RangeAggregationBuilder aggregationBuilder = new RangeAggregationBuilder("test_range_agg");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public void testBuild() throws IOException {
false,
null,
null,
null,
false
);
when(searchExecutionContext.getFieldType("field")).thenReturn(numberFieldType);
Expand All @@ -174,6 +175,7 @@ public void testBuild() throws IOException {
false,
null,
null,
null,
false
);
when(searchExecutionContext.getFieldType("field")).thenReturn(numberFieldType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ private MappedFieldType counterField(String name) {
null,
false,
TimeSeriesParams.MetricType.COUNTER,
null,
IndexMode.TIME_SERIES,
false
);
Expand Down
Loading