38
38
import org .springframework .core .env .EnvironmentCapable ;
39
39
import org .springframework .core .env .StandardEnvironment ;
40
40
import org .springframework .data .convert .CustomConversions ;
41
+ import org .springframework .data .domain .Sort ;
41
42
import org .springframework .data .elasticsearch .annotations .FieldType ;
42
43
import org .springframework .data .elasticsearch .annotations .ScriptedField ;
43
44
import org .springframework .data .elasticsearch .core .document .Document ;
50
51
import org .springframework .data .elasticsearch .core .query .CriteriaQuery ;
51
52
import org .springframework .data .elasticsearch .core .query .FetchSourceFilter ;
52
53
import org .springframework .data .elasticsearch .core .query .Field ;
54
+ import org .springframework .data .elasticsearch .core .query .Order ;
53
55
import org .springframework .data .elasticsearch .core .query .Query ;
54
56
import org .springframework .data .elasticsearch .core .query .SeqNoPrimaryTerm ;
55
57
import org .springframework .data .elasticsearch .core .query .SourceFilter ;
@@ -1231,7 +1233,7 @@ public void updateQuery(Query query, @Nullable Class<?> domainClass) {
1231
1233
return ;
1232
1234
}
1233
1235
1234
- updatePropertiesInFieldsAndSourceFilter (query , domainClass );
1236
+ updatePropertiesInFieldsSortAndSourceFilter (query , domainClass );
1235
1237
1236
1238
if (query instanceof CriteriaQuery criteriaQuery ) {
1237
1239
updatePropertiesInCriteriaQuery (criteriaQuery , domainClass );
@@ -1242,20 +1244,27 @@ public void updateQuery(Query query, @Nullable Class<?> domainClass) {
1242
1244
}
1243
1245
}
1244
1246
1245
- private void updatePropertiesInFieldsAndSourceFilter (Query query , Class <?> domainClass ) {
1247
+ /**
1248
+ * replaces the names of fields in the query, the sort or soucre filters with the field names used in Elasticsearch
1249
+ * when they are defined on the ElasticsearchProperties
1250
+ *
1251
+ * @param query the query to process
1252
+ * @param domainClass the domain class (persistent entity)
1253
+ */
1254
+ private void updatePropertiesInFieldsSortAndSourceFilter (Query query , Class <?> domainClass ) {
1246
1255
1247
1256
ElasticsearchPersistentEntity <?> persistentEntity = mappingContext .getPersistentEntity (domainClass );
1248
1257
1249
1258
if (persistentEntity != null ) {
1250
1259
List <String > fields = query .getFields ();
1251
1260
1252
1261
if (!fields .isEmpty ()) {
1253
- query .setFields (updateFieldNames (fields , persistentEntity ));
1262
+ query .setFields (propertyToFieldNames (fields , persistentEntity ));
1254
1263
}
1255
1264
1256
1265
List <String > storedFields = query .getStoredFields ();
1257
1266
if (!CollectionUtils .isEmpty (storedFields )) {
1258
- query .setStoredFields (updateFieldNames (storedFields , persistentEntity ));
1267
+ query .setStoredFields (propertyToFieldNames (storedFields , persistentEntity ));
1259
1268
}
1260
1269
1261
1270
SourceFilter sourceFilter = query .getSourceFilter ();
@@ -1266,37 +1275,60 @@ private void updatePropertiesInFieldsAndSourceFilter(Query query, Class<?> domai
1266
1275
String [] excludes = null ;
1267
1276
1268
1277
if (sourceFilter .getIncludes () != null ) {
1269
- includes = updateFieldNames (Arrays .asList (sourceFilter .getIncludes ()), persistentEntity )
1278
+ includes = propertyToFieldNames (Arrays .asList (sourceFilter .getIncludes ()), persistentEntity )
1270
1279
.toArray (new String [] {});
1271
1280
}
1272
1281
1273
1282
if (sourceFilter .getExcludes () != null ) {
1274
- excludes = updateFieldNames (Arrays .asList (sourceFilter .getExcludes ()), persistentEntity )
1283
+ excludes = propertyToFieldNames (Arrays .asList (sourceFilter .getExcludes ()), persistentEntity )
1275
1284
.toArray (new String [] {});
1276
1285
}
1277
1286
1278
1287
query .addSourceFilter (new FetchSourceFilter (includes , excludes ));
1279
1288
}
1289
+
1290
+ if (query .getSort () != null ) {
1291
+ var sort = query .getSort ();
1292
+ // stream the orders and map them to a new order with the changed names,
1293
+ // then replace the existing sort with a new sort containing the new orders.
1294
+ var newOrders = sort .stream ().map (order -> {
1295
+ var fieldNames = updateFieldNames (order .getProperty (), persistentEntity );
1296
+
1297
+ if (order instanceof Order springDataElasticsearchOrder ) {
1298
+ return springDataElasticsearchOrder .withProperty (fieldNames );
1299
+ } else {
1300
+ return new Sort .Order (order .getDirection (),
1301
+ fieldNames ,
1302
+ order .isIgnoreCase (),
1303
+ order .getNullHandling ());
1304
+ }
1305
+ }).toList ();
1306
+
1307
+ if (query instanceof BaseQuery baseQuery ) {
1308
+ baseQuery .setSort (Sort .by (newOrders ));
1309
+ }
1310
+ }
1280
1311
}
1281
1312
}
1282
1313
1283
1314
/**
1284
- * relaces the fieldName with the property name of a property of the persistentEntity with the corresponding
1285
- * fieldname. If no such property exists, the original fieldName is kept.
1315
+ * replaces property name of a property of the persistentEntity with the corresponding fieldname. If no such property
1316
+ * exists, the original fieldName is kept.
1286
1317
*
1287
- * @param fieldNames list of fieldnames
1318
+ * @param propertyNames list of fieldnames
1288
1319
* @param persistentEntity the persistent entity to check
1289
1320
* @return an updated list of field names
1290
1321
*/
1291
- private List <String > updateFieldNames (List <String > fieldNames , ElasticsearchPersistentEntity <?> persistentEntity ) {
1292
- return fieldNames .stream ().map (fieldName -> updateFieldName (persistentEntity , fieldName ))
1322
+ private List <String > propertyToFieldNames (List <String > propertyNames ,
1323
+ ElasticsearchPersistentEntity <?> persistentEntity ) {
1324
+ return propertyNames .stream ().map (propertyName -> propertyToFieldName (persistentEntity , propertyName ))
1293
1325
.collect (Collectors .toList ());
1294
1326
}
1295
1327
1296
1328
@ NotNull
1297
- private String updateFieldName (ElasticsearchPersistentEntity <?> persistentEntity , String fieldName ) {
1298
- ElasticsearchPersistentProperty persistentProperty = persistentEntity .getPersistentProperty (fieldName );
1299
- return persistentProperty != null ? persistentProperty .getFieldName () : fieldName ;
1329
+ private String propertyToFieldName (ElasticsearchPersistentEntity <?> persistentEntity , String propertyName ) {
1330
+ ElasticsearchPersistentProperty persistentProperty = persistentEntity .getPersistentProperty (propertyName );
1331
+ return persistentProperty != null ? persistentProperty .getFieldName () : propertyName ;
1300
1332
}
1301
1333
1302
1334
private void updatePropertiesInCriteriaQuery (CriteriaQuery criteriaQuery , Class <?> domainClass ) {
@@ -1410,7 +1442,7 @@ public String updateFieldNames(String propertyPath, ElasticsearchPersistentEntit
1410
1442
1411
1443
if (properties .length > 0 ) {
1412
1444
var propertyName = properties [0 ];
1413
- var fieldName = updateFieldName (persistentEntity , propertyName );
1445
+ var fieldName = propertyToFieldName (persistentEntity , propertyName );
1414
1446
1415
1447
if (properties .length > 1 ) {
1416
1448
var persistentProperty = persistentEntity .getPersistentProperty (propertyName );
@@ -1431,7 +1463,6 @@ public String updateFieldNames(String propertyPath, ElasticsearchPersistentEntit
1431
1463
}
1432
1464
1433
1465
}
1434
-
1435
1466
// endregion
1436
1467
1437
1468
@ SuppressWarnings ("ClassCanBeRecord" )
0 commit comments