35
35
import org .springframework .data .elasticsearch .annotations .FieldType ;
36
36
import org .springframework .data .elasticsearch .core .ElasticsearchOperations ;
37
37
import org .springframework .data .elasticsearch .core .IndexOperations ;
38
+ import org .springframework .data .elasticsearch .core .SearchHits ;
38
39
import org .springframework .data .elasticsearch .junit .jupiter .ElasticsearchRestTemplateConfiguration ;
39
40
import org .springframework .data .elasticsearch .junit .jupiter .SpringIntegrationTest ;
40
41
import org .springframework .data .elasticsearch .repository .ElasticsearchRepository ;
@@ -75,9 +76,10 @@ public void before() {
75
76
Product product3 = new Product ("3" , "Sugar" , "Beet sugar" , 1.1f , true , "sort3" );
76
77
Product product4 = new Product ("4" , "Salt" , "Rock salt" , 1.9f , true , "sort2" );
77
78
Product product5 = new Product ("5" , "Salt" , "Sea salt" , 2.1f , false , "sort1" );
78
- Product product6 = new Product ("6" , null , "no name" , 3.4f , false , "sort0" );
79
+ Product product6 = new Product ("6" , null , "no name" , 3.4f , false , "sort6" );
80
+ Product product7 = new Product ("7" , "" , "empty name" , 3.4f , false , "sort7" );
79
81
80
- repository .saveAll (Arrays .asList (product1 , product2 , product3 , product4 , product5 , product6 ));
82
+ repository .saveAll (Arrays .asList (product1 , product2 , product3 , product4 , product5 , product6 , product7 ));
81
83
}
82
84
83
85
@ AfterEach
@@ -118,7 +120,7 @@ public void shouldSupportTrueAndFalse() {
118
120
119
121
// then
120
122
assertThat (repository .findByAvailableTrue ()).hasSize (3 );
121
- assertThat (repository .findByAvailableFalse ()).hasSize (3 );
123
+ assertThat (repository .findByAvailableFalse ()).hasSize (4 );
122
124
}
123
125
124
126
@ Test
@@ -130,8 +132,8 @@ public void shouldSupportInAndNotInAndNot() {
130
132
131
133
// then
132
134
assertThat (repository .findByPriceIn (Arrays .asList (1.2f , 1.1f ))).hasSize (2 );
133
- assertThat (repository .findByPriceNotIn (Arrays .asList (1.2f , 1.1f ))).hasSize (4 );
134
- assertThat (repository .findByPriceNot (1.2f )).hasSize (5 );
135
+ assertThat (repository .findByPriceNotIn (Arrays .asList (1.2f , 1.1f ))).hasSize (5 );
136
+ assertThat (repository .findByPriceNot (1.2f )).hasSize (6 );
135
137
}
136
138
137
139
@ Test // DATAES-171
@@ -142,7 +144,7 @@ public void shouldWorkWithNotIn() {
142
144
// when
143
145
144
146
// then
145
- assertThat (repository .findByIdNotIn (Arrays .asList ("2" , "3" ))).hasSize (4 );
147
+ assertThat (repository .findByIdNotIn (Arrays .asList ("2" , "3" ))).hasSize (5 );
146
148
}
147
149
148
150
@ Test
@@ -167,8 +169,8 @@ public void shouldSupportLessThanAndGreaterThan() {
167
169
assertThat (repository .findByPriceLessThan (1.1f )).hasSize (1 );
168
170
assertThat (repository .findByPriceLessThanEqual (1.1f )).hasSize (2 );
169
171
170
- assertThat (repository .findByPriceGreaterThan (1.9f )).hasSize (2 );
171
- assertThat (repository .findByPriceGreaterThanEqual (1.9f )).hasSize (3 );
172
+ assertThat (repository .findByPriceGreaterThan (1.9f )).hasSize (3 );
173
+ assertThat (repository .findByPriceGreaterThanEqual (1.9f )).hasSize (4 );
172
174
}
173
175
174
176
@ Test // DATAES-615
@@ -193,7 +195,8 @@ public void shouldSupportSortOnStandardFieldWithoutCriteria() {
193
195
List <String > sortedIds = repository .findAllByOrderByText ().stream () //
194
196
.map (it -> it .text ).collect (Collectors .toList ());
195
197
196
- assertThat (sortedIds ).containsExactly ("Beet sugar" , "Cane sugar" , "Cane sugar" , "Rock salt" , "Sea salt" , "no name" );
198
+ assertThat (sortedIds ).containsExactly ("Beet sugar" , "Cane sugar" , "Cane sugar" , "Rock salt" , "Sea salt" ,
199
+ "empty name" , "no name" );
197
200
}
198
201
199
202
@ Test // DATAES-615
@@ -202,7 +205,7 @@ public void shouldSupportSortOnFieldWithCustomFieldNameWithoutCriteria() {
202
205
List <String > sortedIds = repository .findAllByOrderBySortName ().stream () //
203
206
.map (it -> it .id ).collect (Collectors .toList ());
204
207
205
- assertThat (sortedIds ).containsExactly ("6" , " 5" , "4" , "3" , "2" , "1" );
208
+ assertThat (sortedIds ).containsExactly ("5" , "4" , "3" , "2" , "1" , "6" , "7 " );
206
209
}
207
210
208
211
@ Test // DATAES-178
@@ -252,7 +255,7 @@ void shouldDeleteWithNullValues() {
252
255
repository .deleteByName (null );
253
256
254
257
long count = repository .count ();
255
- assertThat (count ).isEqualTo (5 );
258
+ assertThat (count ).isEqualTo (6 );
256
259
}
257
260
258
261
@ Test // DATAES-937
@@ -273,6 +276,52 @@ void shouldReturnEmptyListOnDerivedMethodWithEmptyInputList() {
273
276
assertThat (products ).isEmpty ();
274
277
}
275
278
279
+ @ Test // #1909
280
+ @ DisplayName ("should find by property exists" )
281
+ void shouldFindByPropertyExists () {
282
+
283
+ SearchHits <Product > searchHits = repository .findByNameExists ();
284
+
285
+ assertThat (searchHits .getTotalHits ()).isEqualTo (6 );
286
+ }
287
+
288
+ @ Test // #1909
289
+ @ DisplayName ("should find by property is not null" )
290
+ void shouldFindByPropertyIsNotNull () {
291
+
292
+ SearchHits <Product > searchHits = repository .findByNameIsNotNull ();
293
+
294
+ assertThat (searchHits .getTotalHits ()).isEqualTo (6 );
295
+ }
296
+
297
+ @ Test // #1909
298
+ @ DisplayName ("should find by property is null" )
299
+ void shouldFindByPropertyIsNull () {
300
+
301
+ SearchHits <Product > searchHits = repository .findByNameIsNull ();
302
+
303
+ assertThat (searchHits .getTotalHits ()).isEqualTo (1 );
304
+ }
305
+
306
+ @ Test // #1909
307
+ @ DisplayName ("should find by empty property" )
308
+ void shouldFindByEmptyProperty () {
309
+
310
+ SearchHits <Product > searchHits = repository .findByNameEmpty ();
311
+
312
+ assertThat (searchHits .getTotalHits ()).isEqualTo (1 );
313
+ }
314
+
315
+ @ Test // #1909
316
+ @ DisplayName ("should find by non-empty property" )
317
+ void shouldFindByNonEmptyProperty () {
318
+
319
+ SearchHits <Product > searchHits = repository .findByNameNotEmpty ();
320
+
321
+ assertThat (searchHits .getTotalHits ()).isEqualTo (5 );
322
+ }
323
+
324
+ @ SuppressWarnings ("unused" )
276
325
@ Document (indexName = "test-index-product-query-keywords" )
277
326
static class Product {
278
327
@ Nullable @ Id private String id ;
@@ -346,6 +395,7 @@ public void setSortName(@Nullable String sortName) {
346
395
}
347
396
}
348
397
398
+ @ SuppressWarnings ({ "SpringDataRepositoryMethodParametersInspection" , "SpringDataMethodInconsistencyInspection" })
349
399
interface ProductRepository extends ElasticsearchRepository <Product , String > {
350
400
351
401
List <Product > findByName (@ Nullable String name );
@@ -399,6 +449,16 @@ interface ProductRepository extends ElasticsearchRepository<Product, String> {
399
449
void deleteByName (@ Nullable String name );
400
450
401
451
List <Product > findAllByNameIn (List <String > names );
452
+
453
+ SearchHits <Product > findByNameExists ();
454
+
455
+ SearchHits <Product > findByNameIsNull ();
456
+
457
+ SearchHits <Product > findByNameIsNotNull ();
458
+
459
+ SearchHits <Product > findByNameEmpty ();
460
+
461
+ SearchHits <Product > findByNameNotEmpty ();
402
462
}
403
463
404
464
}
0 commit comments