在 ASP.Net 中搜索关键字突出显示

Posted

技术标签:

【中文标题】在 ASP.Net 中搜索关键字突出显示【英文标题】:Search keyword highlight in ASP.Net 【发布时间】:2010-12-10 10:30:41 【问题描述】:

我正在输出给定关键字字符串的搜索结果列表,并且我希望突出显示搜索结果中的任何匹配关键字。每个单词都应该包含在 span 或类似内容中。我正在寻找一个有效的功能来做到这一点。

例如

关键词:“lorem ipsum”

结果:“一些包含 lorem 和 ipsum 的文本”

所需的 html 输出:“Some text containing <span class="hit">lorem</span> and <span class="hit">ipsum</span>

我的结果不区分大小写。

【问题讨论】:

【参考方案1】:

尝试来自 Lucene.net 的荧光笔

http://incubator.apache.org/lucene.net/docs/2.0/Highlighter.Net/Lucene.Net.Highlight.html

使用方法:

http://davidpodhola.blogspot.com/2008/02/how-to-highlight-phrase-on-results-from.html

编辑: 只要 Lucene.net 荧光笔不适合这里新链接:

http://mhinze.com/archive/search-term-highlighter-httpmodule/

【讨论】:

看起来不错,但我是否必须使用 Lucene.Net 来获取我的搜索结果才能使用 Lucene highligher 函数?我实际上只是在使用一个简单的存储过程(数据只在一个表中,所以我不想构建和维护单独的 Lucene 索引)。 这里你可以找到来源svn.apache.org/repos/asf/incubator/lucene.net/trunk/C%23/…。可能会帮助你做出决定 嗯。看起来你只能将它与 Lucene 一起使用。 ((但也许你可以使用这个项目中的一些代码...... 第一个和最后一个链接失效 @ssg 是的,有新版本的 lucene 本身和新的文档布局。该课程的当前链接是incubator.apache.org/lucene.net/docs/2.9.4/html/…。但这不是永久链接【参考方案2】:

使用 jquery highlight 插件。

用于在服务器端突出显示它

protected override void Render( HtmlTextWriter writer )

    StringBuilder html = new StringBuilder();
    HtmlTextWriter w = new HtmlTextWriter( new StringWriter( html ) );

    base.Render( w );

    html.Replace( "lorem", "<span class=\"hit\">lorem</span>" );

    writer.Write( html.ToString() );

您可以使用正则表达式进行高级文本替换。

你也可以把上面的代码写在一个 HttpModule 中,这样它就可以在其他应用程序中重复使用。

【讨论】:

感谢您的想法 - 在这种情况下,我正在尝试做这个服务器端,因为它需要在各种非 javascript 设备上工作。【参考方案3】:

这是我决定的。我可以在我的页面/页面部分中调用相关字符串的扩展函数:

public static string HighlightKeywords(this string input, string keywords)

    if (input == string.Empty || keywords == string.Empty)
    
        return input;
    

    string[] sKeywords = keywords.Split(' ');
    foreach (string sKeyword in sKeywords)
    
        try
        
            input = Regex.Replace(input, sKeyword, string.Format("<span class=\"hit\">0</span>", "$0"), RegexOptions.IgnoreCase);
        
        catch
        
            //
        
    
    return input;

还有什么建议吗?

【讨论】:

首先,这将识别单词中的部分匹配。您的正则表达式只需要进行整个单词替换。其次,可以输入' '而不是Convert.ToChar(" ") 谢谢 Richard - char 的好技巧,我知道一定有更好的方法,但没有成功。 RE 部分匹配,这就是我在这种情况下所追求的,因为搜索使用通配符(因此需要通过突出显示使事情更清晰)。 我不确定,但有用于文本突出显示的 javascript 文件。例如:eggheadcafe.com/articles/highlight_google_keywords.asp 这看起来很像我刚刚写给我的项目的解决方案。如果我搜索超过 1 个单词并且最后一个单词是 span、class 或 hit,我会发现问题。那会把事情搞砸的。我试图寻找更好的解决方案并找到了这个,所以我想提醒人们这样做会导致什么问题。【参考方案4】:

对上述答案的扩展。 (没有足够的声誉发表评论)

当搜索条件为 [span pan an a] 时,为了避免 span 被替换,找到的单词被替换为其他内容,而不是替换回......虽然效率不高......

public string Highlight(string input)

    if (input == string.Empty || searchQuery == string.Empty)
    
        return input;
    

    string[] sKeywords = searchQuery.Replace("~",String.Empty).Replace("  "," ").Trim().Split(' ');
    int totalCount = sKeywords.Length + 1;
    string[] sHighlights = new string[totalCount];
    int count = 0;

    input = Regex.Replace(input, Regex.Escape(searchQuery.Trim()), string.Format("~0~", count), RegexOptions.IgnoreCase);
    sHighlights[count] = string.Format("<span class=\"highlight\">0</span>", searchQuery);
    foreach (string sKeyword in sKeywords.OrderByDescending(s => s.Length))
    
        count++;
        input = Regex.Replace(input, Regex.Escape(sKeyword), string.Format("~0~", count), RegexOptions.IgnoreCase);
        sHighlights[count] = string.Format("<span class=\"highlight\">0</span>", sKeyword);
    

    for (int i = totalCount - 1; i >= 0; i--)
    
        input = Regex.Replace(input, "\\~" + i + "\\~", sHighlights[i], RegexOptions.IgnoreCase);
    

    return input;

【讨论】:

以上是关于在 ASP.Net 中搜索关键字突出显示的主要内容,如果未能解决你的问题,请参考以下文章

在 asp.net 中突出显示新记录

使用 jQuery 突出显示选定的 ASP.NET DataGrid 行

Asp.Net Mvc 突出显示当前页面链接技术?

使用 JavaScript 在 Asp.net mvc 中突出显示 html 文档表达式

在搜索中突出显示多个关键字

在 ASP.NET MVC 视图中突出显示 javascript 文件的语法?