Example usage for com.liferay.portal.kernel.search Sort isReverse

List of usage examples for com.liferay.portal.kernel.search Sort isReverse

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.search Sort isReverse.

Prototype

public boolean isReverse() 

Source Link

Usage

From source file:com.liferay.document.library.repository.cmis.search.BaseCmisSearchQueryBuilder.java

License:Open Source License

@Override
public String buildQuery(SearchContext searchContext, Query query) throws SearchException {

    StringBundler sb = new StringBundler();

    sb.append("SELECT cmis:objectId");

    QueryConfig queryConfig = searchContext.getQueryConfig();

    if (queryConfig.isScoreEnabled()) {
        sb.append(", SCORE() AS HITS");
    }/*from  ww w .j av a  2s.  c om*/

    sb.append(" FROM cmis:document");

    CMISDisjunction cmisDisjunction = new CMISDisjunction();

    if (_log.isDebugEnabled()) {
        _log.debug("Repository query support " + queryConfig.getAttribute("capabilityQuery"));
    }

    if (!isSupportsOnlyFullText(queryConfig)) {
        traversePropertiesQuery(cmisDisjunction, query, queryConfig);
    }

    if (isSupportsFullText(queryConfig)) {
        CMISContainsExpression cmisContainsExpression = new CMISContainsExpression();

        traverseContentQuery(cmisContainsExpression, query, queryConfig);

        if (!cmisContainsExpression.isEmpty()) {
            cmisDisjunction.add(cmisContainsExpression);
        }
    }

    if (!cmisDisjunction.isEmpty()) {
        sb.append(" WHERE ");
        sb.append(cmisDisjunction.toQueryFragment());
    }

    Sort[] sorts = searchContext.getSorts();

    if (queryConfig.isScoreEnabled() || ArrayUtil.isNotEmpty(sorts)) {
        sb.append(" ORDER BY ");
    }

    if (ArrayUtil.isNotEmpty(sorts)) {
        int i = 0;

        for (Sort sort : sorts) {
            String fieldName = sort.getFieldName();

            if (!isSupportedField(fieldName)) {
                continue;
            }

            if (i > 0) {
                sb.append(", ");
            }

            sb.append(getCmisField(fieldName));

            if (sort.isReverse()) {
                sb.append(" DESC");
            } else {
                sb.append(" ASC");
            }

            i++;
        }
    } else if (queryConfig.isScoreEnabled()) {
        sb.append("HITS DESC");
    }

    if (_log.isDebugEnabled()) {
        _log.debug("CMIS query " + sb);
    }

    return sb.toString();
}

From source file:com.liferay.faces.demos.list.UserLazyDataModel.java

License:Open Source License

/**
 * This method is called by the PrimeFaces {@link DataTable} according to the rows specified in the currently
 * displayed page of data.//from   www  .j  a  v  a 2 s  .  co  m
 *
 * @param  first      The zero-relative first row index.
 * @param  pageSize   The number of rows to fetch.
 * @param  sortField  The name of the field which the table is sorted by.
 * @param  sortOrder  The sort order, which can be either ascending (default) or descending.
 * @param  filters    The query criteria. Note that in order for the filtering to work with the Liferay API, the
 *                    end-user must specify complete, matching words. Wildcards and partial matches are not
 *                    supported.
 */
@Override
public List<User> load(int first, int pageSize, String sortField, SortOrder sortOrder,
        Map<String, Object> filters) {

    List<User> users = null;

    Sort sort;

    // sort
    if (sortField != null) {

        if (sortOrder.equals(SortOrder.DESCENDING)) {
            sort = SortFactoryUtil.getSort(User.class, sortField, "desc");
        } else {
            sort = SortFactoryUtil.getSort(User.class, sortField, "asc");
        }
    } else {
        sort = SortFactoryUtil.getSort(User.class, DEFAULT_SORT_CRITERIA, "asc");
    }

    try {
        LinkedHashMap<String, Object> params = new LinkedHashMap<String, Object>();
        int liferayOneRelativeFinishRow = first + pageSize + 1;

        boolean andSearch = true;
        int status = WorkflowConstants.STATUS_ANY;

        String firstName = trimExpresssion((String) filters.get("firstName"));
        String middleName = trimExpresssion((String) filters.get("middleName"));
        String lastName = trimExpresssion((String) filters.get("lastName"));
        String screenName = trimExpresssion((String) filters.get("screenName"));
        String emailAddress = trimExpresssion((String) filters.get("emailAddress"));

        // For the sake of speed, search for users in the index rather than
        // querying the database directly.
        Hits hits = UserLocalServiceUtil.search(companyId, firstName, middleName, lastName, screenName,
                emailAddress, status, params, andSearch, first, liferayOneRelativeFinishRow, sort);

        List<Document> documentHits = hits.toList();

        logger.debug(
                ("filters firstName=[{0}] middleName=[{1}] lastName=[{2}] screenName=[{3}] emailAddress=[{4}] active=[{5}] andSearch=[{6}] startRow=[{7}] liferayOneRelativeFinishRow=[{8}] sortColumn=[{9}] reverseOrder=[{10}] hitCount=[{11}]"),
                firstName, middleName, lastName, screenName, emailAddress, status, andSearch, first,
                liferayOneRelativeFinishRow, sortField, sort.isReverse(), documentHits.size());

        // Convert the results from the search index into a list of user
        // objects.
        users = new ArrayList<User>(documentHits.size());

        for (Document document : documentHits) {

            long userId = GetterUtil.getLong(document.get(Field.USER_ID));

            try {
                User user = UserLocalServiceUtil.getUserById(userId);
                users.add(user);
            } catch (NoSuchUserException nsue) {
                logger.error("User with userId=[{0}] does not exist in the search index. Please reindex.");
            }
        }

    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    }

    return users;

}

From source file:com.rknowsys.portal.search.elastic.ElasticsearchIndexSearcher.java

License:Open Source License

private void addSortToSearch(Sort[] sorts, SearchRequestBuilder searchRequestBuilder) {
    String query = searchRequestBuilder.toString();
    if (query.contains("assetTagNames")) //term search
    {//  w w w. j  ava 2 s  .c o m
        //always adds score to the sort
        searchRequestBuilder.addSort(SortBuilders.scoreSort());
    } else //empty search
    {
        //no score needed
        if (query.contains("com.liferay.portal.model.Organization")) {
            searchRequestBuilder
                    .addSort(SortBuilders.fieldSort("name_sortable").ignoreUnmapped(true).order(SortOrder.ASC));
        }
    }
    if (sorts == null) {
        //for alphabetic order on orgs

        return;
    }
    for (Sort sort : sorts) {
        if (sort == null) {
            continue;
        }
        String sortFieldName = sort.getFieldName();
        SortBuilder sortBuilder = null;

        if (DocumentImpl.isSortableTextField(sortFieldName)) {
            sortFieldName = DocumentImpl.getSortableFieldName(sortFieldName);
        }
        if (Validator.isNull(sortFieldName) || !sortFieldName.endsWith("sortable")) {
            continue;
        }
        sortBuilder = SortBuilders.fieldSort(sortFieldName).ignoreUnmapped(true)
                .order(sort.isReverse() ? SortOrder.DESC : SortOrder.ASC);

        searchRequestBuilder.addSort(sortBuilder);
    }
}

From source file:org.rsc.liferay.solr.SolrIndexSearcher.java

License:Open Source License

protected SolrQuery translateQuery(long companyId, Query query, Sort[] sorts, int start, int end)
        throws Exception {

    QueryConfig queryConfig = query.getQueryConfig();

    SolrQuery solrQuery = new SolrQuery();

    if (queryConfig.isHighlightEnabled()) {
        solrQuery.setHighlight(true);//  ww w.j  a v  a 2 s .c om
        solrQuery.setHighlightFragsize(queryConfig.getHighlightFragmentSize());
        solrQuery.setHighlightSnippets(queryConfig.getHighlightSnippetSize());

        String localizedContentName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), Field.CONTENT);

        String localizedTitleName = DocumentImpl.getLocalizedName(queryConfig.getLocale(), Field.TITLE);

        solrQuery.setParam("hl.fl", Field.CONTENT, localizedContentName, Field.TITLE, localizedTitleName);
    }

    solrQuery.setIncludeScore(queryConfig.isScoreEnabled());

    QueryTranslatorUtil.translateForSolr(query);

    String queryString = query.toString();

    StringBundler sb = new StringBundler(6);

    sb.append(queryString);
    sb.append(StringPool.SPACE);
    sb.append(StringPool.PLUS);
    sb.append(Field.COMPANY_ID);
    sb.append(StringPool.COLON);
    sb.append(companyId);

    solrQuery.setQuery(sb.toString());

    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS)) {
        solrQuery.setRows(0);
    } else {
        solrQuery.setRows(end - start);
        solrQuery.setStart(start);
    }

    if (sorts != null) {
        for (Sort sort : sorts) {
            if (sort == null) {
                continue;
            }

            String sortFieldName = sort.getFieldName();

            if (DocumentImpl.isSortableTextField(sortFieldName)) {
                sortFieldName = DocumentImpl.getSortableFieldName(sortFieldName);
            }

            ORDER order = ORDER.asc;

            if (Validator.isNull(sortFieldName) || !sortFieldName.endsWith("sortable")) {

                sortFieldName = "score";

                order = ORDER.desc;
            }

            if (sort.isReverse()) {
                order = ORDER.desc;
            }

            solrQuery.addSort(new SortClause(sortFieldName, order));
        }
    }

    return solrQuery;
}