Search keyword highlight in ASP.Net

15,846

Solution 1

Here's what I've decided on. An extension function that I can call on the relevant strings within my page / section of my page:

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;
}

Any further suggestions or comments?

Solution 2

try highlighter from Lucene.net

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

How to use:

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

EDIT: As long as Lucene.net highlighter is not suitable here new link:

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

Solution 3

Use the jquery highlight plugin.

For highlighting it at server side

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() );
}

You can use regular expressions for advanced text replacing.

You can also write the above code in an HttpModule so that it can be re used in other applications.

Share:
15,846
TimS
Author by

TimS

I am a developer for a media company in London. I am a Microsoft Certified Technology Specialist in .NET Framework 3.5, ASP.NET Applications.

Updated on June 07, 2022

Comments

  • TimS
    TimS about 2 years

    I am outputting a list of search results for a given string of keywords, and I want any matching keywords in my search results to be highlighted. Each word should be wrapped in a span or similar. I am looking for an efficient function to do this.

    E.g.

    Keywords: "lorem ipsum"

    Result: "Some text containing lorem and ipsum"

    Desired HTML output: "Some text containing <span class="hit">lorem</span> and <span class="hit">ipsum</span>"

    My results are case insensitive.

  • TimS
    TimS over 14 years
    Thanks for the idea - In this instance I'm trying to do this server side, as it needs to work on a variety of non-JavaScript devices.
  • TimS
    TimS over 14 years
    Looks good, but do I have to be using Lucene.Net for my search results to use the Lucene highligher functions? I'm actually just using a simple stored procedure (the data is only in one table so I don't want to have to build and maintain a separate Lucene index).
  • Ilya Khaprov
    Ilya Khaprov over 14 years
    Here you can find sources svn.apache.org/repos/asf/incubator/lucene.net/trunk/C%23/…. Possibly it will help you make a decision
  • Ilya Khaprov
    Ilya Khaprov over 14 years
    Hmm. Look like you can use it only with Lucene. (( But may be you can use some code from this project...
  • Richard
    Richard over 14 years
    First, this is going to recognise partial matches within words. Your regex needs to be doing whole word replacements only. Secondly, you can enter ' ' instead of Convert.ToChar(" ")
  • TimS
    TimS over 14 years
    Thanks Richard - good tip for char, I knew there must be a better way but it hadn't clicked. RE partial matches, that's what I'm after in this case, as the search uses wildcards (hence the need to make things clearer with highlighting).
  • HasanG
    HasanG almost 14 years
    I'm not sure but there are javascript files for text highlighting. Ex: eggheadcafe.com/articles/highlight_google_keywords.asp
  • Jimmy Mattsson
    Jimmy Mattsson over 12 years
    This looks pretty much like the solution I just wrote to my project. I found a problem if I searched on more than 1 word and the last word were either span, class or hit. That will screw up things bad. I tried to seach for a better solution and found this, so I want to give people a heads up what can go bad if doing like this.
  • Sedat Kapanoglu
    Sedat Kapanoglu about 12 years
    first and last links are down
  • Ilya Khaprov
    Ilya Khaprov about 12 years
    @ssg yes, there is new version of lucene itself and also new documntation layout. current link for this class is incubator.apache.org/lucene.net/docs/2.9.4/html/…. But this is not a permanent link