|
18 | 18 | import static org.springframework.data.elasticsearch.client.elc.TypeUtils.*;
|
19 | 19 | import static org.springframework.util.CollectionUtils.*;
|
20 | 20 |
|
21 |
| -import co.elastic.clients.elasticsearch._types.Conflicts; |
22 |
| -import co.elastic.clients.elasticsearch._types.ExpandWildcard; |
23 |
| -import co.elastic.clients.elasticsearch._types.FieldValue; |
24 |
| -import co.elastic.clients.elasticsearch._types.InlineScript; |
25 |
| -import co.elastic.clients.elasticsearch._types.OpType; |
26 |
| -import co.elastic.clients.elasticsearch._types.SortOptions; |
27 |
| -import co.elastic.clients.elasticsearch._types.SortOrder; |
28 |
| -import co.elastic.clients.elasticsearch._types.VersionType; |
29 |
| -import co.elastic.clients.elasticsearch._types.WaitForActiveShardOptions; |
| 21 | +import co.elastic.clients.elasticsearch._types.*; |
30 | 22 | import co.elastic.clients.elasticsearch._types.mapping.FieldType;
|
31 | 23 | import co.elastic.clients.elasticsearch._types.mapping.RuntimeField;
|
32 | 24 | import co.elastic.clients.elasticsearch._types.mapping.RuntimeFieldType;
|
|
71 | 63 |
|
72 | 64 | import org.apache.commons.logging.Log;
|
73 | 65 | import org.apache.commons.logging.LogFactory;
|
| 66 | +import org.jetbrains.annotations.NotNull; |
74 | 67 | import org.springframework.dao.InvalidDataAccessApiUsageException;
|
75 | 68 | import org.springframework.data.domain.Sort;
|
76 | 69 | import org.springframework.data.elasticsearch.core.RefreshPolicy;
|
|
89 | 82 | import org.springframework.data.elasticsearch.core.mapping.ElasticsearchPersistentProperty;
|
90 | 83 | import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
|
91 | 84 | import org.springframework.data.elasticsearch.core.query.*;
|
| 85 | +import org.springframework.data.elasticsearch.core.query.IndicesOptions; |
92 | 86 | import org.springframework.data.elasticsearch.core.reindex.ReindexRequest;
|
93 | 87 | import org.springframework.data.elasticsearch.core.reindex.Remote;
|
94 | 88 | import org.springframework.data.elasticsearch.core.script.Script;
|
@@ -269,36 +263,7 @@ public UpdateAliasesRequest indicesUpdateAliasesRequest(AliasActions aliasAction
|
269 | 263 | List<Action> actions = new ArrayList<>();
|
270 | 264 | aliasActions.getActions().forEach(aliasAction -> {
|
271 | 265 |
|
272 |
| - Action.Builder actionBuilder = new Action.Builder(); |
273 |
| - |
274 |
| - if (aliasAction instanceof AliasAction.Add add) { |
275 |
| - AliasActionParameters parameters = add.getParameters(); |
276 |
| - actionBuilder.add(addActionBuilder -> { |
277 |
| - addActionBuilder // |
278 |
| - .indices(Arrays.asList(parameters.getIndices())) // |
279 |
| - .isHidden(parameters.getHidden()) // |
280 |
| - .isWriteIndex(parameters.getWriteIndex()) // |
281 |
| - .routing(parameters.getRouting()) // |
282 |
| - .indexRouting(parameters.getIndexRouting()) // |
283 |
| - .searchRouting(parameters.getSearchRouting()); // |
284 |
| - |
285 |
| - if (parameters.getAliases() != null) { |
286 |
| - addActionBuilder.aliases(Arrays.asList(parameters.getAliases())); |
287 |
| - } |
288 |
| - |
289 |
| - Query filterQuery = parameters.getFilterQuery(); |
290 |
| - |
291 |
| - if (filterQuery != null) { |
292 |
| - elasticsearchConverter.updateQuery(filterQuery, parameters.getFilterQueryClass()); |
293 |
| - co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = getQuery(filterQuery, null); |
294 |
| - if (esQuery != null) { |
295 |
| - addActionBuilder.filter(esQuery); |
296 |
| - |
297 |
| - } |
298 |
| - } |
299 |
| - return addActionBuilder; |
300 |
| - }); |
301 |
| - } |
| 266 | + var actionBuilder = getBuilder(aliasAction); |
302 | 267 |
|
303 | 268 | if (aliasAction instanceof AliasAction.Remove remove) {
|
304 | 269 | AliasActionParameters parameters = remove.getParameters();
|
@@ -327,6 +292,40 @@ public UpdateAliasesRequest indicesUpdateAliasesRequest(AliasActions aliasAction
|
327 | 292 | return updateAliasRequestBuilder.build();
|
328 | 293 | }
|
329 | 294 |
|
| 295 | + @NotNull |
| 296 | + private Action.Builder getBuilder(AliasAction aliasAction) { |
| 297 | + Action.Builder actionBuilder = new Action.Builder(); |
| 298 | + |
| 299 | + if (aliasAction instanceof AliasAction.Add add) { |
| 300 | + AliasActionParameters parameters = add.getParameters(); |
| 301 | + actionBuilder.add(addActionBuilder -> { |
| 302 | + addActionBuilder // |
| 303 | + .indices(Arrays.asList(parameters.getIndices())) // |
| 304 | + .isHidden(parameters.getHidden()) // |
| 305 | + .isWriteIndex(parameters.getWriteIndex()) // |
| 306 | + .routing(parameters.getRouting()) // |
| 307 | + .indexRouting(parameters.getIndexRouting()) // |
| 308 | + .searchRouting(parameters.getSearchRouting()); // |
| 309 | + |
| 310 | + if (parameters.getAliases() != null) { |
| 311 | + addActionBuilder.aliases(Arrays.asList(parameters.getAliases())); |
| 312 | + } |
| 313 | + |
| 314 | + Query filterQuery = parameters.getFilterQuery(); |
| 315 | + |
| 316 | + if (filterQuery != null) { |
| 317 | + elasticsearchConverter.updateQuery(filterQuery, parameters.getFilterQueryClass()); |
| 318 | + co.elastic.clients.elasticsearch._types.query_dsl.Query esQuery = getQuery(filterQuery, null); |
| 319 | + if (esQuery != null) { |
| 320 | + addActionBuilder.filter(esQuery); |
| 321 | + } |
| 322 | + } |
| 323 | + return addActionBuilder; |
| 324 | + }); |
| 325 | + } |
| 326 | + return actionBuilder; |
| 327 | + } |
| 328 | + |
330 | 329 | public PutMappingRequest indicesPutMappingRequest(IndexCoordinates indexCoordinates, Document mapping) {
|
331 | 330 |
|
332 | 331 | Assert.notNull(indexCoordinates, "indexCoordinates must not be null");
|
@@ -1502,59 +1501,88 @@ private List<SortOptions> getSortOptions(Sort sort, @Nullable ElasticsearchPersi
|
1502 | 1501 | private SortOptions getSortOptions(Sort.Order order, @Nullable ElasticsearchPersistentEntity<?> persistentEntity) {
|
1503 | 1502 | SortOrder sortOrder = order.getDirection().isDescending() ? SortOrder.Desc : SortOrder.Asc;
|
1504 | 1503 |
|
1505 |
| - Order.Mode mode = Order.DEFAULT_MODE; |
| 1504 | + Order.Mode mode = order.getDirection().isAscending() ? Order.Mode.min : Order.Mode.max; |
1506 | 1505 | String unmappedType = null;
|
| 1506 | + String missing = null; |
| 1507 | + NestedSortValue nestedSortValue = null; |
| 1508 | + |
| 1509 | + if (SortOptions.Kind.Score.jsonValue().equals(order.getProperty())) { |
| 1510 | + return SortOptions.of(so -> so.score(s -> s.order(sortOrder))); |
| 1511 | + } |
1507 | 1512 |
|
1508 | 1513 | if (order instanceof Order o) {
|
1509 |
| - mode = o.getMode(); |
| 1514 | + |
| 1515 | + if (o.getMode() != null) { |
| 1516 | + mode = o.getMode(); |
| 1517 | + } |
1510 | 1518 | unmappedType = o.getUnmappedType();
|
| 1519 | + missing = o.getMissing(); |
| 1520 | + nestedSortValue = getNestedSort(o.getNested(), persistentEntity); |
1511 | 1521 | }
|
| 1522 | + Order.Mode finalMode = mode; |
| 1523 | + String finalUnmappedType = unmappedType; |
| 1524 | + var finalNestedSortValue = nestedSortValue; |
1512 | 1525 |
|
1513 |
| - if (SortOptions.Kind.Score.jsonValue().equals(order.getProperty())) { |
1514 |
| - return SortOptions.of(so -> so.score(s -> s.order(sortOrder))); |
1515 |
| - } else { |
1516 |
| - ElasticsearchPersistentProperty property = (persistentEntity != null) // |
1517 |
| - ? persistentEntity.getPersistentProperty(order.getProperty()) // |
1518 |
| - : null; |
1519 |
| - String fieldName = property != null ? property.getFieldName() : order.getProperty(); |
1520 |
| - |
1521 |
| - Order.Mode finalMode = mode; |
1522 |
| - if (order instanceof GeoDistanceOrder geoDistanceOrder) { |
1523 |
| - |
1524 |
| - return SortOptions.of(so -> so // |
1525 |
| - .geoDistance(gd -> gd // |
1526 |
| - .field(fieldName) // |
1527 |
| - .location(loc -> loc.latlon(Queries.latLon(geoDistanceOrder.getGeoPoint()))) // |
1528 |
| - .distanceType(geoDistanceType(geoDistanceOrder.getDistanceType())).mode(sortMode(finalMode)) // |
1529 |
| - .order(sortOrder(geoDistanceOrder.getDirection())) // |
1530 |
| - .unit(distanceUnit(geoDistanceOrder.getUnit())) // |
1531 |
| - .ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped()))); |
1532 |
| - } else { |
1533 |
| - String missing = (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) ? "_first" |
1534 |
| - : ((order.getNullHandling() == Sort.NullHandling.NULLS_LAST) ? "_last" : null); |
1535 |
| - String finalUnmappedType = unmappedType; |
1536 |
| - return SortOptions.of(so -> so // |
1537 |
| - .field(f -> { |
1538 |
| - f.field(fieldName) // |
1539 |
| - .order(sortOrder) // |
1540 |
| - .mode(sortMode(finalMode)); |
1541 |
| - |
1542 |
| - if (finalUnmappedType != null) { |
1543 |
| - FieldType fieldType = fieldType(finalUnmappedType); |
1544 |
| - |
1545 |
| - if (fieldType != null) { |
1546 |
| - f.unmappedType(fieldType); |
1547 |
| - } |
1548 |
| - } |
| 1526 | + ElasticsearchPersistentProperty property = (persistentEntity != null) // |
| 1527 | + ? persistentEntity.getPersistentProperty(order.getProperty()) // |
| 1528 | + : null; |
| 1529 | + String fieldName = property != null ? property.getFieldName() : order.getProperty(); |
1549 | 1530 |
|
1550 |
| - if (missing != null) { |
1551 |
| - f.missing(fv -> fv // |
1552 |
| - .stringValue(missing)); |
1553 |
| - } |
1554 |
| - return f; |
1555 |
| - })); |
1556 |
| - } |
| 1531 | + if (order instanceof GeoDistanceOrder geoDistanceOrder) { |
| 1532 | + return getSortOptions(geoDistanceOrder, fieldName, finalMode); |
1557 | 1533 | }
|
| 1534 | + |
| 1535 | + var finalMissing = missing != null ? missing |
| 1536 | + : (order.getNullHandling() == Sort.NullHandling.NULLS_FIRST) ? "_first" |
| 1537 | + : ((order.getNullHandling() == Sort.NullHandling.NULLS_LAST) ? "_last" : null); |
| 1538 | + |
| 1539 | + return SortOptions.of(so -> so // |
| 1540 | + .field(f -> { |
| 1541 | + f.field(fieldName) // |
| 1542 | + .order(sortOrder) // |
| 1543 | + .mode(sortMode(finalMode)); |
| 1544 | + |
| 1545 | + if (finalUnmappedType != null) { |
| 1546 | + FieldType fieldType = fieldType(finalUnmappedType); |
| 1547 | + |
| 1548 | + if (fieldType != null) { |
| 1549 | + f.unmappedType(fieldType); |
| 1550 | + } |
| 1551 | + } |
| 1552 | + |
| 1553 | + if (finalMissing != null) { |
| 1554 | + f.missing(fv -> fv // |
| 1555 | + .stringValue(finalMissing)); |
| 1556 | + } |
| 1557 | + |
| 1558 | + if (finalNestedSortValue != null) { |
| 1559 | + f.nested(finalNestedSortValue); |
| 1560 | + } |
| 1561 | + |
| 1562 | + return f; |
| 1563 | + })); |
| 1564 | + } |
| 1565 | + |
| 1566 | + @Nullable |
| 1567 | + private NestedSortValue getNestedSort(@Nullable Order.Nested nested, |
| 1568 | + ElasticsearchPersistentEntity<?> persistentEntity) { |
| 1569 | + return (nested == null) ? null |
| 1570 | + : NestedSortValue.of(b -> b // |
| 1571 | + .path(elasticsearchConverter.updateFieldNames(nested.getPath(), persistentEntity)) // |
| 1572 | + .maxChildren(nested.getMaxChildren()) // |
| 1573 | + .nested(getNestedSort(nested.getNested(), persistentEntity)) // |
| 1574 | + .filter(getQuery(nested.getFilter(), persistentEntity.getType()))); |
| 1575 | + } |
| 1576 | + |
| 1577 | + private static SortOptions getSortOptions(GeoDistanceOrder geoDistanceOrder, String fieldName, Order.Mode finalMode) { |
| 1578 | + return SortOptions.of(so -> so // |
| 1579 | + .geoDistance(gd -> gd // |
| 1580 | + .field(fieldName) // |
| 1581 | + .location(loc -> loc.latlon(Queries.latLon(geoDistanceOrder.getGeoPoint()))) // |
| 1582 | + .distanceType(geoDistanceType(geoDistanceOrder.getDistanceType())).mode(sortMode(finalMode)) // |
| 1583 | + .order(sortOrder(geoDistanceOrder.getDirection())) // |
| 1584 | + .unit(distanceUnit(geoDistanceOrder.getUnit())) // |
| 1585 | + .ignoreUnmapped(geoDistanceOrder.getIgnoreUnmapped()))); |
1558 | 1586 | }
|
1559 | 1587 |
|
1560 | 1588 | @SuppressWarnings("DuplicatedCode")
|
|
0 commit comments