Search in sources :

Example 6 with SortClause

use of org.apache.solr.client.solrj.SolrQuery.SortClause 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 7 with SortClause

use of org.apache.solr.client.solrj.SolrQuery.SortClause in project BroadleafCommerce by BroadleafCommerce.

the class SolrHelperServiceImpl method attachSortClause.

@Override
public void attachSortClause(SolrQuery query, SearchCriteria searchCriteria, String defaultSort) {
    String sortQuery = searchCriteria.getSortQuery();
    if (StringUtils.isBlank(sortQuery)) {
        sortQuery = defaultSort;
    }
    if (StringUtils.isNotBlank(sortQuery)) {
        String[] sortFields = sortQuery.split(",");
        List<String> sortableFieldTypes = getSortableFieldTypes();
        for (String sortField : sortFields) {
            String[] sortFieldsSegments = sortField.split(" ");
            String requestedSortFieldName = sortFieldsSegments[0];
            ORDER order = getSortOrder(sortFieldsSegments, sortQuery);
            ExtensionResultStatusType result = searchExtensionManager.getProxy().attachSortField(query, requestedSortFieldName, order);
            if (!ExtensionResultStatusType.NOT_HANDLED.equals(result)) {
                // if an index field was not found, or the extension handler handled attaching the sort field, move to the next field
                continue;
            }
            List<IndexFieldType> fieldTypes = indexFieldDao.getIndexFieldTypesByAbbreviation(requestedSortFieldName);
            // Used to determine if, by looping through the index field types managed in the database, we actually
            // attach the sort field that is being requested. If we do, then we shouldn't manually add the requested
            // sort field ourselves but if not, we should
            boolean requestedSortFieldAdded = false;
            // and some not  give a preference to NOT tokenized fields, and remove tokenized.
            // In case you have tokenized only just add them all
            List<SortClause> sortClauses = new ArrayList<>();
            boolean foundNotTokenizedField = false;
            // this sorts by them all
            for (IndexFieldType fieldType : fieldTypes) {
                String field = getPropertyNameForIndexField(fieldType.getIndexField(), fieldType.getFieldType());
                // be checked
                if (fieldType.getIndexField().getField().getAbbreviation().equals(requestedSortFieldName)) {
                    requestedSortFieldAdded = true;
                }
                SortClause sortClause = new SortClause(field, order);
                if (sortableFieldTypes.contains(fieldType.getFieldType().getType())) {
                    if (!sortClauses.isEmpty() && !foundNotTokenizedField) {
                        sortClauses.clear();
                    }
                    sortClauses.add(sortClause);
                    foundNotTokenizedField = true;
                } else if (!foundNotTokenizedField) {
                    sortClauses.add(sortClause);
                }
            }
            if (!foundNotTokenizedField) {
                LOG.warn(String.format("Sorting on a tokenized field, this could have adverse effects on the ordering of results. " + "Add a field type for this field from the following list to ensure proper result ordering: [%s]", StringUtils.join(sortableFieldTypes, ", ")));
            }
            if (!sortClauses.isEmpty()) {
                for (SortClause sortClause : sortClauses) {
                    query.addSort(sortClause);
                }
            }
            // field anyway since we're trusting that the field was actually added to the index by some programmatic means
            if (!requestedSortFieldAdded) {
                query.addSort(new SortClause(requestedSortFieldName, order));
            }
        }
    }
}
Also used : ORDER(org.apache.solr.client.solrj.SolrQuery.ORDER) IndexFieldType(org.broadleafcommerce.core.search.domain.IndexFieldType) SortClause(org.apache.solr.client.solrj.SolrQuery.SortClause) ArrayList(java.util.ArrayList) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType)

Example 8 with SortClause

use of org.apache.solr.client.solrj.SolrQuery.SortClause in project ddf by codice.

the class ReindexCommand method getHits.

private long getHits(org.codice.solr.client.solrj.SolrClient sourceSolr) throws IOException, SolrServerException {
    final SolrQuery query = getQuery();
    query.setRows(0);
    query.setStart(0);
    query.addSort(new SortClause("id_txt", ORDER.desc));
    /**
     * Always query the default collection, unable to use overload function that takes in a core
     * name due to how HttpSolrClientFactory create its client
     * https://github.com/codice/ddf/blob/master/platform/solr/solr-factory-impl/src/main/java/org/codice/solr/factory/impl/HttpSolrClientFactory.java#L130
     * this limitation also spill over when using solr Cloud
     */
    QueryResponse response = sourceSolr.query(query);
    if (response != null && response.getResults() != null) {
        return response.getResults().getNumFound();
    }
    return 0;
}
Also used : SortClause(org.apache.solr.client.solrj.SolrQuery.SortClause) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Aggregations

SortClause (org.apache.solr.client.solrj.SolrQuery.SortClause)8 SolrQuery (org.apache.solr.client.solrj.SolrQuery)3 ArrayList (java.util.ArrayList)2 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 DatasetFieldType (edu.harvard.iq.dataverse.DatasetFieldType)1 DataverseFacet (edu.harvard.iq.dataverse.DataverseFacet)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 ObjectOutputStream (java.io.ObjectOutputStream)1 Field (java.lang.reflect.Field)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 EJBTransactionRolledbackException (javax.ejb.EJBTransactionRolledbackException)1 TransactionRolledbackLocalException (javax.ejb.TransactionRolledbackLocalException)1 NoResultException (javax.persistence.NoResultException)1 ORDER (org.apache.solr.client.solrj.SolrQuery.ORDER)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1