Skip to content

Commit ab3db9c

Browse files
committed
refactoring missing filter, move from SqlParser to Maker to support more identifiers in the future.
1 parent ee5753b commit ab3db9c

File tree

4 files changed

+56
-37
lines changed

4 files changed

+56
-37
lines changed

src/main/java/org/nlpcn/es4sql/parse/SqlParser.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,12 @@ private Object parseValue(SQLExpr expr) throws SqlParseException {
144144
return expr;
145145
} else if (expr instanceof SQLNullExpr) {
146146
return null;
147-
} else if (expr instanceof SQLIdentifierExpr && "miss".equalsIgnoreCase(expr.toString())) {
147+
} else if (expr instanceof SQLIdentifierExpr) {
148148
return expr;
149149
} else {
150-
throw new SqlParseException("i can not know value type " + expr.getClass() + " , value is : " + expr);
150+
throw new SqlParseException(
151+
String.format("Failed to parse SqlExpression of type %s. expression value: %s", expr.getClass(), expr)
152+
);
151153
}
152154
}
153155

src/main/java/org/nlpcn/es4sql/query/maker/Maker.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,15 @@ private ToXContent make(Condition cond, String name, Object value) throws SqlPar
118118
case N:
119119
case EQ:
120120
if (value instanceof SQLIdentifierExpr) {
121-
x = FilterBuilders.missingFilter(name);
122-
if (isQuery) {
123-
x = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.missingFilter(name));
121+
SQLIdentifierExpr identifier = (SQLIdentifierExpr) value;
122+
if(identifier.getName().equalsIgnoreCase("missing")) {
123+
x = FilterBuilders.missingFilter(name);
124+
if (isQuery) {
125+
x = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.missingFilter(name));
126+
}
127+
}
128+
else {
129+
throw new SqlParseException(String.format("Cannot recoginze Sql identifer %s", identifier.getName()));
124130
}
125131
break;
126132
} else {

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

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.elasticsearch.search.SearchHit;
88
import org.elasticsearch.search.SearchHitField;
99
import org.elasticsearch.search.SearchHits;
10+
import org.hamcrest.collection.IsMapContaining;
1011
import org.junit.Assert;
1112
import org.junit.Test;
1213
import org.nlpcn.es4sql.exception.SqlParseException;
@@ -39,6 +40,14 @@ public void selectSpecificFields() throws IOException, SqlParseException {
3940
}
4041
}
4142

43+
44+
@Test
45+
public void searchTypeTest() throws IOException, SqlParseException{
46+
SearchHits response = query(String.format("SELECT * FROM %s/phrase LIMIT 1000", TEST_INDEX));
47+
Assert.assertEquals(4, response.getTotalHits());
48+
}
49+
50+
4251
@Test
4352
public void equallityTest() throws SqlParseException {
4453
SearchHits response = query(String.format("select * from %s where city = 'Nogal' LIMIT 1000", TEST_INDEX));
@@ -73,6 +82,7 @@ public void greaterThanTest() throws IOException, SqlParseException {
7382
}
7483
}
7584

85+
7686
@Test
7787
public void greaterThanOrEqualTest() throws IOException, SqlParseException {
7888
int someAge = 25;
@@ -103,6 +113,7 @@ public void lessThanTest() throws IOException, SqlParseException {
103113
}
104114
}
105115

116+
106117
@Test
107118
public void lessThanOrEqualTest() throws IOException, SqlParseException {
108119
int someAge = 25;
@@ -122,7 +133,6 @@ public void lessThanOrEqualTest() throws IOException, SqlParseException {
122133
}
123134

124135

125-
126136
@Test
127137
public void orTest() throws IOException, SqlParseException {
128138
SearchHits response = query(String.format("SELECT * FROM %s WHERE gender='F' OR gender='M' LIMIT 1000", TEST_INDEX));
@@ -142,7 +152,6 @@ public void andTest() throws IOException, SqlParseException {
142152
}
143153

144154

145-
146155
@Test
147156
public void likeTest() throws IOException, SqlParseException {
148157
SearchHits response = query(String.format("SELECT * FROM %s WHERE firstname LIKE 'amb%%' LIMIT 1000", TEST_INDEX));
@@ -162,7 +171,8 @@ public void limitTest() throws IOException, SqlParseException {
162171
// assert the results is correct according to accounts.json data.
163172
Assert.assertEquals(30, hits.length);
164173
}
165-
174+
175+
166176
@Test
167177
public void betweenTest() throws IOException, SqlParseException {
168178
int min = 27;
@@ -196,7 +206,8 @@ public void notBetweenTest() throws IOException, SqlParseException {
196206
}
197207
}
198208
}
199-
209+
210+
200211
@Test
201212
public void inTest() throws IOException, SqlParseException{
202213
SearchHits response = query(String.format("SELECT age FROM %s WHERE age IN (20, 22) LIMIT 1000", TEST_INDEX));
@@ -207,6 +218,7 @@ public void inTest() throws IOException, SqlParseException{
207218
}
208219
}
209220

221+
210222
@Test
211223
public void inTestWithStrings() throws IOException, SqlParseException{
212224
SearchHits response = query(String.format("SELECT phrase FROM %s WHERE phrase IN ('quick fox here', 'fox brown') LIMIT 1000", TEST_INDEX));
@@ -253,7 +265,8 @@ public void dateSearch() throws IOException, SqlParseException, ParseException {
253265
Assert.assertTrue(errorMessage, insertTime.isBefore(dateToCompare));
254266
}
255267
}
256-
268+
269+
257270
@Test
258271
public void dateBetweenSearch() throws IOException, SqlParseException {
259272
DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_FORMAT);
@@ -274,50 +287,48 @@ public void dateBetweenSearch() throws IOException, SqlParseException {
274287
Assert.assertTrue("insert_time must be between 2014-08-18 and 2014-08-21", isBetween);
275288
}
276289
}
277-
278-
/**
279-
* 是否存在查询
280-
* @throws IOException
281-
* @throws SqlParseException
282-
*/
290+
291+
283292
@Test
284293
public void missFilterSearch() throws IOException, SqlParseException{
285-
SearchRequestBuilder select = searchDao.explan("select insert_time from online where insert_time is not miss order by _score desc limit 10");
286-
System.out.println(select);
294+
SearchHits response = query(String.format("SELECT * FROM %s/phrase WHERE insert_time IS missing", TEST_INDEX));
295+
SearchHit[] hits = response.getHits();
296+
297+
// should be 2 according to the data.
298+
Assert.assertEquals(response.getTotalHits(), 2);
299+
for(SearchHit hit : hits) {
300+
assertThat(hit.getSource(), not(hasKey("insert_time")));
301+
}
287302
}
288-
303+
289304
@Test
290-
public void missQuerySearch() throws IOException, SqlParseException{
291-
SearchRequestBuilder select = searchDao.explan("select insert_time from online where insert_time is not miss limit 10");
292-
System.out.println(select);
305+
public void notMissFilterSearch() throws IOException, SqlParseException{
306+
SearchHits response = query(String.format("SELECT * FROM %s/phrase WHERE insert_time IS NOT missing", TEST_INDEX));
307+
SearchHit[] hits = response.getHits();
308+
309+
// should be 2 according to the data.
310+
Assert.assertEquals(response.getTotalHits(), 2);
311+
for(SearchHit hit : hits) {
312+
assertThat(hit.getSource(), hasKey("insert_time"));
313+
}
293314
}
294315

295316

317+
296318
@Test
297319
public void boolQuerySearch() throws IOException, SqlParseException{
298-
SearchRequestBuilder select = searchDao.explan("select * from bank where (gender='m' and (age> 25 or account_number>5)) or (gender='w' and (age>30 or account_number < 8)) and email is not miss order by age,_score desc limit 10 ");
320+
SearchRequestBuilder select = searchDao.explan("select * from bank where (gender='m' and (age> 25 or account_number>5)) or (gender='w' and (age>30 or account_number < 8)) and email is not missing order by age,_score desc limit 10 ");
299321
System.out.println(select);
300322
}
301323

302324

303325

304326
@Test
305327
public void countSearch() throws IOException, SqlParseException{
306-
SearchRequestBuilder select = searchDao.explan("select count(*) from bank where (gender='m' and (age> 25 or account_number>5)) or (gender='w' and (age>30 or account_number < 8)) and email is not miss");
328+
SearchRequestBuilder select = searchDao.explan("select count(*) from bank where (gender='m' and (age> 25 or account_number>5)) or (gender='w' and (age>30 or account_number < 8)) and email is not missing");
307329
System.out.println(select);
308330
}
309331

310-
/**
311-
* table have '.' and type test
312-
* @throws IOException
313-
* @throws SqlParseException
314-
*/
315-
@Test
316-
public void searchTableTest() throws IOException, SqlParseException{
317-
SearchRequestBuilder select = searchDao.explan("select count(*) from doc/accounts,bank/doc limit 10");
318-
System.out.println(select);
319-
}
320-
321332

322333
private SearchHits query(String query) throws SqlParseException {
323334
SearchDao searchDao = MainTestSuite.getSearchDao();

src/test/resources/phrases.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{"index":{"_type": "phrase"}}
22
{"phrase": "quick fox"}
33
{"index":{"_type": "phrase"}}
4-
{"phrase": "quick fox here"}
4+
{"phrase": "quick fox here", "insert_time":"2014-08-19T07:09:13.434Z" }
55
{"index":{"_type": "phrase"}}
66
{"phrase": "brown fox"}
77
{"index":{"_type": "phrase"}}
8-
{"phrase": "fox brown"}
8+
{"phrase": "fox brown", "insert_time":"2014-08-19T07:09:13.434Z"}

0 commit comments

Comments
 (0)