4
4
import org .elasticsearch .plugin .nlpcn .QueryActionElasticExecutor ;
5
5
import org .elasticsearch .plugin .nlpcn .executors .CSVResult ;
6
6
import org .elasticsearch .plugin .nlpcn .executors .CSVResultsExtractor ;
7
+ import org .elasticsearch .plugin .nlpcn .executors .CsvExtractorException ;
7
8
import org .elasticsearch .search .SearchHits ;
8
9
import org .elasticsearch .search .aggregations .Aggregations ;
9
10
import org .elasticsearch .search .aggregations .bucket .terms .Terms ;
14
15
import org .nlpcn .es4sql .query .QueryAction ;
15
16
import org .nlpcn .es4sql .query .SqlElasticSearchRequestBuilder ;
16
17
17
- import java .io .IOException ;
18
18
import java .sql .SQLFeatureNotSupportedException ;
19
19
import java .util .List ;
20
20
21
+ import static org .hamcrest .MatcherAssert .assertThat ;
22
+ import static org .hamcrest .Matchers .equalTo ;
21
23
import static org .nlpcn .es4sql .TestsConstants .TEST_INDEX ;
22
24
23
25
/**
@@ -27,7 +29,7 @@ public class CSVResultsExtractorTests {
27
29
28
30
29
31
@ Test
30
- public void simpleSearchResultNotNestedNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
32
+ public void simpleSearchResultNotNestedNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
31
33
String query = String .format ("select name,age from %s/dog order by age" ,TEST_INDEX );
32
34
CSVResult csvResult = getCsvResult (false , query );
33
35
@@ -45,7 +47,7 @@ public void simpleSearchResultNotNestedNotFlatNoAggs() throws SqlParseException,
45
47
46
48
47
49
@ Test
48
- public void simpleSearchResultWithNestedNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
50
+ public void simpleSearchResultWithNestedNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
49
51
String query = String .format ("select name,house from %s/gotCharacters" ,TEST_INDEX );
50
52
CSVResult csvResult = getCsvResult (false , query );
51
53
@@ -68,7 +70,7 @@ public void simpleSearchResultWithNestedNotFlatNoAggs() throws SqlParseException
68
70
69
71
70
72
@ Test
71
- public void simpleSearchResultWithNestedOneFieldNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
73
+ public void simpleSearchResultWithNestedOneFieldNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
72
74
String query = String .format ("select name.firstname,house from %s/gotCharacters" ,TEST_INDEX );
73
75
CSVResult csvResult = getCsvResult (false , query );
74
76
@@ -87,7 +89,7 @@ public void simpleSearchResultWithNestedOneFieldNotFlatNoAggs() throws SqlParseE
87
89
}
88
90
89
91
@ Test
90
- public void simpleSearchResultWithNestedTwoFieldsFromSameNestedNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
92
+ public void simpleSearchResultWithNestedTwoFieldsFromSameNestedNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
91
93
String query = String .format ("select name.firstname,name.lastname,house from %s/gotCharacters" , TEST_INDEX );
92
94
CSVResult csvResult = getCsvResult (false , query );
93
95
@@ -110,7 +112,7 @@ public void simpleSearchResultWithNestedTwoFieldsFromSameNestedNotFlatNoAggs() t
110
112
}
111
113
112
114
@ Test
113
- public void simpleSearchResultWithNestedWithFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
115
+ public void simpleSearchResultWithNestedWithFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
114
116
String query = String .format ("select name.firstname,house from %s/gotCharacters" ,TEST_INDEX );
115
117
CSVResult csvResult = getCsvResult (true , query );
116
118
@@ -128,7 +130,7 @@ public void simpleSearchResultWithNestedWithFlatNoAggs() throws SqlParseExceptio
128
130
129
131
}
130
132
@ Test
131
- public void joinSearchResultNotNestedNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
133
+ public void joinSearchResultNotNestedNotFlatNoAggs () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
132
134
String query = String .format ("select c.gender , h.name,h.words from %s/gotCharacters c " +
133
135
"JOIN %s/gotHouses h " +
134
136
"on h.name = c.house " ,TEST_INDEX ,TEST_INDEX );
@@ -154,7 +156,7 @@ public void joinSearchResultNotNestedNotFlatNoAggs() throws SqlParseException, S
154
156
}
155
157
156
158
@ Test
157
- public void simpleNumericValueAgg () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
159
+ public void simpleNumericValueAgg () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
158
160
String query = String .format ("select count(*) from %s/dog " ,TEST_INDEX );
159
161
CSVResult csvResult = getCsvResult (false , query );
160
162
@@ -169,7 +171,7 @@ public void simpleNumericValueAgg() throws SqlParseException, SQLFeatureNotSuppo
169
171
170
172
}
171
173
@ Test
172
- public void simpleNumericValueAggWithAlias () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
174
+ public void simpleNumericValueAggWithAlias () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
173
175
String query = String .format ("select avg(age) as myAlias from %s/dog " ,TEST_INDEX );
174
176
CSVResult csvResult = getCsvResult (false , query );
175
177
@@ -185,7 +187,7 @@ public void simpleNumericValueAggWithAlias() throws SqlParseException, SQLFeatur
185
187
}
186
188
187
189
@ Test
188
- public void twoNumericAggWithAlias () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
190
+ public void twoNumericAggWithAlias () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
189
191
String query = String .format ("select count(*) as count, avg(age) as myAlias from %s/dog " ,TEST_INDEX );
190
192
CSVResult csvResult = getCsvResult (false , query );
191
193
@@ -208,7 +210,7 @@ public void twoNumericAggWithAlias() throws SqlParseException, SQLFeatureNotSupp
208
210
}
209
211
210
212
@ Test
211
- public void aggAfterTermsGroupBy () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
213
+ public void aggAfterTermsGroupBy () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
212
214
String query = String .format ("SELECT COUNT(*) FROM %s/account GROUP BY gender" ,TEST_INDEX );
213
215
CSVResult csvResult = getCsvResult (false , query );
214
216
List <String > headers = csvResult .getHeaders ();
@@ -223,7 +225,7 @@ public void aggAfterTermsGroupBy() throws SqlParseException, SQLFeatureNotSuppor
223
225
224
226
}
225
227
@ Test
226
- public void aggAfterTwoTermsGroupBy () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
228
+ public void aggAfterTwoTermsGroupBy () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
227
229
String query = String .format ("SELECT COUNT(*) FROM %s/account where age in (35,36) GROUP BY gender,age" ,TEST_INDEX );
228
230
CSVResult csvResult = getCsvResult (false , query );
229
231
List <String > headers = csvResult .getHeaders ();
@@ -241,7 +243,7 @@ public void aggAfterTwoTermsGroupBy() throws SqlParseException, SQLFeatureNotSup
241
243
242
244
}
243
245
@ Test
244
- public void multipleAggAfterTwoTermsGroupBy () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
246
+ public void multipleAggAfterTwoTermsGroupBy () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
245
247
String query = String .format ("SELECT COUNT(*) , sum(balance) FROM %s/account where age in (35,36) GROUP BY gender,age" ,TEST_INDEX );
246
248
CSVResult csvResult = getCsvResult (false , query );
247
249
List <String > headers = csvResult .getHeaders ();
@@ -261,7 +263,7 @@ public void multipleAggAfterTwoTermsGroupBy() throws SqlParseException, SQLFeatu
261
263
}
262
264
263
265
@ Test
264
- public void dateHistogramTest () throws SqlParseException , SQLFeatureNotSupportedException , IOException {
266
+ public void dateHistogramTest () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
265
267
String query = String .format ("select count(*) from %s/online" +
266
268
" group by date_histogram('field'='insert_time','interval'='4d','alias'='days')" ,TEST_INDEX );
267
269
CSVResult csvResult = getCsvResult (false , query );
@@ -278,16 +280,77 @@ public void dateHistogramTest() throws SqlParseException, SQLFeatureNotSupported
278
280
279
281
}
280
282
283
+ @ Test
284
+ public void statsAggregationTest () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
285
+ String query = String .format ("SELECT STATS(age) FROM %s/account" , TEST_INDEX );
286
+ CSVResult csvResult = getCsvResult (false , query );
287
+ List <String > headers = csvResult .getHeaders ();
288
+ Assert .assertEquals (5 , headers .size ());
289
+ Assert .assertEquals ("STATS(age).count" , headers .get (0 ));
290
+ Assert .assertEquals ("STATS(age).sum" , headers .get (1 ));
291
+ Assert .assertEquals ("STATS(age).avg" , headers .get (2 ));
292
+ Assert .assertEquals ("STATS(age).min" , headers .get (3 ));
293
+ Assert .assertEquals ("STATS(age).max" , headers .get (4 ));
294
+
295
+ List <String > lines = csvResult .getLines ();
296
+ Assert .assertEquals (1 , lines .size ());
297
+ Assert .assertEquals ("1000.0,30171.0,30.171,20.0,40.0" , lines .get (0 ));
298
+
299
+ }
300
+
301
+ @ Test
302
+ public void extendedStatsAggregationTest () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
303
+ String query = String .format ("SELECT EXTENDED_STATS(age) FROM %s/account" , TEST_INDEX );
304
+ CSVResult csvResult = getCsvResult (false , query );
305
+ List <String > headers = csvResult .getHeaders ();
306
+ Assert .assertEquals (8 , headers .size ());
307
+ Assert .assertEquals ("EXTENDED_STATS(age).count" , headers .get (0 ));
308
+ Assert .assertEquals ("EXTENDED_STATS(age).sum" , headers .get (1 ));
309
+ Assert .assertEquals ("EXTENDED_STATS(age).avg" , headers .get (2 ));
310
+ Assert .assertEquals ("EXTENDED_STATS(age).min" , headers .get (3 ));
311
+ Assert .assertEquals ("EXTENDED_STATS(age).max" , headers .get (4 ));
312
+ Assert .assertEquals ("EXTENDED_STATS(age).sumOfSquares" , headers .get (5 ));
313
+ Assert .assertEquals ("EXTENDED_STATS(age).variance" , headers .get (6 ));
314
+ Assert .assertEquals ("EXTENDED_STATS(age).stdDeviation" , headers .get (7 ));
315
+
316
+ List <String > lines = csvResult .getLines ();
317
+ Assert .assertEquals (1 , lines .size ());
318
+ String line = lines .get (0 );
319
+ Assert .assertTrue (line .startsWith ("1000.0,30171.0,30.171,20.0,40.0,946393.0" ));
320
+ Assert .assertTrue (line .contains (",6.008" ));
321
+ Assert .assertTrue (line .contains (",36.103" ));
322
+ }
323
+
324
+ @ Test
325
+ public void percentileAggregationTest () throws SqlParseException , SQLFeatureNotSupportedException , Exception {
326
+ String query = String .format ("select percentiles(age) as per from %s/account where age > 31" , TEST_INDEX );
327
+ CSVResult csvResult = getCsvResult (false , query );
328
+ List <String > headers = csvResult .getHeaders ();
329
+ Assert .assertEquals (7 , headers .size ());
330
+ Assert .assertEquals ("per.1.0" , headers .get (0 ));
331
+ Assert .assertEquals ("per.5.0" , headers .get (1 ));
332
+ Assert .assertEquals ("per.25.0" , headers .get (2 ));
333
+ Assert .assertEquals ("per.50.0" , headers .get (3 ));
334
+ Assert .assertEquals ("per.75.0" , headers .get (4 ));
335
+ Assert .assertEquals ("per.95.0" , headers .get (5 ));
336
+ Assert .assertEquals ("per.99.0" , headers .get (6 ));
337
+
338
+
339
+ List <String > lines = csvResult .getLines ();
340
+ Assert .assertEquals (1 , lines .size ());
341
+ Assert .assertEquals ("32.0,32.0,34.0,36.0,38.0,40.0,40.0" ,lines .get (0 ));
342
+ }
343
+
344
+
281
345
282
346
/* todo: more tests:
283
- * multi_numeric extended_stats , stats , percentiles.
284
347
* filter/nested and than metric
285
348
* histogram
286
349
* geo
287
350
*/
288
351
289
352
290
- private CSVResult getCsvResult (boolean flat , String query ) throws SqlParseException , SQLFeatureNotSupportedException , IOException {
353
+ private CSVResult getCsvResult (boolean flat , String query ) throws SqlParseException , SQLFeatureNotSupportedException , Exception , CsvExtractorException {
291
354
SearchDao searchDao = MainTestSuite .getSearchDao ();
292
355
QueryAction queryAction = searchDao .explain (query );
293
356
Object execution = QueryActionElasticExecutor .executeAnyAction (searchDao .getClient (), queryAction );
0 commit comments