15
15
*/
16
16
package org .springframework .data .elasticsearch .core ;
17
17
18
- import static org .assertj .core .api .Assertions .*;
19
18
import static org .skyscreamer .jsonassert .JSONAssert .*;
20
19
21
20
import java .time .LocalDate ;
25
24
import java .util .List ;
26
25
import java .util .Objects ;
27
26
27
+ import org .assertj .core .api .SoftAssertions ;
28
28
import org .json .JSONException ;
29
29
import org .junit .jupiter .api .BeforeEach ;
30
30
import org .junit .jupiter .api .DisplayName ;
44
44
import org .springframework .data .elasticsearch .core .mapping .SimpleElasticsearchMappingContext ;
45
45
import org .springframework .data .elasticsearch .core .query .Criteria ;
46
46
import org .springframework .data .elasticsearch .core .query .CriteriaQuery ;
47
+ import org .springframework .data .elasticsearch .core .query .FetchSourceFilter ;
48
+ import org .springframework .data .elasticsearch .core .query .Query ;
49
+ import org .springframework .data .elasticsearch .core .query .SourceFilter ;
47
50
import org .springframework .lang .Nullable ;
48
51
49
52
/**
@@ -356,9 +359,7 @@ void shouldMapNamesAndValueInNestedEntities() throws JSONException {
356
359
" }\n " + //
357
360
"}\n " ; //
358
361
359
- CriteriaQuery criteriaQuery = new CriteriaQuery (
360
- new Criteria ("persons.birthDate" ).is (LocalDate .of (1999 , 10 , 3 ))
361
- );
362
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ("persons.birthDate" ).is (LocalDate .of (1999 , 10 , 3 )));
362
363
mappingElasticsearchConverter .updateQuery (criteriaQuery , House .class );
363
364
String queryString = new CriteriaQueryProcessor ().createQuery (criteriaQuery .getCriteria ()).toString ();
364
365
@@ -389,9 +390,7 @@ void shouldMapNamesAndValueInNestedEntitiesWithSubfields() throws JSONException
389
390
" }\n " + //
390
391
"}\n " ; //
391
392
392
- CriteriaQuery criteriaQuery = new CriteriaQuery (
393
- new Criteria ("persons.nickName.keyword" ).is ("Foobar" )
394
- );
393
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ("persons.nickName.keyword" ).is ("Foobar" ));
395
394
mappingElasticsearchConverter .updateQuery (criteriaQuery , House .class );
396
395
String queryString = new CriteriaQueryProcessor ().createQuery (criteriaQuery .getCriteria ()).toString ();
397
396
@@ -417,14 +416,33 @@ void shouldMapNamesAndValueInObjectEntities() throws JSONException {
417
416
" }\n " + //
418
417
"}\n " ; //
419
418
420
- CriteriaQuery criteriaQuery = new CriteriaQuery (
421
- new Criteria ("persons.birthDate" ).is (LocalDate .of (1999 , 10 , 3 ))
422
- );
419
+ CriteriaQuery criteriaQuery = new CriteriaQuery (new Criteria ("persons.birthDate" ).is (LocalDate .of (1999 , 10 , 3 )));
423
420
mappingElasticsearchConverter .updateQuery (criteriaQuery , ObjectWithPerson .class );
424
421
String queryString = new CriteriaQueryProcessor ().createQuery (criteriaQuery .getCriteria ()).toString ();
425
422
426
423
assertEquals (expected , queryString , false );
427
424
}
425
+
426
+ @ Test // #1778
427
+ @ DisplayName ("should map names in source fields and SourceFilters" )
428
+ void shouldMapNamesInSourceFieldsAndSourceFilters () {
429
+
430
+ Query query = Query .findAll ();
431
+ // Note: we don't care if these filters make sense here, this test is only about name mapping
432
+ query .addFields ("firstName" , "lastName" );
433
+ query .addSourceFilter (new FetchSourceFilter (new String [] { "firstName" }, new String [] { "lastName" }));
434
+
435
+ mappingElasticsearchConverter .updateQuery (query , Person .class );
436
+
437
+ SoftAssertions softly = new SoftAssertions ();
438
+ softly .assertThat (query .getFields ()).containsExactly ("first-name" , "last-name" );
439
+ SourceFilter sourceFilter = query .getSourceFilter ();
440
+ softly .assertThat (sourceFilter ).isNotNull ();
441
+ softly .assertThat (sourceFilter .getIncludes ()).containsExactly ("first-name" );
442
+ softly .assertThat (sourceFilter .getExcludes ()).containsExactly ("last-name" );
443
+ softly .assertAll ();
444
+ }
445
+
428
446
// endregion
429
447
430
448
// region helper functions
@@ -442,24 +460,21 @@ static class Person {
442
460
@ Nullable @ Id String id ;
443
461
@ Nullable @ Field (name = "first-name" ) String firstName ;
444
462
@ Nullable @ Field (name = "last-name" ) String lastName ;
445
- @ Nullable @ MultiField (mainField = @ Field (name ="nick-name" ), otherFields = {@ InnerField (suffix = "keyword" , type = FieldType .Keyword )}) String nickName ;
463
+ @ Nullable @ MultiField (mainField = @ Field (name = "nick-name" ),
464
+ otherFields = { @ InnerField (suffix = "keyword" , type = FieldType .Keyword ) }) String nickName ;
446
465
@ Nullable @ Field (name = "created-date" , type = FieldType .Date , format = DateFormat .epoch_millis ) Date createdDate ;
447
466
@ Nullable @ Field (name = "birth-date" , type = FieldType .Date , format = {},
448
467
pattern = "dd.MM.uuuu" ) LocalDate birthDate ;
449
468
}
450
469
451
470
static class House {
452
471
@ Nullable @ Id String id ;
453
- @ Nullable
454
- @ Field (name = "per-sons" , type = FieldType .Nested )
455
- List <Person > persons ;
472
+ @ Nullable @ Field (name = "per-sons" , type = FieldType .Nested ) List <Person > persons ;
456
473
}
457
474
458
475
static class ObjectWithPerson {
459
476
@ Nullable @ Id String id ;
460
- @ Nullable
461
- @ Field (name = "per-sons" , type = FieldType .Object )
462
- List <Person > persons ;
477
+ @ Nullable @ Field (name = "per-sons" , type = FieldType .Object ) List <Person > persons ;
463
478
}
464
479
465
480
static class GeoShapeEntity {
0 commit comments