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