Search in sources :

Example 6 with IndexFieldType

use of org.broadleafcommerce.core.search.domain.IndexFieldType 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 7 with IndexFieldType

use of org.broadleafcommerce.core.search.domain.IndexFieldType in project BroadleafCommerce by BroadleafCommerce.

the class IndexFieldDaoImpl method getIndexFieldTypes.

@Override
public List<IndexFieldType> getIndexFieldTypes(FieldType facetFieldType) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<IndexFieldType> criteria = builder.createQuery(IndexFieldType.class);
    Root<IndexFieldTypeImpl> root = criteria.from(IndexFieldTypeImpl.class);
    criteria.select(root);
    criteria.where(builder.equal(root.get("fieldType").as(String.class), facetFieldType.getType()));
    TypedQuery<IndexFieldType> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search");
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) IndexFieldType(org.broadleafcommerce.core.search.domain.IndexFieldType) IndexFieldTypeImpl(org.broadleafcommerce.core.search.domain.IndexFieldTypeImpl)

Example 8 with IndexFieldType

use of org.broadleafcommerce.core.search.domain.IndexFieldType in project BroadleafCommerce by BroadleafCommerce.

the class IndexFieldDaoImpl method getIndexFieldTypesByAbbreviationOrPropertyName.

@Override
public List<IndexFieldType> getIndexFieldTypesByAbbreviationOrPropertyName(String name) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<IndexFieldType> criteria = builder.createQuery(IndexFieldType.class);
    Root<IndexFieldTypeImpl> root = criteria.from(IndexFieldTypeImpl.class);
    criteria.select(root);
    List<Predicate> restrictions = new ArrayList<>();
    restrictions.add(builder.or(builder.equal(root.get("indexField").get("field").get("abbreviation").as(String.class), name), builder.equal(root.get("indexField").get("field").get("propertyName").as(String.class), name)));
    restrictions.add(builder.or(builder.isNull(root.get("archiveStatus").get("archived")), builder.equal(root.get("archiveStatus").get("archived"), 'N')));
    criteria.where(restrictions.toArray(new Predicate[restrictions.size()]));
    TypedQuery<IndexFieldType> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog");
    return query.getResultList();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) IndexFieldType(org.broadleafcommerce.core.search.domain.IndexFieldType) IndexFieldTypeImpl(org.broadleafcommerce.core.search.domain.IndexFieldTypeImpl) ArrayList(java.util.ArrayList) Predicate(javax.persistence.criteria.Predicate)

Aggregations

IndexFieldType (org.broadleafcommerce.core.search.domain.IndexFieldType)8 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)4 IndexFieldTypeImpl (org.broadleafcommerce.core.search.domain.IndexFieldTypeImpl)4 ArrayList (java.util.ArrayList)3 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)3 FieldType (org.broadleafcommerce.core.search.domain.solr.FieldType)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 Predicate (javax.persistence.criteria.Predicate)1 ORDER (org.apache.solr.client.solrj.SolrQuery.ORDER)1 SortClause (org.apache.solr.client.solrj.SolrQuery.SortClause)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 ExtensionResultHolder (org.broadleafcommerce.common.extension.ExtensionResultHolder)1 IndexField (org.broadleafcommerce.core.search.domain.IndexField)1 Entity (org.broadleafcommerce.openadmin.dto.Entity)1