Value provider in Hybris Solr – Overview
Value providers are used to provide the information to the Solr about how to index the given field/property.
In Hybris , we will have 2 set of data which we send for indexing.
One set of data will be sent as it is to solr for indexing
Another set of data will not be sent to solr as it is directly, instead we will write some custom logic on that data and send it to Solr for indexing.
Please look at the below diagram on the same
Hybris provides an interface called FieldValueProvider which has the method called getFieldValues() with appropriate parameters.
Every value provider we write must implement this interface and override the getFieldValues() method as per our requirement.
We can also extend AbstractPropertyFieldValueProvider abstract class for our custom value provider.
- //Just a method signature
- public Collection<FieldValue> getFieldValues(final IndexConfig indexConfig, final IndexedProperty indexedProperty,
- final Object model) throws FieldValueProviderException
//Just a method signature public Collection<FieldValue> getFieldValues(final IndexConfig indexConfig, final IndexedProperty indexedProperty, final Object model) throws FieldValueProviderException
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.
Name:
The field names are created by getFieldNames() method inside DefaultFieldNameProvider class.
The field names are created in a way that Solr can understand it.
The field names created by FieldNameProvider should match the field names defined inside schema.xml file inside solrfacetsearch extension and good part is that it is already taken care by getFieldNames() method
Value:
It is the value that we have retrieved from the model with some custom logic as per our requirement.
When we should go for Value Provider?
We should go for value providers when we don’t want the exact value from DB to be indexed.
In some cases it’s required that we have to modify the value after getting from DB and send the modified value to Solr so that modified value can be queried later to Solr.
Consider the below Example
Images of a product
We know that there are multiple media formats for the image to be displayed.
While sending image data to solr for indexing, we need to send the images of different formats.
So for this we have to write some custom logic to get the image according to the media format and send it for indexing.
Hence we can go for value provider to write this logic so that right image will be indexed by Solr.
we can see the below code of value providers mentioned in impex for images with different formats
hybris\bin\ext-data\apparelstore\resources\apparelstore\import\coredata\stores\apparel-uk\solr.impex
- # impex for showing indexing attributes with value providers
- 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; img-515Wx515H ;string ; ; ; ; ; ; ;image515ValueProvider
- ;$solrIndexedType; img-300Wx300H ;string ; ; ; ; ; ; ;image300ValueProvider
- ;$solrIndexedType; img-96Wx96H ;string ; ; ; ; ; ; ;image96ValueProvider
- ;$solrIndexedType; img-65Wx65H ;string ; ; ; ; ; ; ;image65ValueProvider
- ;$solrIndexedType; img-30Wx30H ;string ; ; ; ; ; ; ;image30ValueProvider
# impex for showing indexing attributes with value providers 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; img-515Wx515H ;string ; ; ; ; ; ; ;image515ValueProvider ;$solrIndexedType; img-300Wx300H ;string ; ; ; ; ; ; ;image300ValueProvider ;$solrIndexedType; img-96Wx96H ;string ; ; ; ; ; ; ;image96ValueProvider ;$solrIndexedType; img-65Wx65H ;string ; ; ; ; ; ; ;image65ValueProvider ;$solrIndexedType; img-30Wx30H ;string ; ; ; ; ; ; ;image30ValueProvider
When we should not go for Value Provider?
Whenever we want to index the value coming from DB as it is without changing anything.
In simple words, get the data from DB and put the same data in Solr as well as it is for indexing.
Consider the below Example
Basic Product details
Whenever we display product details, we display product name, code and description.
Here we don’t need to do modify these values to display in the Front end.
We get product name,code and description from DB and display as it is in the front end.
So we don’t need to write any custom logic to send these fields from DB to Solr, hence value provider is not required in this case.
we can see the below code of specifying indexing attributes without value providers
hybris\bin\ext-data\apparelstore\resources\apparelstore\import\coredata\stores\apparel-uk\solr.impex
- # impex for showing indexing attributes without value providers
- 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; code ;string ; ; ; ; ;true;true;
- ;$solrIndexedType; name ;text ;sortabletext; ;true; ;true;true;
- ;$solrIndexedType; description ;text ; ; ;true; ; ; ;
# impex for showing indexing attributes without value providers 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; code ;string ; ; ; ; ;true;true; ;$solrIndexedType; name ;text ;sortabletext; ;true; ;true;true; ;$solrIndexedType; description ;text ; ; ;true; ; ; ;
When we don’t define value provider explicitly in solr.impex file then the default value provider ModelPropertyFieldValueProvider will be invoked.
But in this case, the indexing attribute name defined in impex should exactly match the attribute name in the indexed type model class
Consider the below Example
If we are indexing name attribute in Product type and we don’t want to provide value provider for it, then indexed property name in solr.impex should match the attribute name in Product type.
So Product Model should have name attribute and same name attribute should be used as indexed property name in solr.impex file
can you please make a post on value resolvers , like you have done on value providers .
Hi,
Could you please say what is the difference between Value Provider and the Value Resolver.?
Some of the other features you can see if you compare the OotB provider & resolver are :
Resolvers interface gives access to the Solr document whereas provider doesn’t
The way Localized attributes are indexed is different. Resolvers use context to resolve the localized attributes and handle them internally, whereas provider require lot of additional code.
Hello, Karibasappa, please, can you provide an example of indexing Product Price for specific UserGroup( linked to UserPriceGroup with PriceRows ) thanks in advance.
what is the difference between String and Text in solr impex file. When to use String and Text. Please explain
I am new to Hybris and trying different utilities provided by Hybirs. I’m confused between value provider and value resolver. What is the basic difference between these two and what should be used where?
Could you please tell me in brief what value provider is return .
and what is the actual purpose to use it
Hi KB ,
For code and name field , springELValueProvider has written OOTB . on the other hand , description and summary doesn’t have any value provider written .
can you elaborate it ?
Thanks and regards,
Hamid
we can configure springELValueProvider as the property value provider.
In that case, Spring expression is configured as the Value Resolver parameter. If this parameter is missing, the system falls back to the name of indexed property to resolve values.
For Code they have not used any Value Resolve parameter , so no use of springELValueProvider , but for Name they have used getName(#lang) as Value Resolve parameter which gets the value based on locale.
We can use it for description and summary as well by specifying Value Resolve parameter for it.