11
11
import org .elasticsearch .search .aggregations .bucket .geogrid .GeoHashGrid ;
12
12
import org .elasticsearch .search .aggregations .metrics .MetricsAggregator ;
13
13
import org .elasticsearch .search .aggregations .metrics .NumericMetricsAggregation ;
14
+ import org .elasticsearch .search .aggregations .metrics .percentiles .Percentile ;
15
+ import org .elasticsearch .search .aggregations .metrics .percentiles .Percentiles ;
14
16
import org .elasticsearch .search .aggregations .metrics .scripted .ScriptedMetric ;
17
+ import org .elasticsearch .search .aggregations .metrics .stats .Stats ;
15
18
import org .elasticsearch .search .aggregations .metrics .stats .extended .ExtendedStats ;
19
+ import org .nlpcn .es4sql .Util ;
16
20
17
21
import java .util .*;
18
22
@@ -26,7 +30,7 @@ public CSVResultsExtractor() {
26
30
this .currentLineIndex = 0 ;
27
31
}
28
32
29
- public CSVResult extractResults (Object queryResult , boolean flat , String separator ) {
33
+ public CSVResult extractResults (Object queryResult , boolean flat , String separator ) throws CsvExtractorException {
30
34
if (queryResult instanceof SearchHits ){
31
35
SearchHit [] hits = ((SearchHits ) queryResult ).getHits ();
32
36
List <Map <String ,Object >> docsAsMap = new ArrayList <>();
@@ -46,7 +50,6 @@ public CSVResult extractResults(Object queryResult, boolean flat, String separat
46
50
}
47
51
48
52
//todo: need to handle more options for aggregations:
49
- //NumericMetricsAggregation.Multi : ExtendedStats,Stats,Percentiles
50
53
//Aggregations that inhrit from base
51
54
//ScriptedMetric
52
55
//TopHits
@@ -58,15 +61,15 @@ public CSVResult extractResults(Object queryResult, boolean flat, String separat
58
61
return null ;
59
62
}
60
63
61
- private void handleAggregations (Aggregations aggregations , List <String > headers , List <List <String >> lines ) {
64
+ private void handleAggregations (Aggregations aggregations , List <String > headers , List <List <String >> lines ) throws CsvExtractorException {
62
65
if (allNumericAggregations (aggregations )){
63
66
lines .get (this .currentLineIndex ).addAll (fillHeaderAndCreateLineForNumericAggregations (aggregations , headers ));
64
67
return ;
65
68
}
66
69
//aggregations with size one only supported when not metrics.
67
70
List <Aggregation > aggregationList = aggregations .asList ();
68
71
if (aggregationList .size () > 1 ){
69
- //todo: throw exception
72
+ throw new CsvExtractorException ( "currently support only one aggregation at same level (Except for numeric metrics)" );
70
73
}
71
74
Aggregation aggregation = aggregationList .get (0 );
72
75
//we want to skip singleBucketAggregations (nested,reverse_nested,filters)
@@ -113,7 +116,7 @@ private void handleAggregations(Aggregations aggregations, List<String> headers
113
116
114
117
}
115
118
116
- private List <String > fillHeaderAndCreateLineForNumericAggregations (Aggregations aggregations , List <String > header ) {
119
+ private List <String > fillHeaderAndCreateLineForNumericAggregations (Aggregations aggregations , List <String > header ) throws CsvExtractorException {
117
120
List <String > line = new ArrayList <>();
118
121
List <Aggregation > aggregationList = aggregations .asList ();
119
122
for (Aggregation aggregation : aggregationList ){
@@ -122,17 +125,69 @@ private List<String> fillHeaderAndCreateLineForNumericAggregations(Aggregations
122
125
return line ;
123
126
}
124
127
125
- private void handleNumericMetricAggregation (List <String > header , List <String > line , Aggregation aggregation ) {
128
+ private void handleNumericMetricAggregation (List <String > header , List <String > line , Aggregation aggregation ) throws CsvExtractorException {
126
129
String name = aggregation .getName ();
127
- if (!header .contains (name )){
128
- header .add (aggregation .getName ());
129
- }
130
+
130
131
if (aggregation instanceof NumericMetricsAggregation .SingleValue ){
132
+ if (!header .contains (name )){
133
+ header .add (name );
134
+ }
131
135
line .add (((NumericMetricsAggregation .SingleValue ) aggregation ).getValueAsString ());
132
136
}
133
137
//todo:Numeric MultiValue - Stats,ExtendedStats,Percentile...
138
+ else if (aggregation instanceof NumericMetricsAggregation .MultiValue ){
139
+ if (aggregation instanceof Stats ) {
140
+ String [] statsHeaders = new String []{"count" , "sum" , "avg" , "min" , "max" };
141
+ boolean isExtendedStats = aggregation instanceof ExtendedStats ;
142
+ if (isExtendedStats ){
143
+ String [] extendedHeaders = new String []{"sumOfSquares" , "variance" , "stdDeviation" };
144
+ statsHeaders = Util .concatStringsArrays (statsHeaders ,extendedHeaders );
145
+ }
146
+ mergeHeadersWithPrefix (header , name , statsHeaders );
147
+ Stats stats = (Stats ) aggregation ;
148
+ line .add (stats .getCountAsString ());
149
+ line .add (stats .getSumAsString ());
150
+ line .add (stats .getAvgAsString ());
151
+ line .add (stats .getMinAsString ());
152
+ line .add (stats .getMaxAsString ());
153
+ if (isExtendedStats ){
154
+ ExtendedStats extendedStats = (ExtendedStats ) aggregation ;
155
+ line .add (extendedStats .getSumOfSquaresAsString ());
156
+ line .add (extendedStats .getVarianceAsString ());
157
+ line .add (extendedStats .getStdDeviationAsString ());
158
+ }
159
+ }
160
+ else if ( aggregation instanceof Percentiles ){
161
+ String [] percentileHeaders = new String []{"1.0" , "5.0" , "25.0" , "50.0" , "75.0" , "95.0" , "99.0" };
162
+ mergeHeadersWithPrefix (header , name , percentileHeaders );
163
+ Percentiles percentiles = (Percentiles ) aggregation ;
164
+ line .add (percentiles .percentileAsString (1.0 ));
165
+ line .add (percentiles .percentileAsString (5.0 ));
166
+ line .add (percentiles .percentileAsString (25.0 ));
167
+ line .add (percentiles .percentileAsString (50.0 ));
168
+ line .add (percentiles .percentileAsString (75 ));
169
+ line .add (percentiles .percentileAsString (95.0 ));
170
+ line .add (percentiles .percentileAsString (99.0 ));
171
+ }
172
+ else {
173
+ throw new CsvExtractorException ("unknown NumericMetricsAggregation.MultiValue:" + aggregation .getClass ());
174
+ }
175
+
176
+ }
134
177
else {
178
+ throw new CsvExtractorException ("unknown NumericMetricsAggregation" + aggregation .getClass ());
179
+ }
180
+ }
135
181
182
+ private void mergeHeadersWithPrefix (List <String > header , String prefix , String [] newHeaders ) {
183
+ for (int i = 0 ; i < newHeaders .length ; i ++) {
184
+ String newHeader = newHeaders [i ];
185
+ if (prefix != null && !prefix .equals ("" )) {
186
+ newHeader = prefix + "." + newHeader ;
187
+ }
188
+ if (!header .contains (newHeader )) {
189
+ header .add (newHeader );
190
+ }
136
191
}
137
192
}
138
193
0 commit comments