7
7
import org .elasticsearch .search .SearchHit ;
8
8
import org .elasticsearch .search .SearchHitField ;
9
9
import org .elasticsearch .search .SearchHits ;
10
+ import org .hamcrest .collection .IsMapContaining ;
10
11
import org .junit .Assert ;
11
12
import org .junit .Test ;
12
13
import org .nlpcn .es4sql .exception .SqlParseException ;
@@ -39,6 +40,14 @@ public void selectSpecificFields() throws IOException, SqlParseException {
39
40
}
40
41
}
41
42
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
+
42
51
@ Test
43
52
public void equallityTest () throws SqlParseException {
44
53
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 {
73
82
}
74
83
}
75
84
85
+
76
86
@ Test
77
87
public void greaterThanOrEqualTest () throws IOException , SqlParseException {
78
88
int someAge = 25 ;
@@ -103,6 +113,7 @@ public void lessThanTest() throws IOException, SqlParseException {
103
113
}
104
114
}
105
115
116
+
106
117
@ Test
107
118
public void lessThanOrEqualTest () throws IOException , SqlParseException {
108
119
int someAge = 25 ;
@@ -122,7 +133,6 @@ public void lessThanOrEqualTest() throws IOException, SqlParseException {
122
133
}
123
134
124
135
125
-
126
136
@ Test
127
137
public void orTest () throws IOException , SqlParseException {
128
138
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 {
142
152
}
143
153
144
154
145
-
146
155
@ Test
147
156
public void likeTest () throws IOException , SqlParseException {
148
157
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 {
162
171
// assert the results is correct according to accounts.json data.
163
172
Assert .assertEquals (30 , hits .length );
164
173
}
165
-
174
+
175
+
166
176
@ Test
167
177
public void betweenTest () throws IOException , SqlParseException {
168
178
int min = 27 ;
@@ -196,7 +206,8 @@ public void notBetweenTest() throws IOException, SqlParseException {
196
206
}
197
207
}
198
208
}
199
-
209
+
210
+
200
211
@ Test
201
212
public void inTest () throws IOException , SqlParseException {
202
213
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{
207
218
}
208
219
}
209
220
221
+
210
222
@ Test
211
223
public void inTestWithStrings () throws IOException , SqlParseException {
212
224
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 {
253
265
Assert .assertTrue (errorMessage , insertTime .isBefore (dateToCompare ));
254
266
}
255
267
}
256
-
268
+
269
+
257
270
@ Test
258
271
public void dateBetweenSearch () throws IOException , SqlParseException {
259
272
DateTimeFormatter formatter = DateTimeFormat .forPattern (DATE_FORMAT );
@@ -274,50 +287,48 @@ public void dateBetweenSearch() throws IOException, SqlParseException {
274
287
Assert .assertTrue ("insert_time must be between 2014-08-18 and 2014-08-21" , isBetween );
275
288
}
276
289
}
277
-
278
- /**
279
- * 是否存在查询
280
- * @throws IOException
281
- * @throws SqlParseException
282
- */
290
+
291
+
283
292
@ Test
284
293
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
+ }
287
302
}
288
-
303
+
289
304
@ 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
+ }
293
314
}
294
315
295
316
317
+
296
318
@ Test
297
319
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 " );
299
321
System .out .println (select );
300
322
}
301
323
302
324
303
325
304
326
@ Test
305
327
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 " );
307
329
System .out .println (select );
308
330
}
309
331
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
-
321
332
322
333
private SearchHits query (String query ) throws SqlParseException {
323
334
SearchDao searchDao = MainTestSuite .getSearchDao ();
0 commit comments