Example usage for com.liferay.portal.kernel.search DocumentImpl isSortableTextField

List of usage examples for com.liferay.portal.kernel.search DocumentImpl isSortableTextField

Introduction

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

Prototype

public static boolean isSortableTextField(String name) 

Source Link

Usage

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  om
        //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  w w .  j  av 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;
}