|
| 1 | +package com.redis.vl.query; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +/** |
| 10 | + * Utility class for parsing flexible sort specifications into normalized format. |
| 11 | + * |
| 12 | + * <p>Python port: Corresponds to SortSpec type alias and _parse_sort_spec() static method in Python |
| 13 | + * redisvl (PR #393) |
| 14 | + * |
| 15 | + * <p>Python SortSpec accepts: |
| 16 | + * |
| 17 | + * <ul> |
| 18 | + * <li>str: field name (defaults to ASC) |
| 19 | + * <li>Tuple[str, str]: (field_name, direction) |
| 20 | + * <li>List[Union[str, Tuple[str, str]]]: multiple fields with optional directions |
| 21 | + * </ul> |
| 22 | + * |
| 23 | + * <p>Java SortSpec provides overloaded methods: |
| 24 | + * |
| 25 | + * <ul> |
| 26 | + * <li>{@code parseSortSpec(String field)} - single field, ASC |
| 27 | + * <li>{@code parseSortSpec(String field, String direction)} - single field with direction |
| 28 | + * <li>{@code parseSortSpec(SortField field)} - single SortField |
| 29 | + * <li>{@code parseSortSpec(List<SortField> fields)} - multiple fields |
| 30 | + * </ul> |
| 31 | + * |
| 32 | + * <p>Note: Redis Search only supports single-field sorting. When multiple fields are provided, only |
| 33 | + * the first field is used and a warning is logged. |
| 34 | + */ |
| 35 | +public final class SortSpec { |
| 36 | + |
| 37 | + private static final Logger logger = LoggerFactory.getLogger(SortSpec.class); |
| 38 | + |
| 39 | + // Private constructor - utility class |
| 40 | + private SortSpec() { |
| 41 | + throw new UnsupportedOperationException("Utility class"); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Parse a single field name (defaults to ascending order). |
| 46 | + * |
| 47 | + * <p>Python equivalent: sort_by="price" |
| 48 | + * |
| 49 | + * @param field Field name to sort by |
| 50 | + * @return List containing single SortField with ascending=true |
| 51 | + * @throws IllegalArgumentException if field is null or empty |
| 52 | + */ |
| 53 | + public static List<SortField> parseSortSpec(String field) { |
| 54 | + validateFieldName(field); |
| 55 | + return Collections.singletonList(SortField.asc(field.trim())); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Parse a single field name with direction. |
| 60 | + * |
| 61 | + * <p>Python equivalent: sort_by=("price", "DESC") |
| 62 | + * |
| 63 | + * @param field Field name to sort by |
| 64 | + * @param direction Sort direction ("ASC" or "DESC", case-insensitive) |
| 65 | + * @return List containing single SortField |
| 66 | + * @throws IllegalArgumentException if field is null/empty or direction is invalid |
| 67 | + */ |
| 68 | + public static List<SortField> parseSortSpec(String field, String direction) { |
| 69 | + validateFieldName(field); |
| 70 | + validateDirection(direction); |
| 71 | + |
| 72 | + String normalizedDirection = direction.trim().toUpperCase(); |
| 73 | + boolean ascending = "ASC".equals(normalizedDirection); |
| 74 | + |
| 75 | + return Collections.singletonList(new SortField(field.trim(), ascending)); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Parse a single SortField. |
| 80 | + * |
| 81 | + * @param field SortField to wrap in list |
| 82 | + * @return List containing the single SortField |
| 83 | + * @throws IllegalArgumentException if field is null |
| 84 | + */ |
| 85 | + public static List<SortField> parseSortSpec(SortField field) { |
| 86 | + if (field == null) { |
| 87 | + throw new IllegalArgumentException("SortField cannot be null"); |
| 88 | + } |
| 89 | + return Collections.singletonList(field); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Parse a list of SortFields (supports multiple fields). |
| 94 | + * |
| 95 | + * <p>Python equivalent: sort_by=[("price", "DESC"), ("rating", "ASC"), "stock"] |
| 96 | + * |
| 97 | + * <p>Note: Redis Search only supports single-field sorting. When multiple fields are provided, |
| 98 | + * only the first field is used and a warning is logged. |
| 99 | + * |
| 100 | + * @param fields List of SortFields |
| 101 | + * @return List of SortFields (may be empty, uses only first field for Redis) |
| 102 | + */ |
| 103 | + public static List<SortField> parseSortSpec(List<SortField> fields) { |
| 104 | + if (fields == null || fields.isEmpty()) { |
| 105 | + return Collections.emptyList(); |
| 106 | + } |
| 107 | + |
| 108 | + // Make defensive copy |
| 109 | + List<SortField> result = new ArrayList<>(fields); |
| 110 | + |
| 111 | + // Log warning if multiple fields specified (Redis limitation) |
| 112 | + if (result.size() > 1) { |
| 113 | + logger.warn( |
| 114 | + "Multiple sort fields specified ({}), but Redis Search only supports single-field" |
| 115 | + + " sorting. Using first field: '{}'", |
| 116 | + result.size(), |
| 117 | + result.get(0).getFieldName()); |
| 118 | + } |
| 119 | + |
| 120 | + return result; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Validate field name is not null or empty. |
| 125 | + * |
| 126 | + * @param field Field name to validate |
| 127 | + * @throws IllegalArgumentException if field is null or empty/blank |
| 128 | + */ |
| 129 | + private static void validateFieldName(String field) { |
| 130 | + if (field == null || field.trim().isEmpty()) { |
| 131 | + throw new IllegalArgumentException("Field name cannot be null or empty"); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Validate sort direction is "ASC" or "DESC" (case-insensitive). |
| 137 | + * |
| 138 | + * <p>Python raises: ValueError("Sort direction must be 'ASC' or 'DESC'") |
| 139 | + * |
| 140 | + * @param direction Direction string to validate |
| 141 | + * @throws IllegalArgumentException if direction is invalid |
| 142 | + */ |
| 143 | + private static void validateDirection(String direction) { |
| 144 | + if (direction == null || direction.trim().isEmpty()) { |
| 145 | + throw new IllegalArgumentException("Sort direction cannot be null or empty"); |
| 146 | + } |
| 147 | + |
| 148 | + String normalized = direction.trim().toUpperCase(); |
| 149 | + if (!normalized.equals("ASC") && !normalized.equals("DESC")) { |
| 150 | + throw new IllegalArgumentException( |
| 151 | + String.format("Sort direction must be 'ASC' or 'DESC', got: '%s'", direction)); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments