In the last blog, we have created Solr auto suggester functionality on a single field. Now in this blog, we are going to discuss how we can display results in suggest component on basis of multiple fields value, suppose you have one field Title and another is a Tag and now when user search any word it will search result on both Title and Tag fields.
To do this, add multiple dictionaries like below for contentSuggester and tagSuggester field.
<searchComponent name="suggest" class="solr.SuggestComponent"> <lst name="suggester"> <str name="name">contentSuggester</str> <str name="lookupImpl">AnalyzingInfixLookupFactory</str> <str name="dictionaryImpl">DocumentDictionaryFactory</str> <str name="field">title_t</str> <str name="contextField">_template</str> <str name="suggestAnalyzerFieldType">text_general</str> <str name="buildOnStartup">false</str> <str name="highlight">false</str> </lst> <lst name="suggester"> <str name="name">tagSuggester</str> <str name="lookupImpl">AnalyzingInfixLookupFactory</str> <str name="dictionaryImpl">DocumentDictionaryFactory</str> <str name="field">tagcontent_s</str> <str name="contextField">_template</str> <str name="suggestAnalyzerFieldType">text_general</str> <str name="buildOnStartup">false</str> <str name="highlight">false</str> </lst> </searchComponent> <requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy"> <lst name="defaults"> <str name="suggest">true</str> <str name="suggest.count">10</str> <str name="suggest.dictionary">contentSuggester</str> <str name="suggest.dictionary">tagSuggester</str> </lst> <arr name="components"> <str>suggest</str> </arr> </requestHandler>
Paste underneath code.
public IEnumerable<string> GetSearchSuggestion(string searchTerm,string templateId) { using (var ctx = ContentSearchManager.GetIndex("sc101_custom_master_index").CreateSearchContext()) { SolrSuggestQuery q = searchTerm; var options = new SuggestHandlerQueryOptions { Parameters = new SuggestParameters { Count = 10, Build = true, ContextFilterQuery = templateId } }; var result = ctx.Suggest(q, options);// This will return results of both Title and Tag field // Apply some Logic here } }
Thank you so much, I hope this will work for you 🙂
This article originally appeared on SWATI GUPTA (SITECORE MVP) | BLOGS (https://swatiguptablogs.blogspot.com/).