Skip to content

Commit a23a5c6

Browse files
committed
scroll with order for non-join queries.
1 parent 6d17e7b commit a23a5c6

File tree

4 files changed

+27
-5
lines changed

4 files changed

+27
-5
lines changed

src/_site/controllers.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ function updateWithScrollIfNeeded (query) {
170170
}
171171
else {
172172
$scope.gotNext=true;
173+
scrollBudy = handler.getBody();
174+
//not using scan type
175+
if(scrollBudy.length >0 ){
176+
$scope.resultsColumns = handler.getHead();
177+
$scope.resultsRows = scrollBudy;
178+
}
173179
}
174180
}
175181

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,9 @@ public boolean containsSubQueries() {
133133
public List<SubQueryExpression> getSubQueries() {
134134
return subQueries;
135135
}
136+
137+
public boolean isOrderdSelect(){
138+
return this.getOrderBys()!=null && this.getOrderBys().size() >0 ;
139+
}
136140
}
137141

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
4343
setSorts(select.getOrderBys());
4444
setLimit(select.getOffset(), select.getRowCount());
4545

46-
// set SearchType.
47-
boolean usedScroll = useScrollIfNeeded();
46+
boolean usedScroll = useScrollIfNeeded(select.isOrderdSelect());
4847
if(!usedScroll){
4948
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
5049
}
@@ -53,7 +52,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
5352
return sqlElasticRequestBuilder;
5453
}
5554

56-
private boolean useScrollIfNeeded() {
55+
private boolean useScrollIfNeeded(boolean existsOrderBy) {
5756
Hint scrollHint = null;
5857
for(Hint hint: select.getHints()){
5958
if(hint.getType() == HintType.USE_SCROLL){
@@ -64,8 +63,8 @@ private boolean useScrollIfNeeded() {
6463
if(scrollHint!=null) {
6564
int scrollSize = (Integer) scrollHint.getParams()[0];
6665
int timeoutInMilli = (Integer) scrollHint.getParams()[1];
67-
request.setSearchType(SearchType.SCAN)
68-
.setScroll(new TimeValue(timeoutInMilli))
66+
if(!existsOrderBy) request.setSearchType(SearchType.SCAN);
67+
request.setScroll(new TimeValue(timeoutInMilli))
6968
.setSize(scrollSize);
7069
}
7170
return scrollHint !=null ;

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,19 @@ public void useScrollWithParams() throws IOException, SqlParseException, SQLFeat
671671
Assert.assertEquals(1000,hits.getTotalHits());
672672
}
673673

674+
675+
@Test
676+
public void useScrollWithOrderByAndParams() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
677+
SearchResponse response = getSearchResponse(String.format("SELECT /*! USE_SCROLL(5,50000)*/ age,gender,firstname,balance FROM %s/account order by age", TEST_INDEX, TEST_INDEX));
678+
Assert.assertNotNull(response.getScrollId());
679+
SearchHits hits = response.getHits();
680+
Assert.assertEquals(5,hits.getHits().length);
681+
Assert.assertEquals(1000,hits.getTotalHits());
682+
for(SearchHit hit : hits){
683+
Assert.assertEquals(20,hit.sourceAsMap().get("age"));
684+
}
685+
}
686+
674687
@Test
675688
public void innerQueryTest() throws SqlParseException, SQLFeatureNotSupportedException {
676689
String query = String.format("select * from %s/dog where holdersName IN (select firstname from %s/account where firstname = 'Hattie')",TEST_INDEX,TEST_INDEX);

0 commit comments

Comments
 (0)