Search in sources :

Example 6 with IndexField

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

the class SolrIndexServiceImpl method buildIncrementalIndex.

@Override
public Collection<SolrInputDocument> buildIncrementalIndex(List<? extends Indexable> indexables, SolrClient solrServer) throws ServiceException {
    TransactionStatus status = TransactionUtils.createTransaction("executeIncrementalIndex", TransactionDefinition.PROPAGATION_REQUIRED, transactionManager, true);
    if (SolrIndexCachedOperation.getCache() == null) {
        LOG.warn("Consider using SolrIndexService.performCachedOperation() in combination with " + "SolrIndexService.buildIncrementalIndex() for better caching performance during solr indexing");
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("Building incremental product index - pageSize: [%s]...", indexables.size()));
    }
    StopWatch s = new StopWatch();
    try {
        sandBoxHelper.ignoreCloneCache(true);
        extensionManager.getProxy().startBatchEvent(indexables);
        Collection<SolrInputDocument> documents = new ArrayList<>();
        List<Locale> locales = getAllLocales();
        List<Long> productIds = BLCCollectionUtils.collectList(indexables, new TypedTransformer<Long>() {

            @Override
            public Long transform(Object input) {
                return shs.getCurrentProductId((Indexable) input);
            }
        });
        solrIndexDao.populateProductCatalogStructure(productIds, SolrIndexCachedOperation.getCache());
        List<IndexField> fields = null;
        FieldEntity currentFieldType = null;
        for (Indexable indexable : indexables) {
            if (fields == null || ObjectUtils.notEqual(currentFieldType, indexable.getFieldEntityType())) {
                fields = indexFieldDao.readFieldsByEntityType(indexable.getFieldEntityType());
            }
            SolrInputDocument doc = buildDocument(indexable, fields, locales);
            // to the index.
            if (doc != null) {
                documents.add(doc);
            }
        }
        extensionManager.getProxy().modifyBuiltDocuments(documents, indexables, fields, locales);
        logDocuments(documents);
        if (!CollectionUtils.isEmpty(documents) && solrServer != null) {
            solrServer.add(documents);
            commit(solrServer);
        }
        TransactionUtils.finalizeTransaction(status, transactionManager, false);
        if (LOG.isDebugEnabled()) {
            LOG.debug(String.format("Built incremental product index - pageSize: [%s] in [%s]", indexables.size(), s.toLapString()));
        }
        return documents;
    } catch (SolrServerException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw new ServiceException("Could not rebuild index", e);
    } catch (IOException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw new ServiceException("Could not rebuild index", e);
    } catch (RuntimeException e) {
        TransactionUtils.finalizeTransaction(status, transactionManager, true);
        throw e;
    } finally {
        extensionManager.getProxy().endBatchEvent(indexables);
        sandBoxHelper.ignoreCloneCache(false);
    }
}
Also used : Locale(org.broadleafcommerce.common.locale.domain.Locale) FieldEntity(org.broadleafcommerce.core.search.domain.FieldEntity) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ArrayList(java.util.ArrayList) TransactionStatus(org.springframework.transaction.TransactionStatus) IOException(java.io.IOException) StopWatch(org.broadleafcommerce.common.util.StopWatch) SolrInputDocument(org.apache.solr.common.SolrInputDocument) ServiceException(org.broadleafcommerce.common.exception.ServiceException) Indexable(org.broadleafcommerce.core.catalog.domain.Indexable) IndexField(org.broadleafcommerce.core.search.domain.IndexField)

Example 7 with IndexField

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

the class SolrIndexServiceImpl method attachIndexableDocumentFields.

@Override
public void attachIndexableDocumentFields(SolrInputDocument document, Indexable indexable, List<IndexField> fields, List<Locale> locales) {
    for (IndexField indexField : fields) {
        try {
            // If we find an IndexField entry for this field, then we need to store it in the index
            if (indexField != null) {
                List<IndexFieldType> searchableFieldTypes = indexField.getFieldTypes();
                // For each of its search field types, get the property values, and add a field to the document for each property value
                for (IndexFieldType sft : searchableFieldTypes) {
                    FieldType fieldType = sft.getFieldType();
                    Map<String, Object> propertyValues = getPropertyValues(indexable, indexField.getField(), fieldType, locales);
                    ExtensionResultStatusType result = extensionManager.getProxy().populateDocumentForIndexField(document, indexField, fieldType, propertyValues);
                    if (ExtensionResultStatusType.NOT_HANDLED.equals(result)) {
                        // Build out the field for every prefix
                        for (Entry<String, Object> entry : propertyValues.entrySet()) {
                            String prefix = entry.getKey();
                            prefix = StringUtils.isBlank(prefix) ? prefix : prefix + "_";
                            String solrPropertyName = shs.getPropertyNameForIndexField(indexField, fieldType, prefix);
                            Object value = entry.getValue();
                            if (FieldType.isMultiValued(fieldType)) {
                                document.addField(solrPropertyName, value);
                            } else {
                                document.setField(solrPropertyName, value);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOG.error("Could not get value for property[" + indexField.getField().getQualifiedFieldName() + "] for product id[" + indexable.getId() + "]", e);
            throw ExceptionHelper.refineException(e);
        }
    }
}
Also used : IndexFieldType(org.broadleafcommerce.core.search.domain.IndexFieldType) ExtensionResultStatusType(org.broadleafcommerce.common.extension.ExtensionResultStatusType) IndexField(org.broadleafcommerce.core.search.domain.IndexField) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ServiceException(org.broadleafcommerce.common.exception.ServiceException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) IndexFieldType(org.broadleafcommerce.core.search.domain.IndexFieldType) FieldType(org.broadleafcommerce.core.search.domain.solr.FieldType)

Example 8 with IndexField

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

the class IndexFieldDaoImpl method readSearchableFieldsByEntityType.

@Override
public List<IndexField> readSearchableFieldsByEntityType(FieldEntity entityType) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<IndexField> criteria = builder.createQuery(IndexField.class);
    Root<IndexFieldImpl> root = criteria.from(IndexFieldImpl.class);
    criteria.select(root);
    criteria.where(builder.equal(root.get("searchable").as(Boolean.class), Boolean.TRUE), root.get("field").get("entityType").as(String.class).in(entityType.getAllLookupTypes()));
    TypedQuery<IndexField> 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) IndexFieldImpl(org.broadleafcommerce.core.search.domain.IndexFieldImpl) IndexField(org.broadleafcommerce.core.search.domain.IndexField)

Example 9 with IndexField

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

the class IndexFieldDaoImpl method readIndexFieldByFieldId.

@Override
public IndexField readIndexFieldByFieldId(Long fieldId) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<IndexField> criteria = builder.createQuery(IndexField.class);
    Root<IndexFieldImpl> search = criteria.from(IndexFieldImpl.class);
    criteria.select(search);
    criteria.where(builder.equal(search.join("field").get("id").as(Long.class), fieldId));
    TypedQuery<IndexField> query = em.createQuery(criteria);
    query.setHint(QueryHints.HINT_CACHEABLE, true);
    query.setHint(QueryHints.HINT_CACHE_REGION, "query.Search");
    try {
        return query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) IndexFieldImpl(org.broadleafcommerce.core.search.domain.IndexFieldImpl) NoResultException(javax.persistence.NoResultException) IndexField(org.broadleafcommerce.core.search.domain.IndexField)

Example 10 with IndexField

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

the class SolrSearchServiceImpl method buildQueryFieldsString.

protected String buildQueryFieldsString(SolrQuery query, SearchCriteria searchCriteria) {
    StringBuilder queryBuilder = new StringBuilder();
    List<IndexField> fields = shs.getSearchableIndexFields();
    // we want to gather all the query fields into one list
    List<String> queryFields = new ArrayList<>();
    for (IndexField currentField : fields) {
        getQueryFields(query, queryFields, currentField, searchCriteria);
    }
    // we join our query fields to a single string to append to the solr query
    queryBuilder.append(StringUtils.join(queryFields, " "));
    return queryBuilder.toString();
}
Also used : ArrayList(java.util.ArrayList) IndexField(org.broadleafcommerce.core.search.domain.IndexField)

Aggregations

IndexField (org.broadleafcommerce.core.search.domain.IndexField)10 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)4 ServiceException (org.broadleafcommerce.common.exception.ServiceException)4 IndexFieldImpl (org.broadleafcommerce.core.search.domain.IndexFieldImpl)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 ExtensionResultStatusType (org.broadleafcommerce.common.extension.ExtensionResultStatusType)2 Entity (org.broadleafcommerce.openadmin.dto.Entity)2 FieldMetadata (org.broadleafcommerce.openadmin.dto.FieldMetadata)2 PersistencePerspective (org.broadleafcommerce.openadmin.dto.PersistencePerspective)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NoResultException (javax.persistence.NoResultException)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 Locale (org.broadleafcommerce.common.locale.domain.Locale)1 StopWatch (org.broadleafcommerce.common.util.StopWatch)1 Indexable (org.broadleafcommerce.core.catalog.domain.Indexable)1 FieldEntity (org.broadleafcommerce.core.search.domain.FieldEntity)1 IndexFieldType (org.broadleafcommerce.core.search.domain.IndexFieldType)1 FieldType (org.broadleafcommerce.core.search.domain.solr.FieldType)1