Skip to content

Commit 773164e

Browse files
committed
Merge pull request NLPchina#152 from ottboy4/elastic2.1.1
When doing an aggregation with docs, added the ability to set the 'from'
2 parents 6bdad98 + 1d4f6b4 commit 773164e

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
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
@@ -51,8 +51,11 @@ public static Hint getHintFromString(String hintAsString){
5151
if(hintAsString.startsWith("! DOCS_WITH_AGGREGATION")) {
5252
String[] number = getParamsFromHint(hintAsString,"! DOCS_WITH_AGGREGATION");
5353
//todo: check if numbers etc..
54-
int docsWithAggregation = Integer.parseInt(number[0]);
55-
return new Hint(HintType.DOCS_WITH_AGGREGATION,new Object[]{docsWithAggregation});
54+
Integer[] params = new Integer[number.length];
55+
for (int i = 0; i < params.length; i++) {
56+
params[i] = Integer.parseInt(number[i]);
57+
}
58+
return new Hint(HintType.DOCS_WITH_AGGREGATION, params);
5659
}
5760

5861
return null;

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

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
139139
}
140140
}
141141
}
142-
143-
setLimit(getLimitFromHint());
142+
143+
setLimitFromHint(this.select.getHints());
144144

145145
request.setSearchType(SearchType.DEFAULT);
146146
updateWithIndicesOptionsIfNeeded(select,request);
@@ -266,20 +266,26 @@ private void setIndicesAndTypes() {
266266
}
267267
}
268268

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

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,29 @@ private SqlElasticSearchRequestBuilder getSearchRequestBuilder(String query) thr
363363
return (SqlElasticSearchRequestBuilder) searchDao.explain(query).explain();
364364
}
365365

366+
@Test
367+
public void testFromSizeWithAggregations() throws Exception {
368+
final String query1 = String.format("SELECT /*! DOCS_WITH_AGGREGATION(0,1) */" +
369+
" account_number FROM %s/account GROUP BY gender", TEST_INDEX);
370+
SearchResponse response1 = (SearchResponse) getSearchRequestBuilder(query1).get();
371+
372+
Assert.assertEquals(1, response1.getHits().getHits().length);
373+
Terms gender1 = response1.getAggregations().get("gender");
374+
Assert.assertEquals(2, gender1.getBuckets().size());
375+
Object account1 = response1.getHits().getHits()[0].getSource().get("account_number");
376+
377+
final String query2 = String.format("SELECT /*! DOCS_WITH_AGGREGATION(1,1) */" +
378+
" account_number FROM %s/account GROUP BY gender", TEST_INDEX);
379+
SearchResponse response2 = (SearchResponse) getSearchRequestBuilder(query2).get();
380+
381+
Assert.assertEquals(1, response2.getHits().getHits().length);
382+
Terms gender2 = response2.getAggregations().get("gender");
383+
Assert.assertEquals(2, gender2.getBuckets().size());
384+
Object account2 = response2.getHits().getHits()[0].getSource().get("account_number");
385+
386+
Assert.assertEquals(response1.getHits().getTotalHits(), response2.getHits().getTotalHits());
387+
Assert.assertNotEquals(account1, account2);
388+
}
366389

367390
@Test
368391
public void testSubAggregations() throws Exception {

0 commit comments

Comments
 (0)