@@ -41,9 +41,40 @@ public class BucketHelpers {
41
41
*
42
42
* "insert_zeros": empty buckets will be filled with zeros for all metrics
43
43
* "skip": empty buckets will simply be ignored
44
+ * "keep_values": for empty buckets the values provided by the metrics will still be used if they are available
44
45
*/
45
46
public enum GapPolicy implements Writeable {
46
- INSERT_ZEROS ((byte ) 0 , "insert_zeros" ), SKIP ((byte ) 1 , "skip" );
47
+ INSERT_ZEROS ((byte ) 0 , "insert_zeros" , false ) {
48
+ @ Override
49
+ public Double processValue (long docCount , Double value ) {
50
+ if (Double .isInfinite (value ) || Double .isNaN (value ) || docCount == 0 ) {
51
+ return 0.0 ;
52
+ } else {
53
+ return value ;
54
+ }
55
+ }
56
+ },
57
+
58
+ SKIP ((byte ) 1 , "skip" , true ) {
59
+ @ Override
60
+ public Double processValue (long docCount , Double value ) {
61
+ if (Double .isInfinite (value ) || docCount == 0 ) {
62
+ return Double .NaN ;
63
+ } else {
64
+ return value ;
65
+ }
66
+ }
67
+ },
68
+
69
+ KEEP_VALUES ((byte ) 2 , "keep_values" , true ) {
70
+ public Double processValue (long docCount , Double value ) {
71
+ if (Double .isInfinite (value ) || Double .isNaN (value )) {
72
+ return Double .NaN ;
73
+ } else {
74
+ return value ;
75
+ }
76
+ }
77
+ };
47
78
48
79
/**
49
80
* Parse a string GapPolicy into the byte enum
@@ -76,10 +107,12 @@ public static GapPolicy parse(String text, XContentLocation tokenLocation) {
76
107
77
108
private final byte id ;
78
109
private final ParseField parseField ;
110
+ public final boolean isSkippable ;
79
111
80
- GapPolicy (byte id , String name ) {
112
+ GapPolicy (byte id , String name , boolean isSkippable ) {
81
113
this .id = id ;
82
114
this .parseField = new ParseField (name );
115
+ this .isSkippable = isSkippable ;
83
116
}
84
117
85
118
/**
@@ -113,6 +146,8 @@ public static GapPolicy readFrom(StreamInput in) throws IOException {
113
146
public String getName () {
114
147
return parseField .getPreferredName ();
115
148
}
149
+
150
+ public abstract Double processValue (long docCount , Double value );
116
151
}
117
152
118
153
/**
@@ -161,18 +196,12 @@ public static Double resolveBucketValue(MultiBucketsAggregation agg,
161
196
throw formatResolutionError (agg , aggPathAsList , propertyValue );
162
197
}
163
198
// doc count never has missing values so gap policy doesn't apply here
164
- boolean isDocCountProperty = aggPathAsList .size () == 1 && "_count" .equals (aggPathAsList .get (0 ));
165
- if (Double .isInfinite (value ) || Double .isNaN (value ) || (bucket .getDocCount () == 0 && isDocCountProperty == false )) {
166
- switch (gapPolicy ) {
167
- case INSERT_ZEROS :
168
- return 0.0 ;
169
- case SKIP :
170
- default :
171
- return Double .NaN ;
172
- }
173
- } else {
199
+ if (aggPathAsList .size () == 1 && "_count" .equals (aggPathAsList .get (0 ))) {
174
200
return value ;
201
+ } else {
202
+ return gapPolicy .processValue (bucket .getDocCount (), value );
175
203
}
204
+
176
205
}
177
206
} catch (InvalidAggregationPathException e ) {
178
207
return null ;
0 commit comments