Configure highlighting in a custom search type

Abstract

Describes how to 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 presetOverride to pass parameters in a LINQ query

  • Define a preset and reference it in a LINQ query

To configure highlighting by passing parameters in a LINQ query:

  • Use presetOverride to pass parameters in the LINQ query.

    var 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 HighlightsInText property contains the following segment:

    " complexity that has previously held marketers back, the latest version of <br>Sitecore</br> makes <br>customer</br> <br>experience</br>"

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:

  1. Define a preset in the sitecore > contentSearch > indexConfigurations > defaultSolrIndexConfiguration > highlightOptions > presetStore > presets node of your custom Sitecore configuration file.

    <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>&lt;span&gt;</preTag>
                    <postTag>&lt;/span&gt;</postTag>
                  </param>
                </preset>
              </presets>
            </presetStore>
          </highlightOptions>
    </defaultSolrIndexConfiguration>

    The preceding example defined a preset named sample and added Solr-specific parameters.

  2. Reference the preset in the LINQ query:

    var 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 HighlightsInText property contains the segment:

    " latest version of Sitecore makes <span>customer</span> <span>experience</span>"