Creating Sitecore Index Part - 3

  Introduction:

In this post we will try to index Complex Fields like DropLink, DropList and so on. In this Part we will only look at DropLink. The process will remain almost the same From Sitecore Configuration perspective. Only change you might come across is Business logic in Asp.Net MVC. Since these field have reference to another Item (and not simple type) and sometimes create different objects, we cannot add them as Simple Field. We will be creating Computed field for them.

Description:

As per part-2, we have now successfully added simple fields. let us see how to add Complex field like Droplink. The Idea here is, to create a business logic that simplify the Output value. Droplink holds GUID of the Item that is been selected. We will try to fetch the Item thats is selected in DropLink, then pull the values as JSON to be indexed.

Background:

We will add Droplink to existing Template (News).


fig.1
fig.2
 

We will add the newly create Field to be part of indexing. let us try to index it as Simple field.
We will follow the same process as we did for other Fields in part-2. 
fig.3
the above figure shows the ID that we need for this Field. we will update our custom config file created for Custom Indexing.
fig.4
Once we save this file, we will see that Sitcore is been refreshed due to updated. Now go to Control panel -> Populate SOLR managed Schema as shown below
fig.5

Similarly, Go to index manager, and rebuild Index.
fig.6
Go to SOLR URL (In My Scenario the URL is) : https://sc10sc.dev.local:8984/solr/#/SC10_master_custom_index/query
 The field we indexed is a DropLink, the value stored in this field will be Selected Item ID. Since we have added this field as Simple Field, it will show the GUID as value in SOLR. The Value will not contain Curly brackets and Dash.
fig.7

We see when we added your field like Simple Field, we got the Item ID that is filtered (without Brackets and dash) . Thus, as to see how we can Apply business logic and get the desired output to be indexed, we will write a business logic that will extract Item ID and Genre Name. We will add the data into JSON format and then add it to be indexed.

Note: I hope you already have a Asp.Net MVC application mapping with Sitecore instance running.
If not it is an easy task it should not take more than 5 to 10 minutes to get a simple app mapped properly with Sitecore application. Just create a back up folder of successfully running Sitecore Application.

fig.8

using BasicSitecoreApp.Models.DropLink;
using Sitecore.ContentSearch;
using Sitecore.ContentSearch.ComputedFields;
using Sitecore.Data.Items;
using System.Text.Json;

namespace BasicSitecoreApp.Indexing.DropLink
{
    public class GenreIndexing : IComputedIndexField
    {
        public string FieldName { get; set; }
        public string ReturnType { get; set; }

        public object ComputeFieldValue(IIndexable indexable)
        {
            Item item = indexable as SitecoreIndexableItem;

            if (item == null)
                return null;

            if ( item.TemplateID.ToString() != "{F92D83EB-9356-4A0F-9779-E140E39E57B3}")
                return null;

            Sitecore.Data.Database currentDb = item.Database;
            Sitecore.Data.ID genreId = new Sitecore.Data.ID(item.Fields["Genre"].Value);
            Item genre = currentDb.GetItem(genreId);
            if (genre == null)
                return null;

            GenreModel genreModel = new GenreModel();
            genreModel.GenreItemId = genre.ID.ToString();
            genreModel.GenreName = genre.DisplayName;

            return (JsonSerializer.Serialize(genreModel));
        }
    }
}

(the above implementation is for demo purpose only, you should follow Clean Code , KISS, SOLID principles and Design pattern)

In the above code we have hardcoded the Template ID  to only entertain our Items that are getting Indexed. We have taken the current DB from Item and fetched the Item that is been selected by Genre Droplink. We than create object and the serialize the object into JSON format.

Once our code is completed, we than deploy our code into Sitecore Web application. Go to Sitecore open the Custom Config File and update that same to have our Computed Field as shown below.

fig.9

we have added field name to be "Genre" and return type is string, save the same. Open Sitecore Control panel, Rebuild Index (shown above in fig.6) .
fig.10
As you can see the values are now as JSON result.

Conclusion:
Based on what we want to achieve, the business logic might change. but the basics will remain the same.

Comments

Popular posts from this blog

Rebuild XDB indexing Issues

Upgrading from Sitecore 9.3 to 10.3: A Practical Guide