Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d02f32c

Browse files
authored
Merge pull request NLPchina#310 from jasonliaoxiaoge/master
fix bug in aggregation query when select has script
2 parents 2d16678 + e7da364 commit d02f32c

File tree

3 files changed

+70
-17
lines changed

3 files changed

+70
-17
lines changed

src/main/java/org/nlpcn/es4sql/query/AggregationQueryAction.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
5757

5858

5959
//make groupby can reference to field alias
60-
boolean refrence = false;
61-
for (Field temp : select.getFields()) {
62-
if (temp instanceof MethodField && temp.getName().equals("script")) {
63-
MethodField scriptField = (MethodField) temp;
64-
for (KVValue kv : scriptField.getParams()) {
65-
if (kv.value.equals(field.getName())) {
66-
lastAgg = aggMaker.makeGroupAgg(scriptField);
67-
refrence = true;
68-
break;
69-
}
70-
}
71-
}
72-
}
73-
74-
if (!refrence) lastAgg = aggMaker.makeGroupAgg(field);
75-
60+
lastAgg = getGroupAgg(field, select);
7661

7762
if (lastAgg != null && lastAgg instanceof TermsBuilder && !(field instanceof MethodField)) {
7863
//if limit size is too small, increasing shard size is required
@@ -115,7 +100,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
115100

116101
for (int i = 1; i < groupBy.size(); i++) {
117102
field = groupBy.get(i);
118-
AggregationBuilder<?> subAgg = aggMaker.makeGroupAgg(field);
103+
AggregationBuilder<?> subAgg = getGroupAgg(field, select);
119104
if (subAgg instanceof TermsBuilder && !(field instanceof MethodField)) {
120105
((TermsBuilder) subAgg).size(0);
121106
}
@@ -193,6 +178,26 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
193178
SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request);
194179
return sqlElasticRequestBuilder;
195180
}
181+
private AggregationBuilder<?> getGroupAgg(Field field, Select select2) throws SqlParseException {
182+
boolean refrence = false;
183+
AggregationBuilder<?> lastAgg = null;
184+
for (Field temp : select.getFields()) {
185+
if (temp instanceof MethodField && temp.getName().equals("script")) {
186+
MethodField scriptField = (MethodField) temp;
187+
for (KVValue kv : scriptField.getParams()) {
188+
if (kv.value.equals(field.getName())) {
189+
lastAgg = aggMaker.makeGroupAgg(scriptField);
190+
refrence = true;
191+
break;
192+
}
193+
}
194+
}
195+
}
196+
197+
if (!refrence) lastAgg = aggMaker.makeGroupAgg(field);
198+
199+
return lastAgg;
200+
}
196201

197202
private AbstractAggregationBuilder wrapNestedIfNeeded(AggregationBuilder nestedBuilder, boolean reverseNested) {
198203
if (!reverseNested) return nestedBuilder;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ public void searchSanity() throws IOException, SqlParseException, NoSuchMethodEx
2828

2929
assertThat(result, equalTo(expectedOutput));
3030
}
31+
32+
@Test
33+
public void aggregationQuery() throws IOException, SqlParseException, NoSuchMethodException, IllegalAccessException, SQLFeatureNotSupportedException, InvocationTargetException {
34+
String expectedOutput = Files.toString(new File("src/test/resources/expectedOutput/aggregation_query_explain.json"), StandardCharsets.UTF_8).replaceAll("\r","");
35+
String result = explain(String.format("SELECT a, CASE WHEN gender='0' then 'aaa' else 'bbb'end a2345,count(c) FROM %s GROUP BY terms('field'='a'),a2345", TEST_INDEX));
36+
37+
assertThat(result, equalTo(expectedOutput));
38+
}
3139

3240
@Test
3341
public void searchSanityFilter() throws IOException, SqlParseException, NoSuchMethodException, IllegalAccessException, SQLFeatureNotSupportedException, InvocationTargetException {
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"from" : 0,
3+
"size" : 0,
4+
"_source" : {
5+
"includes" : [ ],
6+
"excludes" : [ ]
7+
},
8+
"fields" : [ "a", "a2345" ],
9+
"script_fields" : {
10+
"a2345" : {
11+
"script" : {
12+
"inline" : "if( (doc['gender'].value=='0')){'aaa'} else {'bbb'}"
13+
}
14+
}
15+
},
16+
"aggregations" : {
17+
"terms(field=a)" : {
18+
"terms" : {
19+
"field" : "a"
20+
},
21+
"aggregations" : {
22+
"a2345" : {
23+
"terms" : {
24+
"script" : {
25+
"inline" : "if( (doc['gender'].value=='0')){'aaa'} else {'bbb'}"
26+
},
27+
"size" : 0
28+
},
29+
"aggregations" : {
30+
"COUNT(c)" : {
31+
"value_count" : {
32+
"field" : "c"
33+
}
34+
}
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)