@@ -1372,53 +1372,21 @@ private void updatePropertiesInCriteria(Criteria criteria, ElasticsearchPersiste
1372
1372
return ;
1373
1373
}
1374
1374
1375
- String [] fieldNames = field .getName ().split ("\\ ." );
1376
-
1377
- ElasticsearchPersistentEntity <?> currentEntity = persistentEntity ;
1378
- ElasticsearchPersistentProperty persistentProperty = null ;
1379
- int propertyCount = 0 ;
1380
- boolean isNested = false ;
1381
-
1382
- for (int i = 0 ; i < fieldNames .length ; i ++) {
1383
- persistentProperty = currentEntity .getPersistentProperty (fieldNames [i ]);
1384
-
1385
- if (persistentProperty != null ) {
1386
- propertyCount ++;
1387
- fieldNames [i ] = persistentProperty .getFieldName ();
1388
-
1389
- org .springframework .data .elasticsearch .annotations .Field fieldAnnotation = persistentProperty
1390
- .findAnnotation (org .springframework .data .elasticsearch .annotations .Field .class );
1391
-
1392
- if (fieldAnnotation != null && fieldAnnotation .type () == FieldType .Nested ) {
1393
- isNested = true ;
1394
- }
1395
-
1396
- try {
1397
- currentEntity = mappingContext .getPersistentEntity (persistentProperty .getActualType ());
1398
- } catch (Exception e ) {
1399
- // using system types like UUIDs will lead to java.lang.reflect.InaccessibleObjectException in JDK 16
1400
- // so if we cannot get an entity here, bail out.
1401
- currentEntity = null ;
1402
- }
1403
- }
1404
-
1405
- if (currentEntity == null ) {
1406
- break ;
1407
- }
1408
- }
1375
+ var propertyNamesUpdate = updatePropertyNames (persistentEntity , field .getName ());
1409
1376
1377
+ var fieldNames = propertyNamesUpdate .names ();
1410
1378
field .setName (String .join ("." , fieldNames ));
1411
1379
1412
- if (propertyCount > 1 && isNested ) {
1380
+ if (propertyNamesUpdate . propertyCount () > 1 && propertyNamesUpdate . nestedProperty () ) {
1413
1381
List <String > propertyNames = Arrays .asList (fieldNames );
1414
- field .setPath (String .join ("." , propertyNames .subList (0 , propertyCount - 1 )));
1382
+ field .setPath (String .join ("." , propertyNames .subList (0 , propertyNamesUpdate . propertyCount - 1 )));
1415
1383
}
1416
1384
1417
- if (persistentProperty != null ) {
1385
+ if (propertyNamesUpdate . persistentProperty != null ) {
1418
1386
1419
- if (persistentProperty .hasPropertyValueConverter ()) {
1387
+ if (propertyNamesUpdate . persistentProperty .hasPropertyValueConverter ()) {
1420
1388
PropertyValueConverter propertyValueConverter = Objects
1421
- .requireNonNull (persistentProperty .getPropertyValueConverter ());
1389
+ .requireNonNull (propertyNamesUpdate . persistentProperty .getPropertyValueConverter ());
1422
1390
criteria .getQueryCriteriaEntries ().forEach (criteriaEntry -> {
1423
1391
1424
1392
if (criteriaEntry .getKey ().hasValue ()) {
@@ -1437,7 +1405,7 @@ private void updatePropertiesInCriteria(Criteria criteria, ElasticsearchPersiste
1437
1405
});
1438
1406
}
1439
1407
1440
- org .springframework .data .elasticsearch .annotations .Field fieldAnnotation = persistentProperty
1408
+ org .springframework .data .elasticsearch .annotations .Field fieldAnnotation = propertyNamesUpdate . persistentProperty
1441
1409
.findAnnotation (org .springframework .data .elasticsearch .annotations .Field .class );
1442
1410
1443
1411
if (fieldAnnotation != null ) {
@@ -1446,36 +1414,70 @@ private void updatePropertiesInCriteria(Criteria criteria, ElasticsearchPersiste
1446
1414
}
1447
1415
}
1448
1416
1417
+ static record PropertyNamesUpdate (
1418
+ String [] names ,
1419
+ Boolean nestedProperty ,
1420
+ Integer propertyCount ,
1421
+ ElasticsearchPersistentProperty persistentProperty ) {
1422
+ }
1423
+
1449
1424
@ Override
1450
1425
public String updateFieldNames (String propertyPath , ElasticsearchPersistentEntity <?> persistentEntity ) {
1451
1426
1452
1427
Assert .notNull (propertyPath , "propertyPath must not be null" );
1453
1428
Assert .notNull (persistentEntity , "persistentEntity must not be null" );
1454
1429
1455
- var properties = propertyPath .split ("\\ ." , 2 );
1430
+ var propertyNamesUpdate = updatePropertyNames (persistentEntity , propertyPath );
1431
+ return String .join ("." , propertyNamesUpdate .names ());
1432
+ }
1456
1433
1457
- if (properties .length > 0 ) {
1458
- var propertyName = properties [0 ];
1459
- var fieldName = propertyToFieldName (persistentEntity , propertyName );
1434
+ /**
1435
+ * Parse a propertyPath and replace the path values with the field names from a persistentEntity. path entries not
1436
+ * found in the entity are kept as they are.
1437
+ *
1438
+ * @return the eventually modified names, a flag if a nested entity was encountered the number of processed
1439
+ * propertiesand the last processed PersistentProperty.
1440
+ */
1441
+ PropertyNamesUpdate updatePropertyNames (ElasticsearchPersistentEntity <?> persistentEntity , String propertyPath ) {
1460
1442
1461
- if ( properties . length > 1 ) {
1462
- var persistentProperty = persistentEntity . getPersistentProperty ( propertyName );
1443
+ String [] propertyNames = propertyPath . split ( " \\ ." );
1444
+ String [] fieldNames = Arrays . copyOf ( propertyNames , propertyNames . length );
1463
1445
1464
- if (persistentProperty != null ) {
1465
- ElasticsearchPersistentEntity <?> nestedPersistentEntity = mappingContext
1466
- .getPersistentEntity (persistentProperty );
1467
- if (nestedPersistentEntity != null ) {
1468
- return fieldName + '.' + updateFieldNames (properties [1 ], nestedPersistentEntity );
1469
- } else {
1470
- return fieldName ;
1471
- }
1446
+ ElasticsearchPersistentEntity <?> currentEntity = persistentEntity ;
1447
+ ElasticsearchPersistentProperty persistentProperty = null ;
1448
+
1449
+ int propertyCount = 0 ;
1450
+ boolean isNested = false ;
1451
+
1452
+ for (int i = 0 ; i < propertyNames .length ; i ++) {
1453
+ persistentProperty = currentEntity .getPersistentProperty (propertyNames [i ]);
1454
+
1455
+ if (persistentProperty != null ) {
1456
+ propertyCount ++;
1457
+ fieldNames [i ] = persistentProperty .getFieldName ();
1458
+
1459
+ org .springframework .data .elasticsearch .annotations .Field fieldAnnotation = persistentProperty
1460
+ .findAnnotation (org .springframework .data .elasticsearch .annotations .Field .class );
1461
+
1462
+ if (fieldAnnotation != null && fieldAnnotation .type () == FieldType .Nested ) {
1463
+ isNested = true ;
1464
+ }
1465
+
1466
+ try {
1467
+ currentEntity = mappingContext .getPersistentEntity (persistentProperty .getActualType ());
1468
+ } catch (Exception e ) {
1469
+ // using system types like UUIDs will lead to java.lang.reflect.InaccessibleObjectException in JDK 16
1470
+ // so if we cannot get an entity here, bail out.
1471
+ currentEntity = null ;
1472
1472
}
1473
1473
}
1474
- return fieldName ;
1475
- } else {
1476
- return propertyPath ;
1474
+
1475
+ if (currentEntity == null ) {
1476
+ break ;
1477
+ }
1477
1478
}
1478
1479
1480
+ return new PropertyNamesUpdate (fieldNames , isNested , propertyCount , persistentProperty );
1479
1481
}
1480
1482
// endregion
1481
1483
0 commit comments