Class SortSpec

java.lang.Object
com.redis.vl.query.SortSpec

public final class SortSpec extends Object
Utility class for parsing flexible sort specifications into normalized format.

Python port: Corresponds to SortSpec type alias and _parse_sort_spec() static method in Python redisvl (PR #393)

Python SortSpec accepts:

  • str: field name (defaults to ASC)
  • Tuple[str, str]: (field_name, direction)
  • List[Union[str, Tuple[str, str]]]: multiple fields with optional directions

Java SortSpec provides overloaded methods:

  • parseSortSpec(String field) - single field, ASC
  • parseSortSpec(String field, String direction) - single field with direction
  • parseSortSpec(SortField field) - single SortField
  • parseSortSpec(List<SortField> fields) - multiple fields

Note: Redis Search only supports single-field sorting. When multiple fields are provided, only the first field is used and a warning is logged.

  • Method Details

    • parseSortSpec

      public static List<SortField> parseSortSpec(String field)
      Parse a single field name (defaults to ascending order).

      Python equivalent: sort_by="price"

      Parameters:
      field - Field name to sort by
      Returns:
      List containing single SortField with ascending=true
      Throws:
      IllegalArgumentException - if field is null or empty
    • parseSortSpec

      public static List<SortField> parseSortSpec(String field, String direction)
      Parse a single field name with direction.

      Python equivalent: sort_by=("price", "DESC")

      Parameters:
      field - Field name to sort by
      direction - Sort direction ("ASC" or "DESC", case-insensitive)
      Returns:
      List containing single SortField
      Throws:
      IllegalArgumentException - if field is null/empty or direction is invalid
    • parseSortSpec

      public static List<SortField> parseSortSpec(SortField field)
      Parse a single SortField.
      Parameters:
      field - SortField to wrap in list
      Returns:
      List containing the single SortField
      Throws:
      IllegalArgumentException - if field is null
    • parseSortSpec

      public static List<SortField> parseSortSpec(List<SortField> fields)
      Parse a list of SortFields (supports multiple fields).

      Python equivalent: sort_by=[("price", "DESC"), ("rating", "ASC"), "stock"]

      Note: Redis Search only supports single-field sorting. When multiple fields are provided, only the first field is used and a warning is logged.

      Parameters:
      fields - List of SortFields
      Returns:
      List of SortFields (may be empty, uses only first field for Redis)