Skip to content

Instantly share code, notes, and snippets.

@gillissm
Last active October 10, 2019 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gillissm/c9a1700d714683f5eeafd58c414b7090 to your computer and use it in GitHub Desktop.
Save gillissm/c9a1700d714683f5eeafd58c414b7090 to your computer and use it in GitHub Desktop.
Files demonstrate the old verse the new way of implmenting Solr Suggesters with Sitecore.
<!--
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>
/// <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>();
}
/// <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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment