3
votes

Wanted to know the exact use of value provider in hybris for solr with some examples. I was working on a project in hybris where I have to add new attribute to Product and want to show it as a facate in product listing page.

2

2 Answers

3
votes

The use of ValueProvider is to transform complexes objects for example (Brand, Price, Unite,...) to a simple primitive data understandable by Solr (string, int, double,...)

For example:

  • Brand to be transformed by the provider to Brand.name
  • Price to be transformed by the provider to Price.formattedValue

As an Example, Let's suppose that your new attribute is Unit for example :

1. First your have to create a ProductUnitValueProvider

public class ProductUnitValueProvider extends AbstractPropertyFieldValueProvider implements Serializable, FieldValueProvider {
    @Override
    public Collection<FieldValue> getFieldValues(final IndexConfig indexConfig, final IndexedProperty indexedProperty, final Object model) throws FieldValueProviderException
    {
        if (model instanceof ProductModel)
        {
            //Product model
            final ProductModel product = (ProductModel) model;

            //List of fieldValues to be inflated and returned
            final Collection<FieldValue> fieldValues = new ArrayList<FieldValue>();

            if (product.getUnit() != null)
            {
                //Create fieldValue, just to make it simple as possible however i would perfer to use FieldNameProvider.getFieldNames() instead, to retrieve all FieldNames for this indexedProperty
                String unitName = product.getUnit().getCode();
                fieldValues.add(new FieldValue(indexedProperty.getName(), unitName));
            }
            //Send FieldValues, to be indexed by Solr
            return fieldValues;
        }
        else
        {
            throw new FieldValueProviderException("Error message!");
        }
    }
}

2. Register your provider to be known by Spring

<bean id="productUnitValueProvider" class="xx.yy.zz.ProductUnitValueProvider" />

3. And Finally associate your valueProvider with SolrIndexedProperty, and added it SolrIndexType using impex

$solrIndexedType=xxx

INSERT_UPDATE SolrIndexedProperty;solrIndexedType(identifier)[unique=true];name[unique=true];type(code);sortableType(code);currency[default=false];localized[default=false];multiValue[default=false];useForSpellchecking[default=false];useForAutocomplete[default=false];fieldValueProvider;valueProviderParameter
;$solrIndexedType; unit; string ; ; ; ; ; ;  ;productUnitValueProvider;

Edit: find more here -> https://www.stackextend.com/hybris/index-a-custom-product-property-with-solr-in-hybris/

0
votes

Imagine you have dynamic property of your product, which is not part of your original ProductModel. For example "Few items left in the store" sign for a product on the Category Page. On your Product Details Page you can calculate this based on your stocklevel and other properties and show it to the customer. BUT, if you want to do this for every product on the Category Page, it will take a lot of time. So the idea is the create a ValueProvider, which will calculate this dynamic property once you start your SOLR Index and add it there as indexed(cached) field. So, on you Category Page, you use this property without calculating again. Also take a look at @Sanchit Khera answer for details.