Search in sources :

Example 1 with Indexable

use of org.broadleafcommerce.core.catalog.domain.Indexable 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 2 with Indexable

use of org.broadleafcommerce.core.catalog.domain.Indexable in project BroadleafCommerce by BroadleafCommerce.

the class I18nSolrIndexServiceExtensionHandler method startBatchEvent.

@Override
public ExtensionResultStatusType startBatchEvent(List<? extends Indexable> indexables) {
    List<String> skuIds = new ArrayList<String>(indexables.size());
    List<String> productIds = new ArrayList<String>();
    List<String> skuAttributeIds = new ArrayList<String>();
    List<String> productAttributeIds = new ArrayList<String>();
    for (Indexable indexable : indexables) {
        Sku sku = null;
        if (Product.class.isAssignableFrom(indexable.getClass())) {
            Product product = (Product) indexable;
            productIds.add(product.getId().toString());
            for (Map.Entry<String, ProductAttribute> attributeEntry : product.getProductAttributes().entrySet()) {
                ProductAttribute attribute = attributeEntry.getValue();
                productAttributeIds.add(attribute.getId().toString());
            }
            sku = product.getDefaultSku();
        }
        if (sku != null) {
            skuIds.add(sku.getId().toString());
            for (Map.Entry<String, SkuAttribute> attributeEntry : sku.getSkuAttributes().entrySet()) {
                SkuAttribute attribute = attributeEntry.getValue();
                skuAttributeIds.add(attribute.getId().toString());
            }
        }
    }
    addEntitiesToTranslationCache(skuIds, TranslatedEntity.SKU);
    addEntitiesToTranslationCache(productIds, TranslatedEntity.PRODUCT);
    addEntitiesToTranslationCache(skuAttributeIds, TranslatedEntity.SKU_ATTRIBUTE);
    addEntitiesToTranslationCache(productAttributeIds, TranslatedEntity.PRODUCT_ATTRIBUTE);
    return ExtensionResultStatusType.HANDLED_CONTINUE;
}
Also used : SkuAttribute(org.broadleafcommerce.core.catalog.domain.SkuAttribute) ArrayList(java.util.ArrayList) Product(org.broadleafcommerce.core.catalog.domain.Product) Indexable(org.broadleafcommerce.core.catalog.domain.Indexable) ProductAttribute(org.broadleafcommerce.core.catalog.domain.ProductAttribute) Sku(org.broadleafcommerce.core.catalog.domain.Sku) Map(java.util.Map)

Aggregations

ArrayList (java.util.ArrayList)2 Indexable (org.broadleafcommerce.core.catalog.domain.Indexable)2 IOException (java.io.IOException)1 Map (java.util.Map)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 SolrInputDocument (org.apache.solr.common.SolrInputDocument)1 ServiceException (org.broadleafcommerce.common.exception.ServiceException)1 Locale (org.broadleafcommerce.common.locale.domain.Locale)1 StopWatch (org.broadleafcommerce.common.util.StopWatch)1 Product (org.broadleafcommerce.core.catalog.domain.Product)1 ProductAttribute (org.broadleafcommerce.core.catalog.domain.ProductAttribute)1 Sku (org.broadleafcommerce.core.catalog.domain.Sku)1 SkuAttribute (org.broadleafcommerce.core.catalog.domain.SkuAttribute)1 FieldEntity (org.broadleafcommerce.core.search.domain.FieldEntity)1 IndexField (org.broadleafcommerce.core.search.domain.IndexField)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1