Skip to main content

Session: Querying: How to use Highlighting

Highlighting can be a great feature for increasing search UX. To take leverage of it use Highlight method.

Syntax

// Make a full-text search dynamic query:
// ======================================
List<Employee> employeesResults = session
// Make a dynamic query on 'Employees' collection
.Query<Employee>()
// Search for documents containing the term 'sales' in their 'Notes' field
.Search(x => x.Notes, "sales")
// Request to highlight the searched term by calling 'Highlight'
.Highlight(
x => x.Notes, // The document-field name in which we search
35, // Max length of each text fragment
4, // Max number of fragments to return per document
out Highlightings salesHighlights) // An out param for getting the highlighted text fragments
// Execute the query
.ToList();
Parameters
fieldNamestringName of a field to highlight.
pathExpression<Func<T, object>>Path to a field to highlight.
fragmentLengthintMaximum length of text fragments that will be returned.
fragmentCountintMaximum number of fragments that will be returned.
optionsHighlightingOptionsOptions that can be used for customization.
highlightingsHighlightingsInstance of a Highlightings that contains the highlight fragments for each returned result.

Options

public string GroupKey { get; set; }

public string[] PreTags { get; set; }

public string[] PostTags { get; set; }
Options
GroupKeystringGrouping key for the results. If null results are grouped by document ID (default).
PreTagsstring[]Array of pre tags used when highlighting.
PostTagsstring[]Array of post tags used when highlighting.

Example

List<SearchItem> results = session
.Query<SearchItem, ContentSearchIndex>()
.Highlight(x => x.Text, 128, 1, out Highlightings highlightings)
.Search(x => x.Text, "raven")
.ToList();

StringBuilder builder = new StringBuilder()
.AppendLine("<ul>");

foreach (SearchItem result in results)
{
string[] fragments = highlightings.GetFragments(result.Id);
builder.AppendLine($"<li>{fragments.First()}</li>");
}

string ul = builder
.AppendLine("</ul>")
.ToString();

Remarks

Default <b></b> tags are coloured and colours are returned in following order:

  • <span style="border-left: 10px solid yellow"> </span>yellow,
  • <span style="border-left: 10px solid lawngreen"> </span>lawngreen,
  • <span style="border-left: 10px solid aquamarine"> </span>aquamarine,
  • <span style="border-left: 10px solid magenta"> </span>magenta,
  • <span style="border-left: 10px solid palegreen"> </span>palegreen,
  • <span style="border-left: 10px solid coral"> </span>coral,
  • <span style="border-left: 10px solid wheat"> </span>wheat,
  • <span style="border-left: 10px solid khaki"> </span>khaki,
  • <span style="border-left: 10px solid lime"> </span>lime,
  • <span style="border-left: 10px solid deepskyblue"> </span>deepskyblue,
  • <span style="border-left: 10px solid deeppink"> </span>deeppink,
  • <span style="border-left: 10px solid salmon"> </span>salmon,
  • <span style="border-left: 10px solid peachpuff"> </span>peachpuff,
  • <span style="border-left: 10px solid violet"> </span>violet,
  • <span style="border-left: 10px solid mediumpurple"> </span>mediumpurple,
  • <span style="border-left: 10px solid palegoldenrod"> </span>palegoldenrod,
  • <span style="border-left: 10px solid darkkhaki"> </span>darkkhaki,
  • <span style="border-left: 10px solid springgreen"> </span>springgreen,
  • <span style="border-left: 10px solid turquoise"> </span>turquoise,
  • <span style="border-left: 10px solid powderblue"> </span>powderblue