Sorting Search Results by Direct Document Properties
You can sort query results by specifying properties directly on the document node. The following example demonstrates how to order results using the HstQuery API:
final HstQuery hstQuery = HstQueryBuilder.create(scope) .ofTypes(BaseDocument.class) // order by property "myproject:date", descending .orderByDescending("myproject:date") // and if the dates are the same, order by property "myproject:title", ascending .orderByAscending("myproject:title") .build();
In this example, the query sorts results by the myproject:date property in descending order. If multiple documents share the same date, the query then sorts by the myproject:title property in ascending order.
Sorting is only supported on properties stored directly on the document node. You cannot sort on properties located in descendant JCR nodes, such as compound fields stored in child nodes. For example, if a document contains a compound field mynamespace:address as a child node, and this node has a property mynamespace:street, you cannot sort search results by mynamespace:street.
Sorting is limited to properties directly present on the HippoDocument node.
Reasons for the Limitation
-
Ambiguity with Same Name Siblings
If a document node contains multiple child nodes with the same name (same name siblings), the system cannot determine which child node's property to use for sorting. For example, if a document has two address nodes, it is unclear which address should determine the sort order. -
Indexing and Performance Constraints
Properties on descendant nodes cannot be efficiently indexed for sorting, especially if they are several levels deep. As a result, sorting on these properties is not supported due to performance limitations.