Value Provider Vs Value Resolver in Solr

Value Provider:

The provider usually implementsFieldValueProvider, FieldNameProvider in which we override the method called getFieldName or getFieldValue. It takes little amount of time to do such operations on data as every time it returns the filed. The use of value provider is to transform complex objects for example (Brand, Price) to a simple primitive data understandable by solr.

In which case we use value provider:

Steps to implement to create value provider:

There are few steps to implement to create value provider as given below:

  • Create a class either by extending the AbstractFieldValueProvider class or implanting the FieldValueProvider interface. We must override the method name public Collection<FieldValue> getFieldValues(final IndexProperty, final Object mode) throws FieldValueProviderException. According to our requirement, The above method has a parameter called model which is used to get the property values based on the indexed property.

This method also uses FieldNameProvider to create the Name-Value pair for the indexing property.

  • Define the spring bean of the value-provider in the spring.xml file.
  • Specify the indexing attribute in Solr.impex with the value provider.
  • Perform build and update the system. To test that our new attribute is getting indexed as per our requirement, we will perform indexing with the help of the BackOffice. After the successful indexing, we can check at the solr port.

Example for custom value Provider:

package com.mywebsite.core.provider;
import de.hybris.platform.solrfacetsearch.provider.impl.AbstractPropertyFieldValueProvider;
import de.hybris.platform.solrfacetsearch.config.exceptions.FieldValueProviderException;
import de.hybris.platform.solrfacetsearch.provider.FieldValueProvider;
public class CustomValueProvider extends AbstractPropertyFieldValueProvider implements FieldValueProvider
{
private FieldNameProvider filedNameProvider;
@Override
public Collection<FieldValue> getFieldValue(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 = fieldNameProvider.getFieldNames(indexedProperty, null);
if (Product.getUnit() != null){
//if product code found wrap product code with more descriptive wordings.
String unitName = "Seri [" + product.getUnit().getCode() +"]";
fieldValues.add(new FieldValue(indexedProperty.getName(), unitName));
}else{
fieldValues.add(new FieldValue(indexedProperty.getName(), "Seri lain"));
}
//send FieldValues, to be indexed by Solr
return fieldValues;
}else{
throw new FieldValueProviderException("Error message!");
}
final Collection<String> fieldNames = fieldNameProvider.getFieldNames(indexedProperty, null);
}
}

Value Resolver:

Resolver groups the indexed properties that use the same value provider. It provides more context information.

For Example, it is possible to access all the items that are being indexed for a particular batch.Resolver interface gives access to solr document.It uses context to resolve the localized attributes and handle them, and never return.

In which case we use value resolver:

When we need to create and remove local session context, we should choose Resolver. It also calls addFieldValues method which adds field values to the indexed property. There is a property called qualifier provider which is embedded in resolver. it has a class that implements QualifierProvider which applies qualifying data to context.

We usually override the addFiledValues() of AbstarctViewResolver and add field values. Very rarely someone overrides the resolve method because it is not needed usually. If we need to add field values, we override addFieldValues, if we need to embed a new qualifier, we can use spring configuration.

Leave a comment