@@ -85,6 +85,13 @@ public static class Builder extends FieldMapper.Builder {
85
85
86
86
private final Parameter <Map <String , String >> meta = Parameter .metaParam ();
87
87
88
+ /**
89
+ * Parameter that marks this field as a time series metric defining its time series metric type.
90
+ * For the numeric fields gauge and counter metric types are
91
+ * supported
92
+ */
93
+ private final Parameter <TimeSeriesParams .MetricType > metric ;
94
+
88
95
public Builder (String name , Settings settings ) {
89
96
this (name , IGNORE_MALFORMED_SETTING .get (settings ), COERCE_SETTING .get (settings ));
90
97
}
@@ -95,6 +102,18 @@ public Builder(String name, boolean ignoreMalformedByDefault, boolean coerceByDe
95
102
= Parameter .explicitBoolParam ("ignore_malformed" , true , m -> toType (m ).ignoreMalformed , ignoreMalformedByDefault );
96
103
this .coerce
97
104
= Parameter .explicitBoolParam ("coerce" , true , m -> toType (m ).coerce , coerceByDefault );
105
+
106
+ this .metric = TimeSeriesParams .metricParam (
107
+ m -> toType (m ).metricType ,
108
+ TimeSeriesParams .MetricType .gauge ,
109
+ TimeSeriesParams .MetricType .counter
110
+ ).addValidator (v -> {
111
+ if (v != null && hasDocValues .getValue () == false ) {
112
+ throw new IllegalArgumentException (
113
+ "Field [" + TimeSeriesParams .TIME_SERIES_METRIC_PARAM + "] requires that [" + hasDocValues .name + "] is true"
114
+ );
115
+ }
116
+ });
98
117
}
99
118
100
119
Builder scalingFactor (double scalingFactor ) {
@@ -107,15 +126,28 @@ Builder nullValue(double nullValue) {
107
126
return this ;
108
127
}
109
128
129
+ public Builder metric (TimeSeriesParams .MetricType metric ) {
130
+ this .metric .setValue (metric );
131
+ return this ;
132
+ }
133
+
110
134
@ Override
111
135
protected List <Parameter <?>> getParameters () {
112
- return Arrays .asList (indexed , hasDocValues , stored , ignoreMalformed , meta , scalingFactor , coerce , nullValue );
136
+ return Arrays .asList (indexed , hasDocValues , stored , ignoreMalformed , meta , scalingFactor , coerce , nullValue , metric );
113
137
}
114
138
115
139
@ Override
116
140
public ScaledFloatFieldMapper build (MapperBuilderContext context ) {
117
- ScaledFloatFieldType type = new ScaledFloatFieldType (context .buildFullName (name ), indexed .getValue (), stored .getValue (),
118
- hasDocValues .getValue (), meta .getValue (), scalingFactor .getValue (), nullValue .getValue ());
141
+ ScaledFloatFieldType type = new ScaledFloatFieldType (
142
+ context .buildFullName (name ),
143
+ indexed .getValue (),
144
+ stored .getValue (),
145
+ hasDocValues .getValue (),
146
+ meta .getValue (),
147
+ scalingFactor .getValue (),
148
+ nullValue .getValue (),
149
+ metric .getValue ()
150
+ );
119
151
return new ScaledFloatFieldMapper (name , type , multiFieldsBuilder .build (this , context ), copyTo .build (), this );
120
152
}
121
153
}
@@ -126,16 +158,20 @@ public static final class ScaledFloatFieldType extends SimpleMappedFieldType {
126
158
127
159
private final double scalingFactor ;
128
160
private final Double nullValue ;
161
+ private final TimeSeriesParams .MetricType metricType ;
162
+
129
163
130
164
public ScaledFloatFieldType (String name , boolean indexed , boolean stored , boolean hasDocValues ,
131
- Map <String , String > meta , double scalingFactor , Double nullValue ) {
165
+ Map <String , String > meta , double scalingFactor , Double nullValue ,
166
+ TimeSeriesParams .MetricType metricType ) {
132
167
super (name , indexed , stored , hasDocValues , TextSearchInfo .SIMPLE_MATCH_WITHOUT_TERMS , meta );
133
168
this .scalingFactor = scalingFactor ;
134
169
this .nullValue = nullValue ;
170
+ this .metricType = metricType ;
135
171
}
136
172
137
173
public ScaledFloatFieldType (String name , double scalingFactor ) {
138
- this (name , true , false , true , Collections .emptyMap (), scalingFactor , null );
174
+ this (name , true , false , true , Collections .emptyMap (), scalingFactor , null , null );
139
175
}
140
176
141
177
public double getScalingFactor () {
@@ -266,6 +302,14 @@ public DocValueFormat docValueFormat(String format, ZoneId timeZone) {
266
302
private double scale (Object input ) {
267
303
return new BigDecimal (Double .toString (parse (input ))).multiply (BigDecimal .valueOf (scalingFactor )).doubleValue ();
268
304
}
305
+
306
+ /**
307
+ * If field is a time series metric field, returns its metric type
308
+ * @return the metric type or null
309
+ */
310
+ public TimeSeriesParams .MetricType getMetricType () {
311
+ return metricType ;
312
+ }
269
313
}
270
314
271
315
private final Explicit <Boolean > ignoreMalformed ;
@@ -278,6 +322,7 @@ private double scale(Object input) {
278
322
279
323
private final boolean ignoreMalformedByDefault ;
280
324
private final boolean coerceByDefault ;
325
+ private final TimeSeriesParams .MetricType metricType ;
281
326
282
327
private ScaledFloatFieldMapper (
283
328
String simpleName ,
@@ -295,6 +340,7 @@ private ScaledFloatFieldMapper(
295
340
this .coerce = builder .coerce .getValue ();
296
341
this .ignoreMalformedByDefault = builder .ignoreMalformed .getDefaultValue ().value ();
297
342
this .coerceByDefault = builder .coerce .getDefaultValue ().value ();
343
+ this .metricType = builder .metric .getValue ();
298
344
}
299
345
300
346
boolean coerce () {
@@ -317,12 +363,11 @@ protected String contentType() {
317
363
318
364
@ Override
319
365
public FieldMapper .Builder getMergeBuilder () {
320
- return new Builder (simpleName (), ignoreMalformedByDefault , coerceByDefault ).init (this );
366
+ return new Builder (simpleName (), ignoreMalformedByDefault , coerceByDefault ).metric ( metricType ). init (this );
321
367
}
322
368
323
369
@ Override
324
370
protected void parseCreateField (DocumentParserContext context ) throws IOException {
325
-
326
371
XContentParser parser = context .parser ();
327
372
Object value ;
328
373
Number numericValue = null ;
0 commit comments