Solr Indexing with basic attribute


Let’s understand Solr indexing with basic attribute and example


Requirement:


Add a new attribute called alias to the Product model and display it on the apparel storefront.

We all know that most of the product attributes are indexed which means they will be retrieved from the Solr when we do the search of a product.

Let’s also include alias in the indexing process so that it will be retrieved while searching the Product.

Implementation steps for the above requirement


Step 1


Add a new attribute called alias to the Product model

We know that in Hybris we need to define the attributes inside *items.xml

Let’s define alias attribute inside trainingcore-items.xml(hybris\bin\custom\training\trainingcore\resources) as below

  1. <itemtype code="Product" autocreate="false" generate="false">
  2.                 <attributes>
  3.                     <attribute autocreate="true" qualifier="alias" type="java.lang.String"
  4.                         generate="true">
  5.                         <persistence type="property" />
  6.                         <modifiers read="true" write="true" search="true"
  7.                             initial="true" optional="true" unique="false" />
  8.                     </attribute>
  9.                 </attributes>
  10.             </itemtype>
<itemtype code="Product" autocreate="false" generate="false">
				<attributes>
					<attribute autocreate="true" qualifier="alias" type="java.lang.String"
						generate="true">
						<persistence type="property" />
						<modifiers read="true" write="true" search="true"
							initial="true" optional="true" unique="false" />
					</attribute>
				</attributes>
			</itemtype>


Step 2


Build and update the system

Do ant all and update system by selecting trainingcore only ,so that newly added attribute will be available in Model class as well as DB.

Step 3


Add the new attribute alias in the solr.impex file for indexing as below

  1. # impex for indexing alias attribute
  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; alias                  ;string ;            ;    ;;    ;true;true;                 ;
# impex for indexing alias attribute
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; alias                  ;string ;            ;    ;;    ;true;true;                 ;

We have added alias as the indexed property.

Since this attribute is of basic String type, we don’t need to provide value provider for the same.

We have also made useForSpellchecking & useForAutocomplete as true so that they sit in the Solr spellcheck and autocomplete dictionaries.

Note:
If you are indexing any localizing property then you need to add that field inside each localized solr impex file

Step 4


Add the sample data for alias attribute to one of the existing products as below
hybris\bin\ext-data\apparelstore\resources\apparelstore\import\sampledata\productCatalogs\apparelProductCatalog\products.impex

  1. # Impex for adding sample data
  2. INSERT_UPDATE ApparelProduct;code[unique=true];$catalogVersion;unit(code);supercategories(code,$catalogVersion);varianttype(code);$approved;$taxGroup;ean;genders(code);alias
  3. ;300441142;;pieces;Blue Tomato,caps;;;;1022436212;MALE; alias for blue cap
# Impex for adding sample data
INSERT_UPDATE ApparelProduct;code[unique=true];$catalogVersion;unit(code);supercategories(code,$catalogVersion);varianttype(code);$approved;$taxGroup;ean;genders(code);alias
;300441142;;pieces;Blue Tomato,caps;;;;1022436212;MALE; alias for blue cap

we have added the alias value as alias for blue cap

After running this impex , verify the alias in HMC under the same product , it should reflect the new value for alias.

HMC->Catalog->Products->search-> enter the value for Article Number β€œ300441142”
Select the product and open administration tab, you should have alias with new value.

Note:
Make sure you do this for online version, if doing it for staged version then don’t forget to synchronize the product catalog.

Step 5


Do ant all and update system by selecting apparel store only

This will call the solr related impex (updated in Step 3) to be loaded

If you want to skip this step for saving time, run the above solr impex manually by going to HAC/console/Impex Import

Step 6


Add the alias column in product data and update populator for the same

This will help us in displaying the indexed alias data in the product details page.

Add alias as new property for ProductData in the trainingfacades-beans.xml
\hybris\bin\custom\training\trainingfacades\resources\trainingfacades-beans.xml

  1. <bean class="de.hybris.platform.commercefacades.product.data.ProductData">
  2.    
  3.         <property name="alias" type="String"/>
  4.     </bean>
<bean class="de.hybris.platform.commercefacades.product.data.ProductData">
	
		<property name="alias" type="String"/>
	</bean>



update the populator
hybris\bin\custom\training\trainingfacades\src\org\training\facades\populators\ApparelProductPopulator.java

  1. @Override
  2.     public void populate(final ProductModel source, final ProductData target) throws ConversionException
  3.     {
  4.         final ProductModel baseProduct = getBaseProduct(source);
  5.  
  6.         if (baseProduct instanceof ApparelProductModel)
  7.         {
  8.             final ApparelProductModel apparelProductModel = (ApparelProductModel) baseProduct;
  9.             if (CollectionUtils.isNotEmpty(apparelProductModel.getGenders()))
  10.             {
  11.                 final List<GenderData> genders = new ArrayList<GenderData>();
  12.                 for (final Gender gender : apparelProductModel.getGenders())
  13.                 {
  14.                     genders.add(getGenderConverter().convert(gender));
  15.                 }
  16.                 target.setGenders(genders);
  17.             }
  18.             target.setAlias(apparelProductModel.getAlias());
  19.         }
  20.     }
@Override
	public void populate(final ProductModel source, final ProductData target) throws ConversionException
	{
		final ProductModel baseProduct = getBaseProduct(source);

		if (baseProduct instanceof ApparelProductModel)
		{
			final ApparelProductModel apparelProductModel = (ApparelProductModel) baseProduct;
			if (CollectionUtils.isNotEmpty(apparelProductModel.getGenders()))
			{
				final List<GenderData> genders = new ArrayList<GenderData>();
				for (final Gender gender : apparelProductModel.getGenders())
				{
					genders.add(getGenderConverter().convert(gender));
				}
				target.setGenders(genders);
			}
			target.setAlias(apparelProductModel.getAlias());
		}
	}

we have added target.setAlias(apparelProductModel.getAlias()); line to add the alias to ProductData


Step 7


Modify the JSP files for displaying alias field in Product details page

hybris\bin\custom\training\trainingstorefront\web\webroot\WEB-INF\tags\desktop\product\productDetailsPanel.tag

add below line under existing div < div class="span-10 productDescription last" > as below

  1. <ycommerce:testId code="productDetails_productNamePrice_label_${product.code}">
  2.             Alias : ${product.alias}
  3.             <h1>
  4.                 ${product.name}
  5.             </h1>
  6. </ycommerce:testId>
<ycommerce:testId code="productDetails_productNamePrice_label_${product.code}">
			Alias : ${product.alias}
			<h1>
				${product.name}
			</h1>
</ycommerce:testId>


Step 8


Run the indexing job through HMC as below

HMC -> System -> Facet Search -> indexer operation wizard
Select full in the indexer operation
Select apparel-ukIndex in Solr configuration
Click on Start and make sure it completes successfully

Step 9


Start the server and access the below url
http://localhost:9001/trainingstorefront/?site=apparel-uk

Try to search products using alias in auto suggestion and verify the results

Product search auto suggestion in Solr


Step 10


Verify the UI to check newly added attribute alias displayed on the site

product search results in solr


We are able to get the indexed data successfully from Solr search

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