mhinze.com archive

this is an archive of the old blog, ended 6/16/08





    21
    Jun

    Search Query Term Highlighter - HttpModule for ASP.NET

    Sometimes I find it useful when, as I'm searching, result documents are presented with my search terms highlighted.  Like they do in Google's cache.

    So I decided to write an HttpModule to handle the highlighting.  I also thought it would be useful to log these terms.  This module can serve that purpose quite nicely with a little modification.

    I found an open source solution so I adapted it.  I ported most of the regular expressions and some of the logic from SEHL, the search engine query terms highlighter for PHP.

    Download: Search Term Highlighter HttpModule for ASP.NET

    To use this HttpModule in any ASP.NET web site, place the class file in your App_Code folder and add this to web.config in the system.web section:

    <httpModules>
        <add type="SearchTermHighlighterModule" name="SearchTermHighlighterModule"/>
    </httpModules>

     

    The HTML source it produces looks like this:

    <span class="highlight-search-query">Ishmael</span>

    So you'll need to include the style definition in your stylesheet:

    .highlight-search-query
    {
        background-color:Lime;
    }

     

    to get this( searching for [ishmael "some years ago" the] ):

     
    image 
     

    One item left on my TODO list is to use multiple CSS classes, so that each term can have it's own color.

     

     

    Technorati tags: , , ,

    Leave a Reply

    You must be logged in to post a comment.

    © 2007 mhinze.com