Search in sources :

Example 1 with DynamicOperandFieldComparatorSource

use of org.apache.jackrabbit.core.query.lucene.sort.DynamicOperandFieldComparatorSource in project jackrabbit by apache.

the class QueryEngine method createSortFields.

public SortField[] createSortFields(Ordering[] orderings, Session session) throws RepositoryException {
    if (orderings == null || orderings.length == 0) {
        return new SortField[] { SortField.FIELD_SCORE };
    }
    // orderings[] -> (property, ordering)
    Map<String, Ordering> orderByProperties = new HashMap<String, Ordering>();
    for (Ordering o : orderings) {
        final String p = o.toString();
        if (!orderByProperties.containsKey(p)) {
            orderByProperties.put(p, o);
        }
    }
    final DynamicOperandFieldComparatorSource dofcs = new DynamicOperandFieldComparatorSource(session, evaluator, orderByProperties);
    List<SortField> sortFields = new ArrayList<SortField>();
    // insertion order
    for (Ordering o : orderings) {
        final String p = o.toString();
        // order on jcr:score does not use the natural order as
        // implemented in lucene. score ascending in lucene means that
        // higher scores are first. JCR specs that lower score values
        // are first.
        boolean isAsc = QueryObjectModelConstants.JCR_ORDER_ASCENDING.equals(o.getOrder());
        if (JcrConstants.JCR_SCORE.equals(p)) {
            sortFields.add(new SortField(null, SortField.SCORE, !isAsc));
        } else {
            // TODO use native sort if available
            sortFields.add(new SortField(p, dofcs, !isAsc));
        }
    }
    return sortFields.toArray(new SortField[sortFields.size()]);
}
Also used : DynamicOperandFieldComparatorSource(org.apache.jackrabbit.core.query.lucene.sort.DynamicOperandFieldComparatorSource) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Ordering(javax.jcr.query.qom.Ordering) ArrayList(java.util.ArrayList) SortField(org.apache.lucene.search.SortField)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Ordering (javax.jcr.query.qom.Ordering)1 DynamicOperandFieldComparatorSource (org.apache.jackrabbit.core.query.lucene.sort.DynamicOperandFieldComparatorSource)1 SortField (org.apache.lucene.search.SortField)1