Index A Custom Product Attribute With Solr in Hybris - Stackextend
Index A Custom Product Attribute With Solr in Hybris - Stackextend
MENU
1. Overview
In Hybris, product list page, search page, product sorting and faceting are powered by Apache Solr.
In this article, I will show you how to index a custom product attribute with Solr and display it on the product list
page.
We will index the unit attribute (no need to create a custom attribute) and display it on the PLP.
2.Implementation
1. First of all, create a custom FieldValueProvider , to convert Unit to a sample/indexable value.
https://www.stackextend.com/hybris/index-a-custom-product-property-with-solr-in-hybris/ 1/12
4/19/2021 Index a custom product attribute with Solr in Hybris - Stackextend
java
1 package com.stackextend.training.core.search.solrfacetsearch.provider.impl;
2
3 // imports...
4
5 public class ProductUnitValueProvider implements FieldValueProvider, Serializable {
6
7 private FieldNameProvider fieldNameProvider;
8 private CommonI18NService commonI18NService;
9
10 @Override
11 public Collection<FieldValue> getFieldValues(final IndexConfig indexConfig, final IndexedPr
12 {
13 if (model instanceof ProductModel)
14 {
15 final ProductModel product = (ProductModel) model;
16 final Collection<FieldValue> fieldValues = new ArrayList<FieldValue>();
17
18 // case of the indexed property is localized
19 if (indexedProperty.isLocalized())
20 {
21 // retrieve and iterate over all the configured languages
22 final Collection<LanguageModel> languages = indexConfig.getLanguages();
23 for (final LanguageModel language : languages)
24 {
25 fieldValues.addAll(createFieldValue(product, language, indexedProperty));
26 }
27 }
28 // case of the indexed property is not localized
29 else
30 {
31 fieldValues.addAll(createFieldValue(product, null, indexedProperty));
32 }
33 return fieldValues;
34 }
35
36 throw new FieldValueProviderException("Error: item is not a Product type !");
37 }
38
39 protected List<FieldValue> createFieldValue(final ProductModel product, final LanguageModel
40 {
41 final List<FieldValue> fieldValues = new ArrayList<FieldValue>();
42 // get Unit name by language
43 final String unitName = getUnitName(product, language);
44 if (unitName != null)
45 {
46 // add Unit name value to the fieldValues list
47 addFieldValues(fieldValues, indexedProperty, language, unitName);
48 }
49 return fieldValues;
50 }
https://www.stackextend.com/hybris/index-a-custom-product-property-with-solr-in-hybris/ 2/12
4/19/2021 Index a custom product attribute with Solr in Hybris - Stackextend
51
52 protected void addFieldValues(final List<FieldValue> fieldValues, final IndexedProperty ind
53 {
54 // generate all Solr fields based on different qualifiers
55 final Collection<String> fieldNames = fieldNameProvider.getFieldNames(indexedProperty,
56 for (final String fieldName : fieldNames)
57 {
58 fieldValues.add(new FieldValue(fieldName, value));
59 }
60 }
61
62 private String getUnitName(ProductModel product, LanguageModel language) {
63 return product.getUnit().getName(commonI18NService.getLocaleForLanguage(language));
64 }
65
66 @Required
67 public void setFieldNameProvider(final FieldNameProvider fieldNameProvider)
68 {
69 this.fieldNameProvider = fieldNameProvider;
70 }
71
72 @Required
73 public void setCommonI18NService(final CommonI18NService commonI18NService)
74 {
75 this.commonI18NService = commonI18NService;
76 }
77 }
Note that no special value provider is needed if you want just to index a basic attribute (boolean, int, String…),
check this article.
xml
1 <!-- ...\trainingecore\resources\trainingecore-spring.xml -->
2
3 <bean id="productUnitValueProvider"
4 class="com.stackextend.training.core.search.solrfacetsearch.provider.impl.ProductUnitValueP
5 <property name="fieldNameProvider" ref="solrFieldNameProvider"/>
6 <property name="commonI18NService" ref="commonI18NService"/>
7 </bean>
3. Create a SolrIndexedProperty instance called unit and attach productUnitValueProvider bean to it, using
an impex.
https://www.stackextend.com/hybris/index-a-custom-product-property-with-solr-in-hybris/ 3/12
4/19/2021 Index a custom product attribute with Solr in Hybris - Stackextend
markup
1 # .../traininginitialdata/import/coredata/stores/hybris/solr.impex
2
3 $solrIndexedType=...
4
5 INSERT_UPDATE SolrIndexedProperty ;solrIndexedType(identifier)[unique=true] ;name[unique=tr
6 ;$solrIndexedType ;unit
4. Add unit attribute to ProductData and run ant all to generate it.
xml
1 <!-- ...\trainingfacades\resources\trainingfacades-beans.xml -->
2
3 <bean class="de.hybris.platform.commercefacades.product.data.ProductData">
4 <property name="unit" type="String"/>
5 </bean>
java
1 package com.stackextend.training.facades.populators.CustomSearchResultVariantProductPopulator;
2
3 // imports...
4
5 public class CustomSearchResultVariantProductPopulator extends SearchResultVariantProductPopula
6
7 @Override
8 public void populate(SearchResultValueData source, ProductData target) {
9
10 super.populate(source, target);
11
12 // populate unit property values
13 target.setUnit(this.<String> getValue(source, "unit"));
14 }
15 }
xml
1 <!-- ...\trainingfacades\resources\trainingfacades-spring.xml -->
2
3 <alias name="customSearchResultProductPopulator" alias="commerceSearchResultProductPopulator"/>
4 <bean id="customSearchResultProductPopulator"
5 class="com.stackextend.training.facades.populators.CustomSearchResultVariantProductPopula
6 parent="variantCommerceSearchResultProductPopulator">
7 </bean>
https://www.stackextend.com/hybris/index-a-custom-product-property-with-solr-in-hybris/ 4/12
4/19/2021 Index a custom product attribute with Solr in Hybris - Stackextend
7. Display the unit value where ever you want inside productListerItem.tag .
xml
1 <!-- ...\trainingstorefront\web\webroot\WEB-INF\tags\desktop\product\productListerItem.tag -->
2
3 ...
4
5 ${product.unit}
6
7 ...
8. Finally, run a full indexation to force Solr to index the unit attribute of all existing products.
Navigate to HMC -> System -> Facet Search -> Indexer operation wizard.
9. Navigate to the PLP, if everything goes well, you will be able to see units.
https://www.stackextend.com/hybris/index-a-custom-product-property-with-solr-in-hybris/ 5/12
4/19/2021 Index a custom product attribute with Solr in Hybris - Stackextend
Mouad EL Fakir
Software Craftsmanship, Stackextend author and Full Stack developer with 6+ years of
experience in Java/Kotlin, Java EE, Angular and Hybris…
I’m Passionate about Microservice architectures, Hexagonal architecture, Event Driven
architecture, Event Sourcing and Domain Driven design (DDD)…
Huge fan of Clean Code school, SOLID, GRASP principles, Design Patterns, TDD and
BDD.
Share this:
https://www.stackextend.com/hybris/index-a-custom-product-property-with-solr-in-hybris/ 6/12