Skip to content

Commit 9f32d0d

Browse files
committed
Merge commit '2c9b06355322a9dc9d7bc3df870bd7e1361d8804' into elastic2.0
Conflicts: src/test/java/org/nlpcn/es4sql/QueryTest.java
2 parents 2fbbe60 + 2c9b063 commit 9f32d0d

File tree

7 files changed

+57
-10
lines changed

7 files changed

+57
-10
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Elasticsearch-SQL
22
=================
33
**1.X** [![1.X Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=master)](https://travis-ci.org/NLPchina/elasticsearch-sql) <br>
4-
**2.X** [![2.0.0 Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=elastic2.0)](https://travis-ci.org/NLPchina/elasticsearch-sql)
4+
**2.0.0** [![2.0.0 Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=elastic2.0)](https://travis-ci.org/NLPchina/elasticsearch-sql)<br>
5+
**2.1.0** [![2.1.0 Build Status](https://travis-ci.org/NLPchina/elasticsearch-sql.svg?branch=elastic2.0)](https://travis-ci.org/NLPchina/elasticsearch-sql)<br>
56

67
Query elasticsearch using familiar SQL syntax.
78
You can also use ES functions in SQL.
@@ -20,18 +21,23 @@ Install as plugin:
2021
Versions
2122
------------
2223

23-
| elasticsearch version | latest version | remarks |
24-
| --------------------- | ------------- | ----------------------------- |
25-
| 1.X | 1.4.6 | tested against elastic 1.4-1.6 |
26-
| 2.0.0 | 2.0.1 | delete commands not supported |
24+
| elasticsearch version | latest version | remarks | branch |
25+
| --------------------- | ------------- | ----------------------------- | ---------- |
26+
| 1.X | 1.4.7 | tested against elastic 1.4-1.6 | master |
27+
| 2.0.0 | 2.0.2 | delete commands not supported | elastic2.0 |
28+
| 2.1.0 | 2.1.0 | delete commands not supported | elastic2.1 |
2729

2830
### Elasticsearch 1.X
2931
````
30-
./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.6/elasticsearch-sql-1.4.6.zip --install sql
32+
./bin/plugin -u https://github.com/NLPchina/elasticsearch-sql/releases/download/1.4.7/elasticsearch-sql-1.4.7.zip --install sql
3133
````
3234
### Elasticsearch 2.0.0
3335
````
34-
./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.0.1/elasticsearch-sql-2.0.1.zip
36+
./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.0.2/elasticsearch-sql-2.0.2.zip
37+
````
38+
### Elasticsearch 2.1.0
39+
````
40+
./bin/plugin install https://github.com/NLPchina/elasticsearch-sql/releases/download/2.1.0/elasticsearch-sql-2.1.0.zip
3541
````
3642
After doing this, you need to restart the Elasticsearch server. Otherwise you may get errors like `Invalid index name [sql], must not start with '']; ","status":400}`.
3743

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
@@ -46,6 +46,7 @@ public SqlElasticSearchRequestBuilder explain() throws SqlParseException {
4646
if(!usedScroll){
4747
request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
4848
}
49+
updateWithIndicesOptionsIfNeeded(select,request);
4950

5051
SqlElasticSearchRequestBuilder sqlElasticRequestBuilder = new SqlElasticSearchRequestBuilder(request);
5152
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: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package org.nlpcn.es4sql;
22

3+
import com.alibaba.druid.sql.ast.SQLExpr;
4+
import com.alibaba.druid.sql.ast.expr.SQLQueryExpr;
5+
import org.elasticsearch.action.search.SearchRequest;
6+
import org.elasticsearch.action.search.SearchRequestBuilder;
37
import org.elasticsearch.action.search.SearchResponse;
4-
8+
import org.elasticsearch.index.IndexNotFoundException;
59
import org.elasticsearch.search.SearchHit;
610
import org.elasticsearch.search.SearchHits;
711
import org.joda.time.DateTime;
812
import org.joda.time.format.DateTimeFormat;
913
import org.joda.time.format.DateTimeFormatter;
1014
import org.junit.Assert;
1115
import org.junit.Test;
16+
import org.nlpcn.es4sql.domain.Select;
1217
import org.nlpcn.es4sql.exception.SqlParseException;
1318
import org.nlpcn.es4sql.query.SqlElasticSearchRequestBuilder;
1419

20+
import javax.naming.directory.SearchControls;
1521
import java.io.IOException;
1622
import java.sql.SQLFeatureNotSupportedException;
1723
import java.text.ParseException;
@@ -809,6 +815,18 @@ public void nestedOnInTermsQuery() throws IOException, SqlParseException, SQLFea
809815
Assert.assertEquals(3, response.getTotalHits());
810816
}
811817

818+
@Test
819+
public void multipleIndicesOneNotExistWithHint() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
820+
SearchHits response = query(String.format("SELECT /*! IGNORE_UNAVAILABLE */ * FROM %s,%s ", TEST_INDEX,"badindex"));
821+
Assert.assertTrue(response.getTotalHits() > 0);
822+
}
823+
824+
@Test(expected=IndexNotFoundException.class)
825+
public void multipleIndicesOneNotExistWithoutHint() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
826+
SearchHits response = query(String.format("SELECT * FROM %s,%s ", TEST_INDEX,"badindex"));
827+
Assert.assertTrue(response.getTotalHits() > 0);
828+
}
829+
812830
private SearchHits query(String query) throws SqlParseException, SQLFeatureNotSupportedException, SQLFeatureNotSupportedException {
813831
SearchDao searchDao = MainTestSuite.getSearchDao();
814832
SqlElasticSearchRequestBuilder select = (SqlElasticSearchRequestBuilder) searchDao.explain(query);

0 commit comments

Comments
 (0)