简体   繁体   English

在 Azure 搜索服务中点击突出显示

[英]Hit Highlighting in Azure Search Service

I am new to Azure Search Service, and I wanted to use the hit highlighting feature of Azure Search Service.我是 Azure 搜索服务的新手,我想使用 Azure 搜索服务的命中突出显示功能。 I am using the .NET SDK NuGet package for azure search.我正在使用 .NET SDK NuGet 包进行 azure 搜索。
I used SearchParameter object to mention the hit highlight fields and also the Pre and Post Tag that I require.我使用 SearchParameter 对象来提及命中突出显示字段以及我需要的 Pre 和 Post 标签。

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?我错过了什么吗?

命中突出显示结果通过SearchResultBase类的Highlights属性公开: 链接

The Highlights property contains just a part of the full field value. Highlights 属性仅包含完整字段值的一部分。 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对于 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM