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;
}