Skip to content

Commit 2c9b063

Browse files
committed
NLPchina#120 ignore_unavailable hint
1 parent 0d41fcc commit 2c9b063

File tree

6 files changed

+38
-2
lines changed

6 files changed

+38
-2
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public static Hint getHintFromString(String hintAsString){
4545
}
4646
return new Hint(HintType.USE_SCROLL, new Object[]{docsPerShardFetch,timeout});
4747
}
48+
if(hintAsString.startsWith("! IGNORE_UNAVAILABLE")){
49+
return new Hint(HintType.IGNORE_UNAVAILABLE,null);
50+
}
51+
4852
return null;
4953
}
5054

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ public enum HintType
1212
JOIN_LIMIT,
1313
USE_NESTED_LOOPS,
1414
NL_MULTISEARCH_SIZE,
15-
USE_SCROLL;
16-
15+
USE_SCROLL,
16+
IGNORE_UNAVAILABLE;
1717
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
139139
setLimit(select.getOffset(), select.getRowCount());
140140

141141
request.setSearchType(SearchType.DEFAULT);
142+
updateWithIndicesOptionsIfNeeded(select,request);
142143
SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request);
143144
return sqlElasticRequestBuilder;
144145
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
4747
if(!usedScroll){
4848
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
4949
}
50+
updateWithIndicesOptionsIfNeeded(select,request);
5051

5152
SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request);
5253
return sqlElasticRequestBuilder;

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
import org.elasticsearch.action.ActionRequestBuilder;
44
import org.elasticsearch.action.search.SearchRequestBuilder;
5+
import org.elasticsearch.action.support.IndicesOptions;
56
import org.elasticsearch.client.Client;
67
import org.nlpcn.es4sql.domain.Query;
78
import org.nlpcn.es4sql.domain.Select;
9+
import org.nlpcn.es4sql.domain.hints.Hint;
10+
import org.nlpcn.es4sql.domain.hints.HintType;
811
import org.nlpcn.es4sql.exception.SqlParseException;
912

1013
/**
@@ -23,6 +26,20 @@ public QueryAction(Client client, Query query) {
2326
this.query = query;
2427
}
2528

29+
protected void updateWithIndicesOptionsIfNeeded(Select select,SearchRequestBuilder request) {
30+
boolean ignore = false;
31+
for(Hint hint : select.getHints()){
32+
if(hint.getType() == HintType.IGNORE_UNAVAILABLE){
33+
ignore = true;
34+
break;
35+
}
36+
}
37+
if(ignore)
38+
//saving the defaults from TransportClient search
39+
request.setIndicesOptions(IndicesOptions.fromOptions(true, false, true, false, IndicesOptions.strictExpandOpenAndForbidClosed()));
40+
}
41+
42+
2643

2744
/**
2845
* Prepare the request, and return ES request.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.elasticsearch.common.joda.time.DateTime;
99
import org.elasticsearch.common.joda.time.format.DateTimeFormat;
1010
import org.elasticsearch.common.joda.time.format.DateTimeFormatter;
11+
import org.elasticsearch.indices.IndexMissingException;
1112
import org.elasticsearch.search.SearchHit;
1213
import org.elasticsearch.search.SearchHits;
1314
import org.junit.Assert;
@@ -801,6 +802,18 @@ public void nestedOnInTermsQuery() throws IOException, SqlParseException, SQLFea
801802
Assert.assertEquals(3, response.getTotalHits());
802803
}
803804

805+
@Test
806+
public void multipleIndicesOneNotExistWithHint() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
807+
SearchHits response = query(String.format("SELECT /*! IGNORE_UNAVAILABLE */ * FROM %s,%s ", TEST_INDEX,"badindex"));
808+
Assert.assertTrue(response.getTotalHits() > 0);
809+
}
810+
811+
@Test(expected=IndexMissingException.class)
812+
public void multipleIndicesOneNotExistWithoutHint() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
813+
SearchHits response = query(String.format("SELECT * FROM %s,%s ", TEST_INDEX,"badindex"));
814+
Assert.assertTrue(response.getTotalHits() > 0);
815+
}
816+
804817
private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException {
805818
SearchDao searchDao = MainTestSuite.getSearchDao();
806819
SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query);

0 commit comments

Comments
 (0)