6

I am new to Azure Search Service, and I wanted to use the hit highlighting feature of Azure Search Service. I am using the .NET SDK NuGet package for azure search.
I used SearchParameter object to mention the hit highlight fields and also the Pre and Post Tag that I require.

searchParameters.HighlightFields = new[] { "Description"};
searchParameters.HighlightPreTag = "<b>";
searchParameters.HighlightPostTag = "</b>";
_searchIndexClient.Documents.Search(searchText, searchParameters);

I am expecting something like this:
SearchText: best
Result (Description) : The best product
The issue is that I do not see any difference in the result with/without using hit highlight. (Description Field is searchable)
Am I missing something?

aochagavia
  • 5,887
  • 5
  • 34
  • 53
Pratik Bhattacharya
  • 3,596
  • 2
  • 32
  • 60
  • 1
    Hi Pratik, I'm from the Azure Search team. Did you look in the Highlights property of the SearchResult object? https://msdn.microsoft.com/en-US/library/azure/dn951218.aspx#P:Microsoft.Azure.Search.Models.SearchResult`1.Highlights – Bruce Johnston Apr 09 '15 at 17:08
  • Hey Bruce, thanks for pointing in that direction, it solved my problem. However I just want to ask one thing, is it possible to get the entire text of a particular field with pre and post tag done, rather that getting text snippets of that field? – Pratik Bhattacharya Apr 10 '15 at 11:36
  • We don't support that today, but feel free to request it on our User Voice site: http://feedback.azure.com/forums/263029-azure-search If you also include some details about your scenario, it would help us prioritize. Thanks! – Bruce Johnston Apr 13 '15 at 19:25

2 Answers2

7

Hit highlighting results are exposed via the Highlights property of the SearchResultBase class: link

Bruce Johnston
  • 8,344
  • 3
  • 32
  • 42
4

The Highlights property contains just a part of the full field value. If you want to show the full field value, you have to merge the highlights into your field value.

Here a snippet that works for me:

public static string Highlight<T>(string fieldName, SearchResult<T> fromResult) where T : class
{
    var value = fromResult.Document.GetType().GetProperty(fieldName).GetValue(fromResult.Document, null) as string;

    if (fromResult.Highlights == null || !fromResult.Highlights.ContainsKey(fieldName))
    {
        return value);
    }

    var highlights = fromResult.Highlights[fieldName];

    var hits = highlights
        .Select(h => h.Replace("<b>", string.Empty).Replace("</b>", string.Empty))
        .ToList();

    for (int i = 0; i < highlights.Count; i++)
    {
        value = value.Replace(hits[i], highlights[i]);
    }

    return value;
}

For ASP.Net MVC

public static MvcHtmlString Highlight<T>(this HtmlHelper htmlHelper, string fieldName, SearchResult<T> fromResult) where T : class
{
    var value = fromResult.Document.GetType().GetProperty(fieldName).GetValue(fromResult.Document, null) as string;

    if (fromResult.Highlights == null || !fromResult.Highlights.ContainsKey(fieldName))
    {
        return MvcHtmlString.Create(htmlHelper.Encode(value));
    }

    var highlights = fromResult.Highlights[fieldName];

    var hits = highlights
        .Select(h => h.Replace("<b>", string.Empty).Replace("</b>", string.Empty))
        .ToList();

    for (int i = 0; i < highlights.Count; i++)
    {
        value = value.Replace(hits[i], highlights[i]);
    }

    return MvcHtmlString.Create(htmlHelper.Encode(value).Replace("&lt;b&gt;", "<b>").Replace("&lt;/b&gt;", "</b>"));
}

In the View you can use it like this:

@model SearchResult<MySearchDocument>
@Html.Highlight(nameof(MySearchDocument.Name), Model)
Markus
  • 3,871
  • 3
  • 23
  • 26