Search in sources :

Example 1 with MCRSortBy

use of org.mycore.services.fieldquery.MCRSortBy in project mycore by MyCoRe-Org.

the class MCRConditionTransformer method applySortOptions.

public static SolrQuery applySortOptions(SolrQuery q, List<MCRSortBy> sortBy) {
    for (MCRSortBy option : sortBy) {
        SortClause sortClause = new SortClause(option.getFieldName(), option.getSortOrder() ? ORDER.asc : ORDER.desc);
        q.addSort(sortClause);
    }
    return q;
}
Also used : SortClause(org.apache.solr.client.solrj.SolrQuery.SortClause) MCRSortBy(org.mycore.services.fieldquery.MCRSortBy)

Example 2 with MCRSortBy

use of org.mycore.services.fieldquery.MCRSortBy in project mycore by MyCoRe-Org.

the class MCRQLSearchUtils method setQueryOptions.

protected static Document setQueryOptions(MCRQuery query, HttpServletRequest req) {
    String maxResults = getReqParameter(req, "maxResults", "0");
    query.setMaxResults(Integer.parseInt(maxResults));
    List<String> sortFields = new ArrayList<>();
    for (Enumeration<String> names = req.getParameterNames(); names.hasMoreElements(); ) {
        String name = names.nextElement();
        if (name.contains(".sortField")) {
            sortFields.add(name);
        }
    }
    if (sortFields.size() > 0) {
        sortFields.sort((arg0, arg1) -> {
            String s0 = arg0.substring(arg0.indexOf(".sortField"));
            String s1 = arg1.substring(arg1.indexOf(".sortField"));
            return s0.compareTo(s1);
        });
        List<MCRSortBy> sortBy = new ArrayList<>();
        for (String name : sortFields) {
            String sOrder = getReqParameter(req, name, "ascending");
            boolean order = "ascending".equals(sOrder) ? MCRSortBy.ASCENDING : MCRSortBy.DESCENDING;
            name = name.substring(0, name.indexOf(".sortField"));
            sortBy.add(new MCRSortBy(name, order));
        }
        query.setSortBy(sortBy);
    }
    Document xml = query.buildXML();
    xml.getRootElement().setAttribute("numPerPage", getReqParameter(req, "numPerPage", "0"));
    xml.getRootElement().setAttribute("mask", getReqParameter(req, "mask", "-"));
    return xml;
}
Also used : MCRSortBy(org.mycore.services.fieldquery.MCRSortBy) ArrayList(java.util.ArrayList) Document(org.jdom2.Document)

Aggregations

MCRSortBy (org.mycore.services.fieldquery.MCRSortBy)2 ArrayList (java.util.ArrayList)1 SortClause (org.apache.solr.client.solrj.SolrQuery.SortClause)1 Document (org.jdom2.Document)1