@@ -59,12 +59,14 @@ public final class AggregationResultUtils {
59
59
* @param stats stats collector
60
60
* @return a map containing the results of the aggregation in a consumable way
61
61
*/
62
- public static Stream <Map <String , Object >> extractCompositeAggregationResults (CompositeAggregation agg ,
63
- GroupConfig groups ,
64
- Collection <AggregationBuilder > aggregationBuilders ,
65
- Collection <PipelineAggregationBuilder > pipelineAggs ,
66
- Map <String , String > fieldTypeMap ,
67
- TransformIndexerStats stats ) {
62
+ public static Stream <Map <String , Object >> extractCompositeAggregationResults (
63
+ CompositeAggregation agg ,
64
+ GroupConfig groups ,
65
+ Collection <AggregationBuilder > aggregationBuilders ,
66
+ Collection <PipelineAggregationBuilder > pipelineAggs ,
67
+ Map <String , String > fieldTypeMap ,
68
+ TransformIndexerStats stats
69
+ ) {
68
70
return agg .getBuckets ().stream ().map (bucket -> {
69
71
stats .incrementNumDocuments (bucket .getDocCount ());
70
72
Map <String , Object > document = new HashMap <>();
@@ -82,7 +84,7 @@ public static Stream<Map<String, Object>> extractCompositeAggregationResults(Com
82
84
List <String > aggNames = aggregationBuilders .stream ().map (AggregationBuilder ::getName ).collect (Collectors .toList ());
83
85
aggNames .addAll (pipelineAggs .stream ().map (PipelineAggregationBuilder ::getName ).collect (Collectors .toList ()));
84
86
85
- for (String aggName : aggNames ) {
87
+ for (String aggName : aggNames ) {
86
88
Aggregation aggResult = bucket .getAggregations ().get (aggName );
87
89
// This indicates not that the value contained in the `aggResult` is null, but that the `aggResult` is not
88
90
// present at all in the `bucket.getAggregations`. This could occur in the case of a `bucket_selector` agg, which
@@ -112,13 +114,14 @@ static AggValueExtractor getExtractor(Aggregation aggregation) {
112
114
} else {
113
115
// Execution should never reach this point!
114
116
// Creating transforms with unsupported aggregations shall not be possible
115
- throw new AggregationExtractionException ("unsupported aggregation [{}] with name [{}]" ,
117
+ throw new AggregationExtractionException (
118
+ "unsupported aggregation [{}] with name [{}]" ,
116
119
aggregation .getType (),
117
- aggregation .getName ());
120
+ aggregation .getName ()
121
+ );
118
122
}
119
123
}
120
124
121
-
122
125
@ SuppressWarnings ("unchecked" )
123
126
static void updateDocument (Map <String , Object > document , String fieldName , Object value ) {
124
127
String [] fieldTokens = fieldName .split ("\\ ." );
@@ -132,23 +135,23 @@ static void updateDocument(Map<String, Object> document, String fieldName, Objec
132
135
if (i == fieldTokens .length - 1 ) {
133
136
if (internalMap .containsKey (token )) {
134
137
if (internalMap .get (token ) instanceof Map ) {
135
- throw new AggregationExtractionException ("mixed object types of nested and non-nested fields [{}]" ,
136
- fieldName );
138
+ throw new AggregationExtractionException ("mixed object types of nested and non-nested fields [{}]" , fieldName );
137
139
} else {
138
- throw new AggregationExtractionException ("duplicate key value pairs key [{}] old value [{}] duplicate value [{}]" ,
140
+ throw new AggregationExtractionException (
141
+ "duplicate key value pairs key [{}] old value [{}] duplicate value [{}]" ,
139
142
fieldName ,
140
143
internalMap .get (token ),
141
- value );
144
+ value
145
+ );
142
146
}
143
147
}
144
148
internalMap .put (token , value );
145
149
} else {
146
150
if (internalMap .containsKey (token )) {
147
151
if (internalMap .get (token ) instanceof Map ) {
148
- internalMap = (Map <String , Object >)internalMap .get (token );
152
+ internalMap = (Map <String , Object >) internalMap .get (token );
149
153
} else {
150
- throw new AggregationExtractionException ("mixed object types of nested and non-nested fields [{}]" ,
151
- fieldName );
154
+ throw new AggregationExtractionException ("mixed object types of nested and non-nested fields [{}]" , fieldName );
152
155
}
153
156
} else {
154
157
Map <String , Object > newMap = new HashMap <>();
@@ -172,15 +175,14 @@ interface AggValueExtractor {
172
175
static class SingleValueAggExtractor implements AggValueExtractor {
173
176
@ Override
174
177
public Object value (Aggregation agg , String fieldType ) {
175
- SingleValue aggregation = (SingleValue )agg ;
178
+ SingleValue aggregation = (SingleValue ) agg ;
176
179
// If the double is invalid, this indicates sparse data
177
180
if (Numbers .isValidDouble (aggregation .value ()) == false ) {
178
181
return null ;
179
182
}
180
183
// If the type is numeric or if the formatted string is the same as simply making the value a string,
181
- // gather the `value` type, otherwise utilize `getValueAsString` so we don't lose formatted outputs.
182
- if (isNumericType (fieldType ) ||
183
- aggregation .getValueAsString ().equals (String .valueOf (aggregation .value ()))){
184
+ // gather the `value` type, otherwise utilize `getValueAsString` so we don't lose formatted outputs.
185
+ if (isNumericType (fieldType ) || aggregation .getValueAsString ().equals (String .valueOf (aggregation .value ()))) {
184
186
return aggregation .value ();
185
187
} else {
186
188
return aggregation .getValueAsString ();
@@ -191,15 +193,15 @@ public Object value(Aggregation agg, String fieldType) {
191
193
static class ScriptedMetricAggExtractor implements AggValueExtractor {
192
194
@ Override
193
195
public Object value (Aggregation agg , String fieldType ) {
194
- ScriptedMetric aggregation = (ScriptedMetric )agg ;
196
+ ScriptedMetric aggregation = (ScriptedMetric ) agg ;
195
197
return aggregation .aggregation ();
196
198
}
197
199
}
198
200
199
201
static class GeoCentroidAggExtractor implements AggValueExtractor {
200
202
@ Override
201
203
public Object value (Aggregation agg , String fieldType ) {
202
- GeoCentroid aggregation = (GeoCentroid )agg ;
204
+ GeoCentroid aggregation = (GeoCentroid ) agg ;
203
205
// if the account is `0` iff there is no contained centroid
204
206
return aggregation .count () > 0 ? aggregation .centroid ().toString () : null ;
205
207
}
@@ -208,38 +210,49 @@ public Object value(Aggregation agg, String fieldType) {
208
210
static class GeoBoundsAggExtractor implements AggValueExtractor {
209
211
@ Override
210
212
public Object value (Aggregation agg , String fieldType ) {
211
- GeoBounds aggregation = (GeoBounds )agg ;
213
+ GeoBounds aggregation = (GeoBounds ) agg ;
212
214
if (aggregation .bottomRight () == null || aggregation .topLeft () == null ) {
213
215
return null ;
214
216
}
215
217
final Map <String , Object > geoShape = new HashMap <>();
216
218
// If the two geo_points are equal, it is a point
217
219
if (aggregation .topLeft ().equals (aggregation .bottomRight ())) {
218
220
geoShape .put (ShapeParser .FIELD_TYPE .getPreferredName (), PointBuilder .TYPE .shapeName ());
219
- geoShape .put (ShapeParser .FIELD_COORDINATES .getPreferredName (),
220
- Arrays .asList (aggregation .topLeft ().getLon (), aggregation .bottomRight ().getLat ()));
221
- // If only the lat or the lon of the two geo_points are equal, than we know it should be a line
221
+ geoShape .put (
222
+ ShapeParser .FIELD_COORDINATES .getPreferredName (),
223
+ Arrays .asList (aggregation .topLeft ().getLon (), aggregation .bottomRight ().getLat ())
224
+ );
225
+ // If only the lat or the lon of the two geo_points are equal, than we know it should be a line
222
226
} else if (Double .compare (aggregation .topLeft ().getLat (), aggregation .bottomRight ().getLat ()) == 0
223
227
|| Double .compare (aggregation .topLeft ().getLon (), aggregation .bottomRight ().getLon ()) == 0 ) {
224
- geoShape .put (ShapeParser .FIELD_TYPE .getPreferredName (), LineStringBuilder .TYPE .shapeName ());
225
- geoShape .put (ShapeParser .FIELD_COORDINATES .getPreferredName (),
226
- Arrays .asList (
227
- new Double []{aggregation .topLeft ().getLon (), aggregation .topLeft ().getLat ()},
228
- new Double []{aggregation .bottomRight ().getLon (), aggregation .bottomRight ().getLat ()}));
229
- } else {
230
- // neither points are equal, we have a polygon that is a square
231
- geoShape .put (ShapeParser .FIELD_TYPE .getPreferredName (), PolygonBuilder .TYPE .shapeName ());
232
- final GeoPoint tl = aggregation .topLeft ();
233
- final GeoPoint br = aggregation .bottomRight ();
234
- geoShape .put (ShapeParser .FIELD_COORDINATES .getPreferredName (),
235
- Collections .singletonList (Arrays .asList (
236
- new Double []{tl .getLon (), tl .getLat ()},
237
- new Double []{br .getLon (), tl .getLat ()},
238
- new Double []{br .getLon (), br .getLat ()},
239
- new Double []{tl .getLon (), br .getLat ()},
240
- new Double []{tl .getLon (), tl .getLat ()})));
241
- }
228
+ geoShape .put (ShapeParser .FIELD_TYPE .getPreferredName (), LineStringBuilder .TYPE .shapeName ());
229
+ geoShape .put (
230
+ ShapeParser .FIELD_COORDINATES .getPreferredName (),
231
+ Arrays .asList (
232
+ new Double [] { aggregation .topLeft ().getLon (), aggregation .topLeft ().getLat () },
233
+ new Double [] { aggregation .bottomRight ().getLon (), aggregation .bottomRight ().getLat () }
234
+ )
235
+ );
236
+ } else {
237
+ // neither points are equal, we have a polygon that is a square
238
+ geoShape .put (ShapeParser .FIELD_TYPE .getPreferredName (), PolygonBuilder .TYPE .shapeName ());
239
+ final GeoPoint tl = aggregation .topLeft ();
240
+ final GeoPoint br = aggregation .bottomRight ();
241
+ geoShape .put (
242
+ ShapeParser .FIELD_COORDINATES .getPreferredName (),
243
+ Collections .singletonList (
244
+ Arrays .asList (
245
+ new Double [] { tl .getLon (), tl .getLat () },
246
+ new Double [] { br .getLon (), tl .getLat () },
247
+ new Double [] { br .getLon (), br .getLat () },
248
+ new Double [] { tl .getLon (), br .getLat () },
249
+ new Double [] { tl .getLon (), tl .getLat () }
250
+ )
251
+ )
252
+ );
253
+ }
242
254
return geoShape ;
243
255
}
244
256
}
257
+
245
258
}
0 commit comments