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

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

Introduction

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

Prototype

public String getFieldName() 

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.  jav  a2s  . c o  m*/

    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.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
    {//from ww w.j  a v  a2 s . co  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);/*from   w  ww .  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;
}