Skip to content

Commit 569cc6b

Browse files
committed
#531 add include and exclude
1 parent 7af652c commit 569cc6b

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>org.nlpcn</groupId>
55
<artifactId>elasticsearch-sql</artifactId>
6-
<version>6.2.3.2</version>
6+
<version>6.2.3.3</version>
77
<packaging>jar</packaging>
88
<description>Query elasticsearch using SQL</description>
99
<name>elasticsearch-sql</name>

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.nlpcn.es4sql.query.maker;
22

3+
import java.io.IOException;
34
import java.math.BigDecimal;
45
import java.time.ZoneOffset;
56
import java.util.*;
67

8+
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
9+
import org.elasticsearch.common.xcontent.XContentParser;
10+
import org.elasticsearch.common.xcontent.json.JsonXContent;
711
import org.elasticsearch.join.aggregations.JoinAggregationBuilders;
812
import org.elasticsearch.script.Script;
913
import org.elasticsearch.script.ScriptType;
@@ -17,6 +21,7 @@
1721
import org.elasticsearch.search.aggregations.bucket.nested.ReverseNestedAggregationBuilder;
1822
import org.elasticsearch.search.aggregations.bucket.range.RangeAggregationBuilder;
1923
import org.elasticsearch.search.aggregations.bucket.range.DateRangeAggregationBuilder;
24+
import org.elasticsearch.search.aggregations.bucket.terms.IncludeExclude;
2025
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
2126
import org.elasticsearch.search.aggregations.metrics.geobounds.GeoBoundsAggregationBuilder;
2227
import org.elasticsearch.search.aggregations.metrics.percentiles.PercentilesAggregationBuilder;
@@ -253,6 +258,7 @@ private AggregationBuilder termsAgg(MethodField field) throws SqlParseException
253258
String aggName = gettAggNameFromParamsOrAlias(field);
254259
TermsAggregationBuilder terms = AggregationBuilders.terms(aggName);
255260
String value = null;
261+
IncludeExclude include = null, exclude = null;
256262
for (KVValue kv : field.getParams()) {
257263
value = kv.value.toString();
258264
switch (kv.key.toLowerCase()) {
@@ -288,10 +294,27 @@ private AggregationBuilder termsAgg(MethodField field) throws SqlParseException
288294
case "execution_hint":
289295
terms.executionHint(value);
290296
break;
297+
case "include":
298+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, value)) {
299+
parser.nextToken();
300+
include = IncludeExclude.parseInclude(parser);
301+
} catch (IOException e) {
302+
throw new SqlParseException("parse include[" + value + "] error: " + e.getMessage());
303+
}
304+
break;
305+
case "exclude":
306+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, value)) {
307+
parser.nextToken();
308+
exclude = IncludeExclude.parseExclude(parser);
309+
} catch (IOException e) {
310+
throw new SqlParseException("parse exclude[" + value + "] error: " + e.getMessage());
311+
}
312+
break;
291313
default:
292314
throw new SqlParseException("terms aggregation err or not define field " + kv.toString());
293315
}
294316
}
317+
terms.includeExclude(IncludeExclude.merge(include, exclude));
295318
return terms;
296319
}
297320

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ public void multiMatchQuery() throws IOException, SqlParseException, SQLFeatureN
8484
assertThat(result.replaceAll("\\s+", ""), equalTo(expectedOutput.replaceAll("\\s+", "")));
8585
}
8686

87+
@Test
88+
public void termsIncludeExcludeExplainTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
89+
System.out.println(explain("SELECT * FROM index GROUP BY terms(field='correspond_brand_name',size='10',alias='correspond_brand_name',include='\".*sport.*\"',exclude='\"water_.*\"')"));
90+
System.out.println(explain("SELECT * FROM index GROUP BY terms(field='correspond_brand_name',size='10',alias='correspond_brand_name',include='[\"mazda\", \"honda\"]',exclude='[\"rover\", \"jensen\"]')"));
91+
System.out.println(explain("SELECT * FROM index GROUP BY terms(field='correspond_brand_name',size='10',alias='correspond_brand_name',include='{\"partition\":0,\"num_partitions\":20}')"));
92+
}
93+
8794
private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException {
8895
SearchDao searchDao = MainTestSuite.getSearchDao();
8996
SqlElasticRequestBuilder requestBuilder = searchDao.explain(sql).explain();

0 commit comments

Comments
 (0)