Skip to content

Commit 1a0ab0b

Browse files
committed
Merge commit 'c3533a9c898ca879543cc35ffcd8db52f77611e6' into elastic2.0
Conflicts: src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java
2 parents 6eaff54 + c3533a9 commit 1a0ab0b

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@ public class CSVResultRestExecutor implements RestExecutor {
1818
@Override
1919
public void execute(Client client, Map<String, String> params, QueryAction queryAction, RestChannel channel) throws Exception {
2020
Object queryResult = QueryActionElasticExecutor.executeAnyAction(client, queryAction);
21-
boolean flat = false;
22-
if(params.containsKey("flat")){
23-
flat = Boolean.parseBoolean(params.get("flat"));
24-
}
21+
22+
boolean flat = getBooleanOrDefault(params,"flat",false);
2523
String separator = ",";
2624
if(params.containsKey("separator")){
2725
separator = params.get("separator");
2826
}
29-
boolean includeScore = Boolean.getBoolean(params.getOrDefault("_score", "false"));
30-
boolean includeType = Boolean.getBoolean(params.getOrDefault("_type", "false"));
27+
boolean includeScore = getBooleanOrDefault(params,"_score",false);
28+
boolean includeType = getBooleanOrDefault(params,"_type",false);
3129
CSVResult result = new CSVResultsExtractor(includeScore,includeType).extractResults(queryResult,flat,separator);
3230
String newLine = "\n";
3331
if(params.containsKey("newLine")){
@@ -38,6 +36,14 @@ public void execute(Client client, Map<String, String> params, QueryAction query
3836
channel.sendResponse(bytesRestResponse);
3937
}
4038

39+
private boolean getBooleanOrDefault(Map<String, String> params, String param, boolean defaultValue) {
40+
boolean flat = defaultValue;
41+
if(params.containsKey(param)){
42+
flat = Boolean.parseBoolean(params.get(param));
43+
}
44+
return flat;
45+
}
46+
4147
private String buildString(String separator, CSVResult result, String newLine) {
4248
StringBuilder csv = new StringBuilder();
4349
csv.append(Joiner.on(separator).join(result.getHeaders()));

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

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGrid;
1313
import org.elasticsearch.search.aggregations.metrics.MetricsAggregator;
1414
import org.elasticsearch.search.aggregations.metrics.NumericMetricsAggregation;
15+
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBounds;
1516
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentile;
1617
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
1718
import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetric;
1819
import org.elasticsearch.search.aggregations.metrics.stats.Stats;
1920
import org.elasticsearch.search.aggregations.metrics.stats.extended.ExtendedStats;
21+
import org.elasticsearch.search.aggregations.metrics.tophits.TopHits;
2022
import org.nlpcn.es4sql.Util;
2123

2224
import java.util.*;
@@ -57,8 +59,6 @@ public CSVResult extractResults(Object queryResult, boolean flat, String separat
5759
//todo: need to handle more options for aggregations:
5860
//Aggregations that inhrit from base
5961
//ScriptedMetric
60-
//TopHits
61-
//GeoBounds
6262

6363
return new CSVResult(headers,csvLines);
6464

@@ -80,13 +80,22 @@ private void handleAggregations(Aggregations aggregations, List<String> headers
8080
//we want to skip singleBucketAggregations (nested,reverse_nested,filters)
8181
if(aggregation instanceof SingleBucketAggregation){
8282
Aggregations singleBucketAggs = ((SingleBucketAggregation) aggregation).getAggregations();
83-
handleAggregations(singleBucketAggs,headers,lines);
83+
handleAggregations(singleBucketAggs, headers, lines);
8484
return;
8585
}
8686
if(aggregation instanceof NumericMetricsAggregation){
87-
handleNumericMetricAggregation(headers,lines.get(currentLineIndex),aggregation);
87+
handleNumericMetricAggregation(headers, lines.get(currentLineIndex), aggregation);
8888
return;
8989
}
90+
if(aggregation instanceof GeoBounds){
91+
handleGeoBoundsAggregation(headers, lines, (GeoBounds) aggregation);
92+
return;
93+
}
94+
if(aggregation instanceof TopHits){
95+
//todo: handle this . it returns hits... maby back to normal?
96+
//todo: read about this usages
97+
// TopHits topHitsAggregation = (TopHits) aggregation;
98+
}
9099
if(aggregation instanceof MultiBucketsAggregation){
91100
MultiBucketsAggregation bucketsAggregation = (MultiBucketsAggregation) aggregation;
92101
String name = bucketsAggregation.getName();
@@ -121,6 +130,20 @@ private void handleAggregations(Aggregations aggregations, List<String> headers
121130

122131
}
123132

133+
private void handleGeoBoundsAggregation(List<String> headers, List<List<String>> lines, GeoBounds geoBoundsAggregation) {
134+
String geoBoundAggName = geoBoundsAggregation.getName();
135+
headers.add(geoBoundAggName+".topLeft.lon");
136+
headers.add(geoBoundAggName+".topLeft.lat");
137+
headers.add(geoBoundAggName+".bottomRight.lon");
138+
headers.add(geoBoundAggName+".bottomRight.lat");
139+
List<String> line = lines.get(this.currentLineIndex);
140+
line.add(String.valueOf(geoBoundsAggregation.topLeft().getLon()));
141+
line.add(String.valueOf(geoBoundsAggregation.topLeft().getLat()));
142+
line.add(String.valueOf(geoBoundsAggregation.bottomRight().getLon()));
143+
line.add(String.valueOf(geoBoundsAggregation.bottomRight().getLat()));
144+
lines.add(line);
145+
}
146+
124147
private List<String> fillHeaderAndCreateLineForNumericAggregations(Aggregations aggregations, List<String> header) throws CsvExtractorException {
125148
List<String> line = new ArrayList<>();
126149
List<Aggregation> aggregationList = aggregations.asList();
@@ -239,7 +262,7 @@ private List<String> createHeadersAndFillDocsMap(boolean flat, SearchHit[] hits,
239262
}
240263
mergeHeaders(csvHeaders, doc, flat);
241264
if(this.includeScore){
242-
doc.put("_score",hit.score());
265+
doc.put("_score", hit.score());
243266
}
244267
if(this.includeType){
245268
doc.put("_type",hit.type());

src/main/java/org/nlpcn/es4sql/query/maker/AggMaker.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.search.aggregations.bucket.range.date.DateRangeBuilder;
2323
import org.elasticsearch.search.aggregations.bucket.terms.TermsBuilder;
2424
import org.elasticsearch.search.aggregations.metrics.ValuesSourceMetricsAggregationBuilder;
25+
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsBuilder;
2526
import org.elasticsearch.search.aggregations.metrics.scripted.ScriptedMetricBuilder;
2627
import org.elasticsearch.search.aggregations.metrics.tophits.TopHitsBuilder;
2728
import org.elasticsearch.search.sort.SortOrder;
@@ -155,6 +156,8 @@ private AggregationBuilder<?> makeRangeGroup(MethodField field) throws SqlParseE
155156
return histogram(field);
156157
case "geohash_grid":
157158
return geohashGrid(field);
159+
case "geo_bounds":
160+
return geoBounds(field);
158161
case "terms":
159162
return termsAgg(field);
160163
default:
@@ -163,6 +166,30 @@ private AggregationBuilder<?> makeRangeGroup(MethodField field) throws SqlParseE
163166

164167
}
165168

169+
private AggregationBuilder<?> geoBounds(MethodField field) throws SqlParseException {
170+
String aggName = gettAggNameFromParamsOrAlias(field);
171+
GeoBoundsBuilder boundsBuilder = AggregationBuilders.geoBounds(aggName);
172+
String value = null;
173+
for (KVValue kv : field.getParams()) {
174+
value = kv.value.toString();
175+
switch (kv.key.toLowerCase()) {
176+
case "field":
177+
boundsBuilder.field(value);
178+
break;
179+
case "wrap_longitude":
180+
boundsBuilder.wrapLongitude(Boolean.getBoolean(value));
181+
break;
182+
case "alias":
183+
case "nested":
184+
case "reverse_nested":
185+
break;
186+
default:
187+
throw new SqlParseException("geo_bounds err or not define field " + kv.toString());
188+
}
189+
}
190+
return boundsBuilder;
191+
}
192+
166193
private AggregationBuilder<?> termsAgg(MethodField field) throws SqlParseException {
167194
String aggName = gettAggNameFromParamsOrAlias(field);
168195
TermsBuilder terms = AggregationBuilders.terms(aggName);

src/test/java/org/nlpcn/es4sql/AggregationTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.elasticsearch.search.aggregations.bucket.nested.InternalReverseNested;
1818
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
1919
import org.elasticsearch.search.aggregations.metrics.avg.Avg;
20+
import org.elasticsearch.search.aggregations.metrics.geobounds.InternalGeoBounds;
2021
import org.elasticsearch.search.aggregations.metrics.max.Max;
2122
import org.elasticsearch.search.aggregations.metrics.min.Min;
2223
import org.elasticsearch.search.aggregations.metrics.percentiles.Percentiles;
@@ -462,6 +463,16 @@ public void geoHashGrid() throws SQLFeatureNotSupportedException, SqlParseExcept
462463
}
463464
}
464465

466+
@Test
467+
public void geoBounds() throws SQLFeatureNotSupportedException, SqlParseException {
468+
Aggregations result = query(String.format("SELECT * FROM %s/location GROUP BY geo_bounds(field='center',alias='bounds') ", TEST_INDEX));
469+
InternalGeoBounds bounds = result.get("bounds");
470+
Assert.assertEquals(0.5,bounds.bottomRight().getLat(),0.001);
471+
Assert.assertEquals(105.0,bounds.bottomRight().getLon(),0.001);
472+
Assert.assertEquals(5.0,bounds.topLeft().getLat(),0.001);
473+
Assert.assertEquals(100.5,bounds.topLeft().getLon(),0.001);
474+
}
475+
465476
@Test
466477
public void groupByOnNestedFieldTest() throws Exception {
467478
Aggregations result = query(String.format("SELECT COUNT(*) FROM %s/nestedType GROUP BY nested(message.info)", TEST_INDEX));

0 commit comments

Comments
 (0)