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

Value provider in solr hybris - overview

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.

  1. //Just a method signature
  2. public Collection<FieldValue> getFieldValues(final IndexConfig indexConfig, final IndexedProperty indexedProperty,
  3.             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

  1. # impex for showing indexing attributes with value providers
  2. 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
  3. $solrIndexedType; img-515Wx515H          ;string ;            ;    ;    ;    ;    ;    ;image515ValueProvider
  4. ;$solrIndexedType; img-300Wx300H          ;string ;            ;    ;    ;    ;    ;    ;image300ValueProvider
  5. ;$solrIndexedType; img-96Wx96H            ;string ;            ;    ;    ;    ;    ;    ;image96ValueProvider
  6. ;$solrIndexedType; img-65Wx65H            ;string ;            ;    ;    ;    ;    ;    ;image65ValueProvider
  7. ;$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

  1. # impex for showing indexing attributes without value providers
  2. 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
  3. ;$solrIndexedType; code                   ;string ;            ;    ;    ;    ;true;true;
  4. ;$solrIndexedType; name                   ;text   ;sortabletext;    ;true;    ;true;true;
  5. ;$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

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on