2

Wiki post :

Item resolving is a time consuming task for Sitecore. If the item tree is wide (one item can have a lot of children) the search for item can be both resource and time consuming.

Question: How to optimze item search during item resolving?

1 Answer 1

1

To optimize item resolving time, we used SOLR search.

Custom index field

First we added new custom SOLR field :

public class RelativeUrlIndexField : IComputedIndexField
{
    public object ComputeFieldValue(IIndexable indexable)
    {
        try
        {
            // Item Url Generator is an abstraction over LinkBuiilder, to allow easier unit tests.
            var relativeUrl = itemUrlGenerator.GetRelativeItemUrl(item);

            if (relativeUrl.IsNullOrEmpty())
                return string.Empty;

            relativeUrl = new UrlModifier(relativeUrl)
                .RemoveCulture()  // Custom method that removes culture segment from url (E.g. /de/)
                .ToRelativeUrl(); // returns relative url

            return relativeUrl;
        }
        catch (Exception ex)
        {
            Logger.CrawlingError($"Unable to generate url for {item.ID}. ", ex);
            // Unable to generate url..
        }

        return string.Empty;
    }
}

Config

Adding newly created field to Sitecore default (or custom, which is recommended) index.

Content Search config file

<sitecore>
    <contentSearch>
        <indexConfigurations>
             <defaultSolrIndexConfiguration>
                 <documentOptions>
                     <fields>
                         <field fieldName="relativeurlcomputed" returnType="string">
                              MyProject.Feature.ItemResolving.
                         </field>
                     </fields>
                 </documentOptions>
             </defaultSolrIndexConfiguration>
        </indexConfigurations>
    </contentSearch>
</sitecore>

Solr search

Search results class

public class ItemResolvingSearchResult : SearchResultItem 
{
    private const string RelativeUrlComputedField = "relativeurlcomputed";
    [IndexField(RelativeUrlComputedField)]
    public string RelativeUrlGenericLink
    {
        get { return GetStringFromFields(RelativeUrlComputedField); }
        set {}
    }

}

Querying

Next thing was creating a SOLR query that will retrive item:

using (var searchContext = this._sitecoreSearchService.GetSearchContext())
{
    var searchResults = searchContext
        .GetQueryable<ItemResolvingSearchResult>(null)
        .Where(x => x.RelativeUrlGenericLink.EndsWith(url))
        .Take(3)
        .Select(r => new
                         {
                             r.ItemId,
                         })
        .ToList();

    var items = searchResults
        .Select(r => r.ItemId.Guid)
        .ToList();

    return items;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.