Skip to content

Commit eb8f96b

Browse files
committed
Merge commit 'c2f4a60c792224fa2c9bb5f091f4282d14cd13be' into elastic2.0
2 parents 8e3f4eb + c2f4a60 commit eb8f96b

File tree

4 files changed

+179
-34
lines changed

4 files changed

+179
-34
lines changed

src/main/java/org/elasticsearch/plugin/nlpcn/executors/CSVResultsExtractor.java

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid;
1212
import org.elasticsearch.search.aggregations.metrics.MetricsAggregator;
1313
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;
1416
import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric;
17+
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
1518
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
19+
import org.nlpcn.es4sql.Util;
1620

1721
import java.util.*;
1822

@@ -26,7 +30,7 @@ public CSVResultsExtractor() {
2630
this.currentLineIndex = 0;
2731
}
2832

29-
public CSVResult extractResults(Object queryResult, boolean flat, String separator) {
33+
public CSVResult extractResults(Object queryResult, boolean flat, String separator) throws CsvExtractorException {
3034
if(queryResult instanceof SearchHits){
3135
SearchHit[] hits = ((SearchHits) queryResult).getHits();
3236
List<Map<String,Object>> docsAsMap = new ArrayList<>();
@@ -46,7 +50,6 @@ public CSVResult extractResults(Object queryResult, boolean flat, String separat
4650
}
4751

4852
//todo: need to handle more options for aggregations:
49-
//NumericMetricsAggregation.Multi : ExtendedStats,Stats,Percentiles
5053
//Aggregations that inhrit from base
5154
//ScriptedMetric
5255
//TopHits
@@ -58,15 +61,15 @@ public CSVResult extractResults(Object queryResult, boolean flat, String separat
5861
return null;
5962
}
6063

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 {
6265
if(allNumericAggregations(aggregations)){
6366
lines.get(this.currentLineIndex).addAll(fillHeaderAndCreateLineForNumericAggregations(aggregations, headers));
6467
return;
6568
}
6669
//aggregations with size one only supported when not metrics.
6770
List<Aggregation> aggregationList = aggregations.asList();
6871
if(aggregationList.size() > 1){
69-
//todo: throw exception
72+
throw new CsvExtractorException("currently support only one aggregation at same level (Except for numeric metrics)");
7073
}
7174
Aggregation aggregation = aggregationList.get(0);
7275
//we want to skip singleBucketAggregations (nested,reverse_nested,filters)
@@ -113,7 +116,7 @@ private void handleAggregations(Aggregations aggregations, List<String> headers
113116

114117
}
115118

116-
private List<String> fillHeaderAndCreateLineForNumericAggregations(Aggregations aggregations, List<String> header) {
119+
private List<String> fillHeaderAndCreateLineForNumericAggregations(Aggregations aggregations, List<String> header) throws CsvExtractorException {
117120
List<String> line = new ArrayList<>();
118121
List<Aggregation> aggregationList = aggregations.asList();
119122
for(Aggregation aggregation : aggregationList){
@@ -122,17 +125,69 @@ private List<String> fillHeaderAndCreateLineForNumericAggregations(Aggregations
122125
return line;
123126
}
124127

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 {
126129
String name = aggregation.getName();
127-
if(!header.contains(name)){
128-
header.add(aggregation.getName());
129-
}
130+
130131
if(aggregation instanceof NumericMetricsAggregation.SingleValue){
132+
if(!header.contains(name)){
133+
header.add(name);
134+
}
131135
line.add(((NumericMetricsAggregation.SingleValue) aggregation).getValueAsString());
132136
}
133137
//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+
}
134177
else {
178+
throw new CsvExtractorException("unknown NumericMetricsAggregation" + aggregation.getClass());
179+
}
180+
}
135181

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+
}
136191
}
137192
}
138193

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.elasticsearch.plugin.nlpcn.executors;
2+
3+
/**
4+
* Created by Eliran on 29/12/2015.
5+
*/
6+
public class CsvExtractorException extends Exception {
7+
public CsvExtractorException(String message) {
8+
super(message);
9+
}
10+
}

src/main/java/org/nlpcn/es4sql/Util.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,15 @@ public static String extendedToString(SQLExpr sqlExpr) {
7979
}
8080
return sqlExpr.toString();
8181
}
82+
83+
public static String[] concatStringsArrays(String[] a1,String[] a2){
84+
String[] strings = new String[a1.length + a2.length];
85+
for(int i=0;i<a1.length;i++){
86+
strings[i] = a1[i];
87+
}
88+
for(int i = 0;i<a2.length;i++){
89+
strings[a1.length+i] = a2[i];
90+
}
91+
return strings;
92+
}
8293
}

0 commit comments

Comments
 (0)