Santry Technology Solutions, Content Management, DotNetNuke, SharePoint Consulting
Register | Login
Saturday, July 04, 2009

Sections
  
About Us
  
Partners
Downloads
  
 WWWCoder.com Resource Directory

Apply Hit Highlighting and Keyword Context to Your Search Results
4/4/2004 11:51:56 AM

In this article we will provide a code snippet for highlighting keywords within text being searched. In addition to highlighting, we will also pull the keywords from within the context of the searched text for display.

Did you ever want to write a search engine similar to the big ones out there that pull the keywords being searched on from the context of the document? In this code snippet we're going to provide you with a method to search for a string within a document, pull out the surrounding text, and then highlight that keyword within the text being displayed.

We'll create a method called GetWrappingText. This method is going to accept the following parameters:

  • strSearchText: The text to be searched.
  • strKeywords: What we're looking for within the text.
  • intLength: How much of the surrounding text we want to pull.
Private Function GetWrappingText(ByVal strSearchText As _
     String, ByVal strKeywords As String, ByVal intLength As Integer) As String
  'declare our variables.
  Dim tmpText As String = strSearchText
  Dim tmpKeywords As String = strKeywords
  Dim intKeywordPos As Integer
  Dim intLimit As Integer = intLength
  Dim strBefore As String
  Dim strAfter As String
  'our array to hold our keywords for searching.
  Dim aryKeywords() As String
  aryKeywords = tmpKeywords.Split(" ", 100) 'space delimiter
  'see if there is a keyword in our text.
  intKeywordPos = tmpText.ToLower.IndexOf(aryKeywords(0).ToLower)
  Dim intLength1 As Integer
  If intKeywordPos <> -1 Then 'found a keyword
    'pull out the text before the keyword.
    strBefore = tmpText.Substring(0, intKeywordPos) 
    If strBefore.Length > intLimit Then
      intLength1 = strBefore.Length - intLimit
      strBefore = strBefore.Remove(0, intLength1 - 1)
    End If
    intLength1 = intKeywordPos + aryKeywords(0).ToString.Length
    Dim intLength2 As Integer
    intLength2 = tmpText.Length
    strAfter = tmpText.Substring(intLength1)
    If strAfter.Length > intLimit Then
      intLength1 = strAfter.Length - intLimit
      strAfter = strAfter.Substring(0, intLimit)
    End If
    tmpText = strBefore & aryKeywords(0).ToString & strAfter
    Dim j As Integer
    'loop through each keyword and check to see if it is in the text.
    Do While j <> aryKeywords.Length
      If (aryKeywords(j).ToString.Trim.Length > 0) Then
        'create a regular expression to insert the html bold tags.
        Dim regex As regex = New regex(aryKeywords(j), RegexOptions.IgnoreCase)
        Dim input As String = tmpText
        Dim replacement As String = "<b>" + aryKeywords(j) & "</b>"
        tmpText = regex.Replace(input, replacement)
      End If
      j = j + 1
    Loop
    tmpText = "..." & tmpText
  End If
  Return tmpText
End Function

So now if you're developing a search engine and someone types in some keywords like, the following would be displayed.

    Here are the results of running a keyword search with hit highlighting code applied to display the search text bolded.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 8/29/2007 12:39:01 PM by Anonymous
Comments:
Left on 5/28/2007 12:52:51 PM by Anonymous
Comments: what about string inside html code like <span id=high>high</span>
this function replace both of them.
but thanks for your article.
please if you have any idea for this problem contact me :infovwd@yahoo.com
Left on 11/8/2006 9:36:16 AM by Anonymous
Comments: explain the integration of the code!!!
Left on 7/28/2005 11:37:29 PM by Anonymous
Comments: Absolutely briliant mate
No ratings available.
Left on 6/15/2005 2:55:41 AM by Anonymous
Comments: Good one ,Thank You
No ratings available.
Left on 2/25/2005 9:31:50 AM by Anonymous
Comments: Jesus, how about some of yopu learn some progarmmnig on your own? The comment about first line "missing )"...I'm laughing at you. If you know shit you fixed it!

/Tobbe, system developer
No ratings available.
Left on 1/6/2005 4:16:06 AM by Anonymous
Comments: Total crap. dosnt work.how abot a demo..
Left on 1/6/2005 12:43:12 AM by Anonymous
Comments: Microsoft VBScript compilation (0x800A03EE)
Expected ')'

In the first Line!!
at column 53

This should work. But a demo would be nice to prove it does.
No ratings available.
Left on 11/16/2004 9:37:12 AM by Anonymous
Comments: Who's the author? And when article was published?
No ratings available.
Left on 8/3/2004 10:41:06 AM by Anonymous
Comments: The only problem with this, as I found when I did a JavaScript version, is that you can run into problems when trying to parse HTML blocks - you can end up highlighting actual HTML properties or other pieces of the tags, which in turn effectively ruins the formatting of the page, or breaks links or images.  Not ideal.
Left on 8/2/2004 1:08:49 PM by Anonymous
Comments: how would you incorporate this function in the microsoft indexer default.asp page associated with the default index server search page?
Left on 7/15/2004 2:39:37 PM by Anonymous
Comments: Definitely, this code needs more comments...
No ratings available.
Left on 6/25/2004 12:17:21 PM by Anonymous
Comments: who's the author?
No ratings available.
Left on 5/28/2004 5:31:21 PM by Anonymous
Comments: Regex = Regular Expression, Import System.Text.RegularExpressions
No ratings available.
Left on 5/17/2004 10:21:50 PM by Anonymous
Comments: what is regex ? is it a class. Please provide some more info about regex . i am getting an error that regex  is not declare. what namespace i need to use?
Left on 5/11/2004 10:39:02 AM by Anonymous
Comments: Assumes looking for a whole word. What if I search for "qui", it should return quick, quit, quiet, etc. This code would not do that.
Left on 5/7/2004 8:22:11 PM by Anonymous
Comments: where is the results ?
www.e-classifiedad.com
Left on 5/7/2004 3:05:32 PM by Anonymous
Comments: Not very well written(explained)...comments
Left on 5/7/2004 11:53:23 AM by Anonymous
Comments: It does not preserve case of keywords if it's upper case.
No ratings available.
Left on 5/7/2004 8:40:03 AM by Anonymous
Comments: it does preserve case
No ratings available.
Left on 5/6/2004 11:37:58 PM by Anonymous
Comments: worked very well for me. very easy.
Left on 5/6/2004 7:41:26 PM by Anonymous
Comments: How to make regex ignore case but preserve it when replacing?
Left on 5/6/2004 3:32:53 PM by Anonymous
Comments: not understood
Left on 4/5/2004 10:59:56 AM by Kingsley Tagbo
Comments: Not enough explanation of code
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970