Configure highlighting in a custom search type
You can configure the highlighting in a custom search type to meet your specific requirements.
You can configure highlighting in two ways:
-
Use
presetOverrideto pass parameters in a LINQ query -
Define a preset and reference it in a LINQ query
Use presetOverride to pass parameters in a LINQ query
To configure highlighting by passing parameters in a LINQ query:
-
Use
presetOverrideto pass parameters in the LINQ query.RequestResponsevar result = context.GetQueryable<SimpleHighlightResultItem>() .Where(it => it.Title == "Sitecore" && it.Text == "customer experience") .WithHighlights(hl => hl.HighlightOn(i => i.HighlightsInText), presetOverride: p => { p.PreTag = "<br>"; p.PostTag = "</br>"; }) .ToArray();The preceding example changes the HTML tags that wrap the highlighted terms and generates the following Solr query:
q=(title_t:("Sitecore") AND text:("customer experience")) ... &hl=true&hl.fl=text&hl.simple.pre=<br>&hl.simple.post=</br>The
HighlightsInTextproperty contains the following segment:RequestResponse" complexity that has previously held marketers back, the latest version of <br>Sitecore</br> makes <br>customer</br> <br>experience</br>"
Define a preset and reference it in a LINQ query
You can also configure highlighting using configuration presets.
A preset is a set of defined highlight parameters with an associated unique name. You can define a preset with multiple highlight parameters and reuse it across your solution.
To define and reference a preset in a LINQ query:
-
Define a preset in the
sitecore>contentSearch>indexConfigurations>defaultSolrIndexConfiguration>highlightOptions>presetStore>presetsnode of your custom Sitecore configuration file.RequestResponse<defaultSolrIndexConfiguration> <highlightOptions> <presetStore> <presets> <preset id="sample" type="Sitecore.ContentSearch.Linq.Highlighting.ConfigurationBasedHighlightsPresetStore+HighlightPreset, Sitecore.ContentSearch.Linq"> <param desc="name">$(id)</param> <param desc="parameters" type="Sitecore.ContentSearch.Linq.Solr.Highlighting.SolrHighlightParameters, Sitecore.ContentSearch.Linq.Solr"> <fragSize>50</fragSize> <maxSnippetsNumber>1</maxSnippetsNumber> <requireFieldMatch>true</requireFieldMatch> <preTag><span></preTag> <postTag></span></postTag> </param> </preset> </presets> </presetStore> </highlightOptions> </defaultSolrIndexConfiguration>The preceding example defined a preset named
sampleand added Solr-specific parameters. -
Reference the preset in the LINQ query:
RequestResponsevar result = context.GetQueryable<SimpleHighlightResultItem>() .Where(it => it.Title == "Sitecore" && it.Text == "customer experience") .WithHighlights(hl => hl.HighlightOn(i => i.HighlightsInText), preset: "sample" ) .ToArray();The preceding LINQ query generates the following Solr query:
q=(title_t:("Sitecore") AND text:("customer experience")) ... &hl=true&hl.fl=text&hl.snippets=1&hl.fragsize=50&hl.requireFieldMatch=true&hl.simple.pre=<span>&hl.simple.post=</span>The
HighlightsInTextproperty contains the segment:RequestResponse" latest version of Sitecore makes <span>customer</span> <span>experience</span>"