58
58
* @author Mohsin Husen
59
59
* @author Franck Marchand
60
60
* @author Abdul Mohammed
61
+ * @author Kevin Leturc
61
62
*/
62
63
@ RunWith (SpringJUnit4ClassRunner .class )
63
64
@ ContextConfiguration ("classpath:elasticsearch-template-test.xml" )
@@ -80,6 +81,26 @@ public void before() {
80
81
elasticsearchTemplate .refresh (SampleEntity .class , true );
81
82
}
82
83
84
+ /*
85
+ DATAES-106
86
+ */
87
+ @ Test
88
+ public void shouldReturnCountForGivenCriteriaQuery () {
89
+ // given
90
+ String documentId = randomNumeric (5 );
91
+ SampleEntity sampleEntity = new SampleEntityBuilder (documentId ).message ("some message" )
92
+ .version (System .currentTimeMillis ()).build ();
93
+
94
+ IndexQuery indexQuery = getIndexQuery (sampleEntity );
95
+ elasticsearchTemplate .index (indexQuery );
96
+ elasticsearchTemplate .refresh (SampleEntity .class , true );
97
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ());
98
+ // when
99
+ long count = elasticsearchTemplate .count (criteriaQuery , SampleEntity .class );
100
+ // then
101
+ assertThat (count , is (equalTo (1L )));
102
+ }
103
+
83
104
@ Test
84
105
public void shouldReturnCountForGivenSearchQuery () {
85
106
// given
@@ -1187,6 +1208,27 @@ public void shouldIndexSampleEntityWithIndexAndTypeAtRuntime() {
1187
1208
assertThat (sampleEntities .getTotalElements (), greaterThanOrEqualTo (1L ));
1188
1209
}
1189
1210
1211
+ /*
1212
+ DATAES-106
1213
+ */
1214
+ @ Test
1215
+ public void shouldReturnCountForGivenCriteriaQueryWithGivenIndexUsingCriteriaQuery () {
1216
+ // given
1217
+ String documentId = randomNumeric (5 );
1218
+ SampleEntity sampleEntity = new SampleEntityBuilder (documentId ).message ("some message" )
1219
+ .version (System .currentTimeMillis ()).build ();
1220
+
1221
+ IndexQuery indexQuery = getIndexQuery (sampleEntity );
1222
+ elasticsearchTemplate .index (indexQuery );
1223
+ elasticsearchTemplate .refresh (SampleEntity .class , true );
1224
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ());
1225
+ criteriaQuery .addIndices ("test-index" );
1226
+ // when
1227
+ long count = elasticsearchTemplate .count (criteriaQuery );
1228
+ // then
1229
+ assertThat (count , is (equalTo (1L )));
1230
+ }
1231
+
1190
1232
/*
1191
1233
DATAES-67
1192
1234
*/
@@ -1210,6 +1252,28 @@ public void shouldReturnCountForGivenSearchQueryWithGivenIndexUsingSearchQuery()
1210
1252
assertThat (count , is (equalTo (1L )));
1211
1253
}
1212
1254
1255
+ /*
1256
+ DATAES-106
1257
+ */
1258
+ @ Test
1259
+ public void shouldReturnCountForGivenCriteriaQueryWithGivenIndexAndTypeUsingCriteriaQuery () {
1260
+ // given
1261
+ String documentId = randomNumeric (5 );
1262
+ SampleEntity sampleEntity = new SampleEntityBuilder (documentId ).message ("some message" )
1263
+ .version (System .currentTimeMillis ()).build ();
1264
+
1265
+ IndexQuery indexQuery = getIndexQuery (sampleEntity );
1266
+ elasticsearchTemplate .index (indexQuery );
1267
+ elasticsearchTemplate .refresh (SampleEntity .class , true );
1268
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ());
1269
+ criteriaQuery .addIndices ("test-index" );
1270
+ criteriaQuery .addTypes ("test-type" );
1271
+ // when
1272
+ long count = elasticsearchTemplate .count (criteriaQuery );
1273
+ // then
1274
+ assertThat (count , is (equalTo (1L )));
1275
+ }
1276
+
1213
1277
/*
1214
1278
DATAES-67
1215
1279
*/
@@ -1234,6 +1298,43 @@ public void shouldReturnCountForGivenSearchQueryWithGivenIndexAndTypeUsingSearch
1234
1298
assertThat (count , is (equalTo (1L )));
1235
1299
}
1236
1300
1301
+ /*
1302
+ DATAES-106
1303
+ */
1304
+ @ Test
1305
+ public void shouldReturnCountForGivenCriteriaQueryWithGivenMultiIndices () {
1306
+ // given
1307
+ cleanUpIndices ();
1308
+ String documentId1 = randomNumeric (5 );
1309
+ SampleEntity sampleEntity1 = new SampleEntityBuilder (documentId1 ).message ("some message" )
1310
+ .version (System .currentTimeMillis ()).build ();
1311
+
1312
+ IndexQuery indexQuery1 = new IndexQueryBuilder ().withId (sampleEntity1 .getId ())
1313
+ .withIndexName ("test-index-1" )
1314
+ .withObject (sampleEntity1 )
1315
+ .build ();
1316
+
1317
+ String documentId2 = randomNumeric (5 );
1318
+ SampleEntity sampleEntity2 = new SampleEntityBuilder (documentId2 ).message ("some test message" )
1319
+ .version (System .currentTimeMillis ()).build ();
1320
+
1321
+ IndexQuery indexQuery2 = new IndexQueryBuilder ().withId (sampleEntity2 .getId ())
1322
+ .withIndexName ("test-index-2" )
1323
+ .withObject (sampleEntity2 )
1324
+ .build ();
1325
+
1326
+ elasticsearchTemplate .bulkIndex (Arrays .asList (indexQuery1 , indexQuery2 ));
1327
+ elasticsearchTemplate .refresh ("test-index-1" , true );
1328
+ elasticsearchTemplate .refresh ("test-index-2" , true );
1329
+
1330
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ());
1331
+ criteriaQuery .addIndices ("test-index-1" , "test-index-2" );
1332
+ // when
1333
+ long count = elasticsearchTemplate .count (criteriaQuery );
1334
+ // then
1335
+ assertThat (count , is (equalTo (2L )));
1336
+ }
1337
+
1237
1338
/*
1238
1339
DATAES-67
1239
1340
*/
@@ -1310,6 +1411,43 @@ public void shouldDeleteIndexForSpecifiedIndexName() {
1310
1411
assertThat (elasticsearchTemplate .indexExists ("test-index" ), is (false ));
1311
1412
}
1312
1413
1414
+ /*
1415
+ DATAES-106
1416
+ */
1417
+ @ Test
1418
+ public void shouldReturnCountForGivenCriteriaQueryWithGivenIndexNameForSpecificIndex () {
1419
+ // given
1420
+ cleanUpIndices ();
1421
+ String documentId1 = randomNumeric (5 );
1422
+ SampleEntity sampleEntity1 = new SampleEntityBuilder (documentId1 ).message ("some message" )
1423
+ .version (System .currentTimeMillis ()).build ();
1424
+
1425
+ IndexQuery indexQuery1 = new IndexQueryBuilder ().withId (sampleEntity1 .getId ())
1426
+ .withIndexName ("test-index-1" )
1427
+ .withObject (sampleEntity1 )
1428
+ .build ();
1429
+
1430
+ String documentId2 = randomNumeric (5 );
1431
+ SampleEntity sampleEntity2 = new SampleEntityBuilder (documentId2 ).message ("some test message" )
1432
+ .version (System .currentTimeMillis ()).build ();
1433
+
1434
+ IndexQuery indexQuery2 = new IndexQueryBuilder ().withId (sampleEntity2 .getId ())
1435
+ .withIndexName ("test-index-2" )
1436
+ .withObject (sampleEntity2 )
1437
+ .build ();
1438
+
1439
+ elasticsearchTemplate .bulkIndex (Arrays .asList (indexQuery1 , indexQuery2 ));
1440
+ elasticsearchTemplate .refresh ("test-index-1" , true );
1441
+ elasticsearchTemplate .refresh ("test-index-2" , true );
1442
+
1443
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ());
1444
+ criteriaQuery .addIndices ("test-index-1" );
1445
+ // when
1446
+ long count = elasticsearchTemplate .count (criteriaQuery );
1447
+ // then
1448
+ assertThat (count , is (equalTo (1L )));
1449
+ }
1450
+
1313
1451
/*
1314
1452
DATAES-67
1315
1453
*/
@@ -1349,11 +1487,28 @@ public void shouldReturnCountForGivenSearchQueryWithGivenIndexNameForSpecificInd
1349
1487
assertThat (count , is (equalTo (1L )));
1350
1488
}
1351
1489
1490
+ @ Test (expected = IllegalArgumentException .class )
1491
+ public void shouldThrowAnExceptionForGivenCriteriaQueryWhenNoIndexSpecifiedForCountQuery () {
1492
+ // given
1493
+ String documentId = randomNumeric (5 );
1494
+ SampleEntity sampleEntity = new SampleEntityBuilder (documentId ).message ("some message" )
1495
+ .version (System .currentTimeMillis ()).build ();
1496
+
1497
+ IndexQuery indexQuery = getIndexQuery (sampleEntity );
1498
+ elasticsearchTemplate .index (indexQuery );
1499
+ elasticsearchTemplate .refresh (SampleEntity .class , true );
1500
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ());
1501
+ // when
1502
+ long count = elasticsearchTemplate .count (criteriaQuery );
1503
+ // then
1504
+ assertThat (count , is (equalTo (1L )));
1505
+ }
1506
+
1352
1507
/*
1353
1508
DATAES-67
1354
1509
*/
1355
1510
@ Test (expected = IllegalArgumentException .class )
1356
- public void shouldThrowAnExceptionWhenNoIndexSpecifiedForCountQuery () {
1511
+ public void shouldThrowAnExceptionForGivenSearchQueryWhenNoIndexSpecifiedForCountQuery () {
1357
1512
// given
1358
1513
String documentId = randomNumeric (5 );
1359
1514
SampleEntity sampleEntity = new SampleEntityBuilder (documentId ).message ("some message" )
0 commit comments