Last active
October 10, 2019 19:08
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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>(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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