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

Sitecore 9 - Custom Page Events & Filtering with XConnect

This is the first article of a series. I am going to start with creating a custom page event and will show how we can fetch event data using xconnect api. Let's start with reminding demo scenario: Imagine that you have a website displaying movies. Visitors are able to see movie details and take some actions like save movie or share it.  You want to follow the visitors' activities and you want to take some marketing actions based on those activities. For example, if a contact visits a movie more than X time or she/he saves a movie, you want to send those movies to an external system. In addition, there is going to be a limit to send same movie. Such as, it will not be possible to send same movie more than 2 times.  You want to configure this as a marketing automation plan to give flexibility to your marketing managers. They should be able to add configurable rules and activities.  My first focus is movie detail page. I want to track visitors when they visi...

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 ...

Sitecore Commerce – XC9 Tips – Configuring Postman

In this post, I am going to show how to setup postman and run the scripts. When you download Sitecore Commerce, it includes ready to use postman sample requests (inside Sitecore.Commerce.Engine.SDK.x.x.x folder). You need to import that postman folder to postman app. The steps are written in official documentation , just click import and choose postman folder. You will see some parameters in sample requests such as  {{SitecoreIdServerHost}}, {{OpsApiHost}}, {{OpsApi}} Those are environment based variables that you can change according to your environment details. When you click "setting" icon, you will see predefined environments: AdventureWorks and Habitat. For example, click "Habitat Environment" to edit its variables. You see that AuthoingHost is using port 5000, it is the default port if the that port is available. You can check which port is using your Authoring role from IIS. If your Authoring port is different than 5000, you ne...