Skip to main content

Keynotes for Solr Index in Sitecore 9

I've recently implemented a product list page and -as you can guess- I've needed to configure a custom index for my products. I will share some keynotes about that, I believe they might be useful for anyone who needs a similar thing.

How to configure a custom index in Solr?
Luckily, we already have a good article which shows how to create custom Solr indexes in Sitecore 9. I followed the steps and created my product indexes (my Sitecore version is 9.0.1).

I added necessary fields and templates to my config file:

                <!-- Included fields -->
                <include hint="list:AddIncludedField">
                  <Name>{A00DA813-46FF-47E4-ADD7-75C1333ACF32}</Name>
                  <CommercialShortText>{0AFE5D2A-8172-46BA-BD96-FD6838E3D65B}</CommercialShortText>
                  <TitleProductPage>{E7E6F859-F989-4BA3-8F31-7A03C9BD69E4}</TitleProductPage>
                  <ProductDescription>{14EC7D7E-D6ED-4F95-936B-6988F49A8D9B}</ProductDescription>
                </ProductClasses>
                </include>

                <!-- Included templates -->
                <include hint="list:AddIncludedTemplate">
                  <Product>{032168C8-4E59-485A-8FBC-0E587AB41EA6}</Product>
                </include>

However, it was not enough for my case, I needed to add product image field as well.

If you add an Image field to index like other text fields above, it only returns "alt" information of image. You don't get image url or any other details.

You can see why it returns alt info by checking default Solr config file:


ImageFieldRenderer is responsible class for image field type and it is implemented to return alt tag:


What should we do then?

Computed index field.

How to add a computed index field?

It should be added under "raw:AddComputedIndexField" key.

                <fields hint="raw:AddComputedIndexField">
                  <field fieldName="product_image_url" returnType="string">Namespace.ProductImageComputedIndexField, DLL</field>
                </fields>

And your class will look like this:

public class ProductImageComputedIndexField : AbstractComputedIndexField
    {
        public override object ComputeFieldValue(IIndexable indexable)
        {
            var item = (Item)(indexable as SitecoreIndexableItem);

            if (item == null)
                return null;

            if (!item.TemplateID.Equals(IProductConstants.TemplateId)) return null;

            var imageField = (ImageField)item.Fields[IProductConstants.Product_ImageFieldName];

            if (imageField?.MediaItem == null) return null;

            using (new SecurityDisabler())
            {
                using (new SiteContextSwitcher(Factory.GetSite("website")))
                {
                    var uri = MediaManager.GetMediaUrl(imageField.MediaItem, new MediaUrlOptions { UseItemPath = true });
                    return uri;
                }
            }
        }

You think you configured everything correctly but it still doesn't work somehow. Maybe you forgot something and then, you need to check your indexed results.

How to check your indexed items?
There are several options but I will show the basic one: Use Solr panel!

Open your solr; most probably, it works here.

Select your custom index from core selector dropdown, click query and then execute:


You can see the fields are in index and check the results are as expected or not. If not, you can reindex from Sitecore panel or check your code if something configured wrong.

How to create custom SearchResultItem?
After you add fields to config file, they will be added to index. But how will you get those in code?
To do that, you will need to create your own search result class and bind your fields like example below:

public class SearchProductResultItem : SearchResultItem
    {
        [DataMember]
        [IndexField("name")]
        public string ProductName { get; set; }

        [DataMember]
        [IndexField("product_description")]
        public string ProductDescription { get; set; }


        [DataMember]
        [IndexField("product_image_url")]
        public string ProductImageUrl { get; set; }

    }

Then, you can use in query:

using (var context = Index.CreateSearchContext())
{
                    var filter = context.GetQueryable<SearchProductResultItem>()
                        .Filter(x => x.TemplateId == IProductConstants.TemplateId &&
                                   x.Language == Context.Language.Name)
                        .OrderBy(x => x.Name)
                        .Skip(pageInfo.SkipIndex)
                        .Take(pageInfo.PageSize)
                        .ToList();

Hope, it helps!

Comments

Popular posts from this blog

Deploying SolrCloud with Zookeeper on Azure Kubernetes Service (AKS)

SolrCloud on Azure Kubernetes Service (AKS) Running SolrCloud on Kubernetes — particularly Azure Kubernetes Service (AKS) — can provide you with a highly scalable, cost-efficient, and cloud-native architecture.  This guide walks through how I deployed SolrCloud 8.11.2 with Zookeeper on AKS . Why This Matters for Sitecore Deployments If you're running Sitecore XP or XM , you know that Solr is a mandatory dependency — powering xDB indexing, content search. While Sitecore provides a developer-friendly Solr container for local use, it clearly states: ⚠️ The included Solr image is intended only for development and testing . This means Sitecore does not provide a production-ready Solr setup. If you're deploying Sitecore in production — especially in Kubernetes — you need to create your own scalable, HA SolrCloud cluster. That’s why this deployment matters: You’re building a production-grade SolrCloud setup You’re deploying 3 Solr + 3 Zookeeper nodes for high availabilit...

Sitecore Commerce – XC9 Tips – Missing Commerce Components in SXA Toolbox on Experience Editor

I've recently had an issue that commerce components were missing in SXA Toolbox. I setup Sitecore Commerce on top of an existing instance and I already had a SXA website working on it. The idea was to add commerce components and functionality to my existing website. But after commerce setup, the toolbox was still showing default SXA components and commerce components were missing although I add commerce tenant and website modules: I checked Available Renderings under Presentation folder, there was no problem, commerce renderings were there. I created another tenant and website to see if it shows the commerce components in toolbox. Nothing seemed different but I was seeing commerce components for new website and it was missing on existing one. Then, I noticed two things: 1- Selected catalog was empty in content editor (/sitecore/Commerce/Catalog Management/Catalogs) even if I see Habitat_Master catalog in Merchandising section on commerce management panel. 2- Bootstrap ...

Modern Observability for Sitecore 10.4 on AKS: Grafana, Alloy, Loki, and Prometheus

In this post, I’ll walk through how I extended the base Sitecore XM 10.4 AKS setup with a modern observability stack using Grafana, Alloy, Loki, and Prometheus. This setup provides deep insights into both infrastructure and application health, with powerful log aggregation and visualization. Project Overview Base:  Sitecore XM 10.4 running on Azure Kubernetes Service (AKS) Enhancements:  Added a full Grafana observability stack: Grafana  for dashboards and visualization Alloy  (Grafana Alloy, formerly Promtail) for log collection and multiline parsing Loki  for log aggregation and querying Prometheus  for metrics collection All configuration...