Sitecore API Native Support for Solr Suggest

This is first in what I’ll deem to be a sporadic series on features and functionalities you might not realize exist in Sitecore. With each new release and the move towards ‘micro-service’ style architecture of the platform, little gems are included, but do not make the bright, shiny headline, this series will work to unveil some of these.

The first gem is the that Sitecore has native APIs to allow you to leverage Solr Suggesters.

What’s Solr Suggest?

Solr has a “suggester” component called SuggestComponent. You use this component to give users automatic suggestions for query terms. For example, you create a text box that has an autocomplete feature and suggested terms appear as a user types a query term in the text box.

Opening paragraph from https://doc.sitecore.com/developers/92/platform-administration-and-architecture/en/using-solr-auto-suggest.html, accessed October 2019

This translates to we can create a google like experience for you.

At which point you get a client reaction of, that’s what we always wanted!!! (Win for the development team!) But that win is really just on the surface in earlier versions of Sitecore, as you ended up manually creating and managing the REST API calls in code to make this happen. Usually took a look something like this:

https://gist.github.com/gillissm/c9a1700d714683f5eeafd58c414b7090#file-solrsuggestor-manualsample-cs

/// <summary>
/// Following is a sample of what a common manual implementation of managing the logic for Solr Suggest of a Sitecore search implementation
/// </summary>
/// <param name="term"></param>
/// <returns></returns>
public IEnumerable<string> GetAutoSuggest(string term)
{
    if (term.Length < 3)
      return Enumerable.Empty<string>();
      
    var queryString = new Dictionary<string, string>()
    {
        { "suggest", "true" },
        { "wt", "json" },
        { "suggest.build", "true" },
        { "suggest.q", term.Trim() }
    };

    List<string> suggestTerms = new List<string>();

    using (var httpClient = new HttpClient())
    {
        httpClient.BaseAddress = new Uri("https://solr662:8983/solr");//Really should be pulled from configuration
        var requestUri = QueryHelpers.AddQueryString("MyCustomSitecoreIndex/suggest", queryString);

        HttpResponseMessage response = await httpClient.PostAsync(requestUri).Result;
        var solrSuggestObj = JsonConvert.DeserializeObject<SolrSuggestModel>(response.Content.ToString());

        foreach (var suggestDictionary in solrSuggestObj.suggest)
        {
            foreach (var termSet in suggestDictionary.suggestions)
            {
                suggestTerms.Add(termSet.term);
            }
        }
    }
    return suggestTerms;
}

Solr Suggest Gets Easy

Starting with Sitecore 9.0, Sitecore API includes abstractions to making use of Solr Suggest. See the full documentation for all the details, https://doc.sitecore.com/developers/92/platform-administration-and-architecture/en/using-solr-auto-suggest.html.

But the cliff-notes version is this:

  • Include the following assemblies from the Sitecore NuGet feed
    • Sitecore.ContentSearch.Data
    • Sitecore.ContentSearch.Linq.NoReferences
    • Sitecore.ContentSearch.NoReferences
    • Sitecore.ContentSearch.SolrNetExtension.NoReferences
    • Sitecore.ContentSearch.SolrProvider.NoReferences
  • Using statement namespaces are
    • using Sitecore.ContentSearch.SolrNetExtension;
    • using Sitecore.ContentSearch.SolrProvider.SolrNetIntegration;

With the above in place you just need some simple code as follows

https://gist.github.com/gillissm/c9a1700d714683f5eeafd58c414b7090#file-solrsuggestor-apisample-cs

/// <summary>
/// Solr Suggester Implementation via Sitecore API
/// </summary>
/// <param name="term"></param>
/// <returns></returns>
public IEnumerable<string> GetSuggestions(string term)
{
    if (term.Length < 3)
        return Enumerable.Empty<string>();

    using (var searchContext = ContentSearchManager.GetIndex("mysitecoreindex").CreateSearchContext())
    {
        SolrSuggestQuery q = term;
        var options = new SuggestHandlerQueryOptions
        {
            Parameters = new SuggestParameters { Count = 10, Build = true }
        };

        var result = searchContext.Suggest(q, options);
        List<string> suggestTerms = new List<string>();
        foreach(var suggestionDictionary in result.Suggestions)
        {
            foreach(var suggestTerm in suggestionDictionary.Value.Suggestions)
            {
                suggestTerms.Add(suggestTerm.Term);
            }
        }
        return locations;
    }
    return Enumerable.Empty<string>();
}

With a little front-end help, you can create the following grouped type ahead solution

Solr Configuration

Just for general reference for future me, the following configuration will need to be added to you solrconfig.xml of each core that is to support suggest. This file is normally found at a path similar to: C:\solr662\solr-6.6.2\server\solr\thecodeattic_SitecoreIndex_web\conf\solrconfig.xml.

https://gist.github.com/gillissm/c9a1700d714683f5eeafd58c414b7090#file-sample-solrconfig-xml

<!-- 
  Add this to the solrconfig.xml of all cores that need to support Suggest 
  See Solr documentation for full description of file: https://lucene.apache.org/solr/guide/6_6/suggester.html
-->

<searchComponent name="suggest" class="solr.SuggestComponent">
  <lst name="suggester">
    <str name="name">locationnamedict</str>
    <!-- Check Solr documentation for options for lookup and dictionary implementation -->
    <str name="lookupImpl">AnalyzingLookupFactory</str>
    <str name="dictionaryImpl">DocumentDictionaryFactory</str>
    <!-- Field in the index that auto suggest will be based on -->
    <str name="field">locationname_s</str>
    <!-- Additional details that you can leverage in rendering, normally used for grouping -->
    <str name="payloadField">locationbaselink_s</str>
    <!-- Be sure to mark as text_general to get case-insensitive results -->
    <str name="suggestAnalyzerFieldType">text_general</str>
    <str name="buildOnStartup">true</str>
  </lst>
  <lst name="suggester">
    <str name="name">servicenamedict</str>
    <str name="lookupImpl">AnalyzingLookupFactory</str>
    <str name="dictionaryImpl">DocumentDictionaryFactory</str>
    <str name="field">servicename_s</str>
    <str name="payloadField">service_s</str> 
    <str name="suggestAnalyzerFieldType">text_general</str>
    <str name="buildOnStartup">true</str>
  </lst>
</searchComponent>

<requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy">
  <lst name="defaults">
    <str name="suggest.dictionary">locationnamedict</str>
    <str name="suggest.dictionary">servicenamedict</str>
    <str name="suggest">true</str>
    <str name="suggest.count">10</str>
  </lst>
  <arr name="components">
    <str>suggest</str>
  </arr>
</requestHandler>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.