Skip to content

Commit cdbaeb6

Browse files
committed
manual merge of NLPchina#152 . too many changes.
1 parent 0b15c46 commit cdbaeb6

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

src/main/java/org/nlpcn/es4sql/domain/hints/HintFactory.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ public static Hint getHintFromString(String hintAsString) throws SqlParseExcepti
5858
if(hintAsString.startsWith("! DOCS_WITH_AGGREGATION")) {
5959
String[] number = getParamsFromHint(hintAsString,"! DOCS_WITH_AGGREGATION");
6060
//todo: check if numbers etc..
61-
int docsWithAggregation = Integer.parseInt(number[0]);
62-
return new Hint(HintType.DOCS_WITH_AGGREGATION,new Object[]{docsWithAggregation});
61+
Integer[] params = new Integer[number.length];
62+
for (int i = 0; i < params.length; i++) {
63+
params[i] = Integer.parseInt(number[i]);
64+
}
65+
return new Hint(HintType.DOCS_WITH_AGGREGATION, params);
6366
}
6467
if(hintAsString.startsWith("! ROUTINGS")) {
6568
String[] routings = getParamsFromHint(hintAsString,"! ROUTINGS");

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
137137
}
138138
}
139139

140-
setLimit(getLimitFromHint());
140+
141+
setLimitFromHint(this.select.getHints());
141142

142143
request.setSearchType(SearchType.DEFAULT);
143144
updateRequestWithIndexAndRoutingOptions(select, request);
@@ -263,20 +264,27 @@ private void setIndicesAndTypes() {
263264
}
264265
}
265266

266-
private void setLimit( int size) {
267-
request.setFrom(0);
268-
269-
if (size > -1) {
270-
request.setSize(size);
271-
}
272-
}
273-
274-
public int getLimitFromHint() {
275-
for(Hint hint : this.select.getHints()){
276-
if(hint.getType() == HintType.DOCS_WITH_AGGREGATION){
277-
return (int) hint.getParams()[0];
267+
private void setLimitFromHint(List<Hint> hints) {
268+
int from = 0;
269+
int size = 0;
270+
for (Hint hint : hints) {
271+
if (hint.getType() == HintType.DOCS_WITH_AGGREGATION) {
272+
Integer[] params = (Integer[]) hint.getParams();
273+
if (params.length > 1) {
274+
// if 2 or more are given, use the first as the from and the second as the size
275+
// so it is the same as LIMIT from,size
276+
// except written as /*! DOCS_WITH_AGGREGATION(from,size) */
277+
from = params[0];
278+
size = params[1];
279+
} else if (params.length == 1) {
280+
// if only 1 parameter is given, use it as the size with a from of 0
281+
size = params[0];
282+
}
283+
break;
278284
}
279285
}
280-
return 0;
286+
request.setFrom(from);
287+
request.setSize(size);
281288
}
289+
282290
}

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,30 @@ private SqlElasticSearchRequestBuilder getSearchRequestBuilder(String query) thr
373373
}
374374

375375

376+
@Test
377+
public void testFromSizeWithAggregations() throws Exception {
378+
final String query1 = String.format("SELECT /*! DOCS_WITH_AGGREGATION(0,1) */" +
379+
" account_number FROM %s/account GROUP BY gender", TEST_INDEX);
380+
SearchResponse response1 = (SearchResponse) getSearchRequestBuilder(query1).get();
381+
382+
Assert.assertEquals(1, response1.getHits().getHits().length);
383+
Terms gender1 = response1.getAggregations().get("gender");
384+
Assert.assertEquals(2, gender1.getBuckets().size());
385+
Object account1 = response1.getHits().getHits()[0].getSource().get("account_number");
386+
387+
final String query2 = String.format("SELECT /*! DOCS_WITH_AGGREGATION(1,1) */" +
388+
" account_number FROM %s/account GROUP BY gender", TEST_INDEX);
389+
SearchResponse response2 = (SearchResponse) getSearchRequestBuilder(query2).get();
390+
391+
Assert.assertEquals(1, response2.getHits().getHits().length);
392+
Terms gender2 = response2.getAggregations().get("gender");
393+
Assert.assertEquals(2, gender2.getBuckets().size());
394+
Object account2 = response2.getHits().getHits()[0].getSource().get("account_number");
395+
396+
Assert.assertEquals(response1.getHits().getTotalHits(), response2.getHits().getTotalHits());
397+
Assert.assertNotEquals(account1, account2);
398+
}
399+
376400
@Test
377401
public void testSubAggregations() throws Exception {
378402
Set expectedAges = new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));

0 commit comments

Comments
 (0)