Skip to content

Commit 311bb04

Browse files
committed
fix nested order have bug #438
1 parent 4ea11d3 commit 311bb04

File tree

6 files changed

+31
-10
lines changed

6 files changed

+31
-10
lines changed

pom.xml

+1-1
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.1.1.4</version>
6+
<version>6.1.1.5</version>
77
<packaging>jar</packaging>
88
<description>Query elasticsearch using SQL</description>
99
<name>elasticsearch-sql</name>

src/main/java/org/nlpcn/es4sql/domain/Order.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@
66
*
77
*/
88
public class Order {
9+
private String nestedPath;
910
private String name;
1011
private String type;
1112

12-
public Order(String name, String type) {
13+
public Order(String nestedPath, String name, String type) {
14+
this.nestedPath = nestedPath;
1315
this.name = name;
1416
this.type = type;
1517
}
1618

17-
public String getName() {
19+
public String getNestedPath() {
20+
return nestedPath;
21+
}
22+
23+
public void setNestedPath(String nestedPath) {
24+
this.nestedPath = nestedPath;
25+
}
26+
27+
public String getName() {
1828
return name;
1929
}
2030

src/main/java/org/nlpcn/es4sql/domain/Select.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ public int getRowCount() {
7272
return rowCount;
7373
}
7474

75-
public void addOrderBy(String name, String type) {
75+
public void addOrderBy(String nestedPath, String name, String type) {
7676
if ("_score".equals(name)) {
7777
isQuery = true;
7878
}
79-
this.orderBys.add(new Order(name, type));
79+
this.orderBys.add(new Order(nestedPath, name, type));
8080
}
8181

8282

src/main/java/org/nlpcn/es4sql/parse/SqlParser.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,8 @@ private void findOrderBy(MySqlSelectQueryBlock query, Select select) throws SqlP
178178
private void addOrderByToSelect(Select select, List<SQLSelectOrderByItem> items, String alias) throws SqlParseException {
179179
for (SQLSelectOrderByItem sqlSelectOrderByItem : items) {
180180
SQLExpr expr = sqlSelectOrderByItem.getExpr();
181-
String orderByName = FieldMaker.makeField(expr, null, null).toString();
181+
Field f = FieldMaker.makeField(expr, null, null);
182+
String orderByName = f.toString();
182183

183184
if (sqlSelectOrderByItem.getType() == null) {
184185
sqlSelectOrderByItem.setType(SQLOrderingSpecification.ASC);
@@ -187,7 +188,7 @@ private void addOrderByToSelect(Select select, List<SQLSelectOrderByItem> items,
187188

188189
orderByName = orderByName.replace("`", "");
189190
if (alias != null) orderByName = orderByName.replaceFirst(alias + "\\.", "");
190-
select.addOrderBy(orderByName, type);
191+
select.addOrderBy(f.getNestedPath(), orderByName, type);
191192

192193
}
193194
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
import org.elasticsearch.client.Client;
99
import org.elasticsearch.common.unit.TimeValue;
1010
import org.elasticsearch.index.query.BoolQueryBuilder;
11-
import org.elasticsearch.index.query.QueryBuilders;
1211
import org.elasticsearch.script.Script;
13-
import org.elasticsearch.script.ScriptService;
1412
import org.elasticsearch.script.ScriptType;
1513
import org.elasticsearch.search.sort.FieldSortBuilder;
14+
import org.elasticsearch.search.sort.NestedSortBuilder;
15+
import org.elasticsearch.search.sort.SortBuilders;
1616
import org.elasticsearch.search.sort.SortOrder;
1717
import org.nlpcn.es4sql.domain.*;
1818
import org.nlpcn.es4sql.domain.hints.Hint;
@@ -158,7 +158,11 @@ private void setWhere(Where where) throws SqlParseException {
158158
*/
159159
private void setSorts(List<Order> orderBys) {
160160
for (Order order : orderBys) {
161-
request.addSort(order.getName(), SortOrder.valueOf(order.getType()));
161+
if (order.getNestedPath() != null) {
162+
request.addSort(SortBuilders.fieldSort(order.getName()).order(SortOrder.valueOf(order.getType())).setNestedSort(new NestedSortBuilder(order.getNestedPath())));
163+
} else {
164+
request.addSort(order.getName(), SortOrder.valueOf(order.getType()));
165+
}
162166
}
163167
}
164168

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

+6
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ public void spatialFilterExplainTest() throws IOException, SqlParseException, No
7171
assertThat(result.replaceAll("\\s+",""), equalTo(expectedOutput.replaceAll("\\s+","")));
7272
}
7373

74+
@Test
75+
public void orderByOnNestedFieldTest() throws Exception {
76+
String result = explain(String.format("SELECT * FROM %s ORDER BY NESTED('message.info','message')", TEST_INDEX_NESTED_TYPE));
77+
assertThat(result.replaceAll("\\s+", ""), equalTo("{\"from\":0,\"size\":200,\"sort\":[{\"message.info\":{\"order\":\"asc\",\"nested\":{\"path\":\"message\"}}}]}"));
78+
}
79+
7480
private String explain(String sql) throws SQLFeatureNotSupportedException, SqlParseException, InvocationTargetException, NoSuchMethodException, IllegalAccessException, IOException {
7581
SearchDao searchDao = MainTestSuite.getSearchDao();
7682
SqlElasticRequestBuilder requestBuilder = searchDao.explain(sql).explain();

0 commit comments

Comments
 (0)