FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » FUDforum » FUDforum Suggestions » Highlighting keywords in search results
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Highlighting keywords in search results [message #23587] Wed, 23 March 2005 17:19 Go to next message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
Hi Ilia,

Have you ever considered term highlighting for search results (not the search result page itself - but when the user drills down to a topic)?

I'm not sure how easy/difficult if would be to implement, but it will allow users to locate the info they searched for much quicker.

Best regards.

Frank
Re: Highlighting keywords in search results [message #23591 is a reply to message #23587] Wed, 23 March 2005 18:20 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
No I have not considered this, since it would require message content parsing realtime and that is never the desired operation performance wise.

FUDforum Core Developer
Re: Highlighting keywords in search results [message #23595 is a reply to message #23591] Wed, 23 March 2005 18:56 Go to previous messageGo to next message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
I accept that it would be slightly more resource intensive. Nevertheless, just thought I'll ask since I only know of one forum system (out of many) that doesn't support it.

Best regards.

Frank
Re: Highlighting keywords in search results [message #23603 is a reply to message #23595] Wed, 23 March 2005 23:38 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
Well, if you can come up with an HTML safe JavaScript implementation, I will definately consider it.

FUDforum Core Developer
Re: Highlighting keywords in search results [message #23623 is a reply to message #23603] Thu, 24 March 2005 11:05 Go to previous messageGo to next message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
Here is the initial code that needs to be added to lib.js. Please let me know if this is what you had in mind, and if we need to add/consider anything else.

The following is still missing:

- Differentiate between words and phrases (it currently breaks everything up into words)
- Use different colors for different words/phrases
- Color definitions must go to the CSS template

I will complete the code sometime next week (just don't have enough time right now).

/* Wrapper function - split search text into separate phrases and words */
function highlightSearchTerms(searchText)
{
  if (!document.body || typeof(document.body.innerHTML) == "undefined") return false;
  if ( typeof(searchText) == "undefined") {
    searchText = getParameter("srch");
    // alert("Search String = [" + searchText + "]");
  }
  var bodyText = document.body.innerHTML;
  var searchArray = searchText.split(" ");
  for (var i = 0; i < searchArray.length; i++) {
    // alert("Highlight word = [" + searchArray[i] + "]");
    bodyText = doHighlight(bodyText, searchArray[i]);
  }
  document.body.innerHTML = bodyText;
  return true;
}

/* Highlights a text string by adding HTML tags before and after the search term. */
function doHighlight(bodyText, searchTerm)
{
  const highlightStartTag = "<font style='background-color:yellow;'>";
  const highlightEndTag = "</font>";
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();

  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  return newText;
}

/* Get URL parameter */
function getParameter(paramName) {
  var currentUrl = window.location.search;
  var strBegin = currentUrl.indexOf(paramName) + (paramName.length+1);
  var strEnd = currentUrl.indexOf("&",strBegin);
  if (strEnd==-1) strEnd = currentUrl.length;
  var paramValue = unescape( currentUrl.substring(strBegin,strEnd) );
  while (paramValue.indexOf("+") > 1 ) paramValue = paramValue.replace("+", " ");
  return paramValue;
}


To start highlighting, use

<body onload="highlightSearchTerms("word1 word2");">

or ...

<body onload="highlightSearchTerms();">
with URL parameter: ...&srch=word1+word2

Best regards.

Frank
Re: Highlighting keywords in search results [message #23627 is a reply to message #23623] Thu, 24 March 2005 14:04 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
The code looks promising, I'll review it in the next couple of days and test it in the various browsers.

FUDforum Core Developer
Re: Highlighting keywords in search results [message #23736 is a reply to message #23627] Sun, 27 March 2005 19:37 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
Added in CVS.

FUDforum Core Developer
Re: Highlighting keywords in search results [message #23791 is a reply to message #23736] Tue, 29 March 2005 15:41 Go to previous messageGo to next message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
Thank you very much. I'm not sure if it's important to also highlight phrases. If so, please extract the required code from the attached zip file.

Best regards.

Frank
  • Attachment: fudsearch.zip
    (Size: 1.76KB, Downloaded 1218 times)
Re: Highlighting keywords in search results [message #23792 is a reply to message #23791] Tue, 29 March 2005 15:49 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
The search is keyword not phrase based, which is why the highlighting is implemented based on keywords.

FUDforum Core Developer
Re: Highlighting keywords in search results [message #23793 is a reply to message #23587] Tue, 29 March 2005 15:50 Go to previous messageGo to next message
JamesS is currently offline  JamesS   United States
Messages: 275
Registered: July 2002
Location: Atlanta, GA
Karma: 0
Senior Member
Might I suggest using the span element instead of font? It would be a bit more compliant.

Something like:
<!--
  /* This belongs in the global stylesheet. */
  .highlight { color: black; background-color: yellow; }
-->

<span class="highlight">keyword</span>
Re: Highlighting keywords in search results [message #23794 is a reply to message #23793] Tue, 29 March 2005 16:01 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
the code in CVS uses span.

FUDforum Core Developer
Re: Highlighting keywords in search results [message #23931 is a reply to message #23794] Tue, 05 April 2005 20:00 Go to previous messageGo to next message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
Would it make sense to also use highlighting on the search result page? If so, can this be added?

Best regards.

Frank
Re: Highlighting keywords in search results [message #23936 is a reply to message #23931] Tue, 05 April 2005 20:55 Go to previous messageGo to next message
Ilia is currently offline  Ilia   Canada
Messages: 13241
Registered: January 2002
Karma: 0
Senior Member
Administrator
Core Developer
Kinda problematic becuase only a short portion of the body is displayed and it may not contain the search for key-words.

FUDforum Core Developer
Re: Highlighting keywords in search results [message #24451 is a reply to message #23623] Thu, 28 April 2005 11:09 Go to previous messageGo to next message
Anonymous   India
hi frank

Let me introduce myself, i am kesavan working in Trivandrum, kerala. This highlighting function is doing fine, is there any possiblity to find out the whole word instead of highlighting whatever matching , for example it is highlighting p'art' if i search for art. if you how to implement that please send it to this Id is kesavand(at)gmail(dot)com

Thanks
kesavan Duraisamy
Re: Highlighting keywords in search results [message #25008 is a reply to message #24451] Sun, 22 May 2005 06:46 Go to previous message
naudefj is currently offline  naudefj   South Africa
Messages: 3771
Registered: December 2004
Karma: 28
Senior Member
Administrator
Core Developer
Hi,

Replace the following function in lib.js and let us know if it helped or not:

function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag)
{
        // find all occurences of the search term in the given text,
        // and add some "highlight" tags to them (we're not using a
        // regular expression search, because we want to filter out
        // matches that occur within HTML tags and script blocks, so
        // we have to do a little extra validation)
        var newText = "";
        var i = 0, j = 0;
        var before_c, before_c;
        var lcSearchTerm = searchTerm.toLowerCase();
        var lcBodyText = bodyText.toLowerCase();

        while ((i = lcBodyText.indexOf(lcSearchTerm, i)) > 0) {
                before_c = lcBodyText.charCodeAt(i-1);
                after_c  = lcBodyText.charCodeAt(i+searchTerm.length);
                if (lcBodyText.lastIndexOf(">", i) >= lcBodyText.lastIndexOf("<", i)) {
                        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
                                if ( (before_c < 65 || before_c > 122) && (after_c < 65 || after_c > 122) ) {
                                        newText += bodyText.substring(j, i) + highlightStartTag + bodyText.sub
str(i, searchTerm.length) + highlightEndTag;
                                        i += searchTerm.length;
                                        j = i;
                                        continue;
                                }
                        }
                }
                i++;
        }
        newText += bodyText.substring(j, bodyText.length);

        return newText;
}


Best regards.

Frank
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Template tree view
Next Topic: May we have ability to show some forums only to selected users?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 21 21:07:14 GMT 2024

Total time taken to generate the page: 0.02447 seconds