Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 81b68a8

Browse files
committed
by default no docs when aggregation. NLPchina#126
1 parent 5431c27 commit 81b68a8

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ public static Hint getHintFromString(String hintAsString){
4848
if(hintAsString.startsWith("! IGNORE_UNAVAILABLE")){
4949
return new Hint(HintType.IGNORE_UNAVAILABLE,null);
5050
}
51+
if(hintAsString.startsWith("! DOCS_WITH_AGGREGATION")) {
52+
String[] number = getParamsFromHint(hintAsString,"! DOCS_WITH_AGGREGATION");
53+
//todo: check if numbers etc..
54+
int docsWithAggregation = Integer.parseInt(number[0]);
55+
return new Hint(HintType.DOCS_WITH_AGGREGATION,new Object[]{docsWithAggregation});
56+
}
5157

5258
return null;
5359
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public enum HintType
1313
USE_NESTED_LOOPS,
1414
NL_MULTISEARCH_SIZE,
1515
USE_SCROLL,
16-
IGNORE_UNAVAILABLE;
16+
IGNORE_UNAVAILABLE,
17+
DOCS_WITH_AGGREGATION;
1718
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.nlpcn.es4sql.domain.Order;
2626
import org.nlpcn.es4sql.domain.Select;
2727
import org.nlpcn.es4sql.domain.Where;
28+
import org.nlpcn.es4sql.domain.hints.Hint;
29+
import org.nlpcn.es4sql.domain.hints.HintType;
2830
import org.nlpcn.es4sql.exception.SqlParseException;
2931
import org.nlpcn.es4sql.query.maker.AggMaker;
3032
import org.nlpcn.es4sql.query.maker.FilterMaker;
@@ -137,7 +139,8 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
137139
}
138140
}
139141
}
140-
setLimit(select.getOffset(), select.getRowCount());
142+
143+
setLimit(getLimitFromHint());
141144

142145
request.setSearchType(SearchType.DEFAULT);
143146
updateWithIndicesOptionsIfNeeded(select,request);
@@ -263,11 +266,20 @@ private void setIndicesAndTypes() {
263266
}
264267
}
265268

266-
private void setLimit(int from, int size) {
267-
request.setFrom(from);
269+
private void setLimit( int size) {
270+
request.setFrom(0);
268271

269272
if (size > -1) {
270273
request.setSize(size);
271274
}
272275
}
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];
281+
}
282+
}
283+
return 0;
284+
}
273285
}

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,21 +332,24 @@ public void topHitTest() throws IOException, SqlParseException, SQLFeatureNotSup
332332

333333

334334
private Aggregations query(String query) throws SqlParseException, SQLFeatureNotSupportedException {
335-
SearchDao searchDao = MainTestSuite.getSearchDao();
336-
SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query);
335+
SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
337336
return ((SearchResponse)select.get()).getAggregations();
338337
}
339338

339+
private SqlElasticSearchRequestBuilder getSearchRequestBuilder(String query) throws SqlParseException, SQLFeatureNotSupportedException {
340+
SearchDao searchDao = MainTestSuite.getSearchDao();
341+
return (SqlElasticSearchRequestBuilder) searchDao.explain(query);
342+
}
340343

341-
@Test
344+
345+
@Test
342346
public void testSubAggregations() throws Exception {
343347
Set expectedAges = new HashSet<>(ContiguousSet.create(Range.closed(20, 40), DiscreteDomain.integers()));
344-
final String query = String.format("SELECT * FROM %s/account GROUP BY (gender, age), (state) LIMIT 0,10", TEST_INDEX);
348+
final String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */ * FROM %s/account GROUP BY (gender, age), (state) LIMIT 0,10", TEST_INDEX);
345349

346350
Map<String, Set<Integer>> buckets = new HashMap<>();
347351

348-
SearchDao searchDao = MainTestSuite.getSearchDao();
349-
SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query);
352+
SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
350353
SearchResponse response = (SearchResponse) select.get();
351354
Aggregations result = response.getAggregations();
352355

@@ -377,10 +380,9 @@ public void testSubAggregations() throws Exception {
377380

378381
@Test
379382
public void testSimpleSubAggregations() throws Exception {
380-
final String query = String.format("SELECT * FROM %s/account GROUP BY (gender), (state) LIMIT 0,10", TEST_INDEX);
383+
final String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */ * FROM %s/account GROUP BY (gender), (state) ", TEST_INDEX);
381384

382-
SearchDao searchDao = MainTestSuite.getSearchDao();
383-
SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query);
385+
SqlElasticSearchRequestBuilder select = getSearchRequestBuilder(query);
384386
SearchResponse response = (SearchResponse) select.get();
385387
Aggregations result = response.getAggregations();
386388

@@ -649,6 +651,21 @@ public void reverseAnotherNestedGroupByOnNestedFieldWithFilterAndSumOnReverseNes
649651
}
650652

651653

654+
@Test
655+
public void docsReturnedTestWithoutDocsHint() throws Exception {
656+
String query = String.format("SELECT count(*) from %s/account", TEST_INDEX);
657+
SqlElasticSearchRequestBuilder searchRequestBuilder = getSearchRequestBuilder(query);
658+
SearchResponse response = (SearchResponse) searchRequestBuilder.get();
659+
Assert.assertEquals(0,response.getHits().getHits().length);
660+
}
661+
662+
@Test
663+
public void docsReturnedTestWithDocsHint() throws Exception {
664+
String query = String.format("SELECT /*! DOCS_WITH_AGGREGATION(10) */ count(*) from %s/account",TEST_INDEX);
665+
SqlElasticSearchRequestBuilder searchRequestBuilder = getSearchRequestBuilder(query);
666+
SearchResponse response = (SearchResponse) searchRequestBuilder.get();
667+
Assert.assertEquals(10,response.getHits().getHits().length);
668+
}
652669

653670

654671

src/test/resources/expectedOutput/search_explain.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"from" : 0,
3-
"size" : 200,
3+
"size" : 0,
44
"query" : {
55
"filtered" : {
66
"filter" : {

0 commit comments

Comments
 (0)