|
6 | 6 | import java.util.*;
|
7 | 7 | import java.util.stream.Collectors;
|
8 | 8 |
|
| 9 | +import com.fasterxml.jackson.core.JsonFactory; |
| 10 | +import org.elasticsearch.common.ParsingException; |
9 | 11 | import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
|
10 | 12 | import org.elasticsearch.common.xcontent.NamedXContentRegistry;
|
11 | 13 | import org.elasticsearch.common.xcontent.XContentParser;
|
12 | 14 | import org.elasticsearch.common.xcontent.json.JsonXContent;
|
| 15 | +import org.elasticsearch.common.xcontent.json.JsonXContentParser; |
13 | 16 | import org.elasticsearch.join.aggregations.JoinAggregationBuilders;
|
14 | 17 | import org.elasticsearch.script.Script;
|
15 | 18 | import org.elasticsearch.script.ScriptType;
|
16 | 19 | import org.elasticsearch.search.aggregations.AbstractAggregationBuilder;
|
17 | 20 | import org.elasticsearch.search.aggregations.AggregationBuilder;
|
18 | 21 | import org.elasticsearch.search.aggregations.AggregationBuilders;
|
19 | 22 | import org.elasticsearch.search.aggregations.BucketOrder;
|
| 23 | +import org.elasticsearch.search.aggregations.InternalOrder; |
20 | 24 | import org.elasticsearch.search.aggregations.bucket.geogrid.GeoGridAggregationBuilder;
|
21 | 25 |
|
22 | 26 | import org.elasticsearch.search.aggregations.bucket.histogram.*;
|
@@ -289,7 +293,24 @@ private AggregationBuilder termsAgg(MethodField field) throws SqlParseException
|
289 | 293 | } else if ("desc".equalsIgnoreCase(value)) {
|
290 | 294 | terms.order(BucketOrder.key(false));
|
291 | 295 | } else {
|
292 |
| - throw new SqlParseException("order can only support asc/desc " + kv.toString()); |
| 296 | + List<BucketOrder> orderElements = new ArrayList<>(); |
| 297 | + try (JsonXContentParser parser = new JsonXContentParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, new JsonFactory().createParser(value))) { |
| 298 | + XContentParser.Token currentToken = parser.nextToken(); |
| 299 | + if (currentToken == XContentParser.Token.START_OBJECT) { |
| 300 | + orderElements.add(InternalOrder.Parser.parseOrderParam(parser)); |
| 301 | + } else if (currentToken == XContentParser.Token.START_ARRAY) { |
| 302 | + for (currentToken = parser.nextToken(); currentToken != XContentParser.Token.END_ARRAY; currentToken = parser.nextToken()) { |
| 303 | + if (currentToken == XContentParser.Token.START_OBJECT) { |
| 304 | + orderElements.add(InternalOrder.Parser.parseOrderParam(parser)); |
| 305 | + } else { |
| 306 | + throw new ParsingException(parser.getTokenLocation(), "Invalid token in order array"); |
| 307 | + } |
| 308 | + } |
| 309 | + } |
| 310 | + } catch (IOException e) { |
| 311 | + throw new SqlParseException("couldn't parse order: " + e.getMessage()); |
| 312 | + } |
| 313 | + terms.order(orderElements); |
293 | 314 | }
|
294 | 315 | break;
|
295 | 316 | case "alias":
|
|
0 commit comments