Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem Sorting Search Engine Results

Status
Not open for further replies.

Dawnit

Technical User
Apr 2, 2001
76
US
Hi there,
I'm have trouble tweaking the javascript for a search engine so that it sorts the result descriptions "desc" alphabetically. The search engine is otherwise running successfully on our internal website. I found the code here: Please help!
:)

Code:
<SCRIPT>

 <!-- Begin copyright - This must be retained and posted as is to use this script -->

 <!--// This Script was created by Satadip Dutta. 
 // Email: sat_dutta@post1.com / satadipd@inf.com 
 // URL:[URL unfurl="true"]http://dutta.home.ml.org[/URL]
 // Version 1.1-IExp 
 // Please honor my hard work, if you use a variant of this in your page, 
 // then please email me :) and keep these comments in the Script.
 // This code is Copyright (c) 1997 Satadip Dutta
 // all rights reserved.
 // License is granted to user to reuse this code on their own Web site 
 // if, and only if, this entire copyright notice is included. The Web Site
 // containing this script must be a not-for-profit ( non-commercial ) web site. 
 // Exclusive written permission must be obtained before using this version of the search engine
 //-->

 <!-- End copyright - This must be retained and posted as is to use this script -->

title = new Object();
 desc = new Object();
 links= new Object();
 matched= new Object();
 keywords= new Object();
 found= new Object();
 var temp=0;
 // actual location or the item to be searched
 // description of he location
 // actual link
 // percentage match found
 // keywords as parsed from the input
 // # of titles present in the database
 title[0]=14
 //no of keywords after parsing
 keywords[0]=0
 //no of matches found.
 found[0]=0

 <!-- Begin List of Searchable Items -->

 <!--put the list of the links and descriptions here!!-->

title[1]="Help CSR Computer Service Tech Technology"
 desc[1]="Computer Service Request (CSR)"
 links[1]="[URL unfurl="true"]http://EmpNet/help_how_to/CSR/CSRPre-screen.htm"[/URL]
 matched[1]=0

 title[2]="Help TSR Computer Service Tech Technology"
 desc[2]="Telephone Service Request (TSR)"
 links[2]="[URL unfurl="true"]http://EmpNet/help_how_to/telephones/TSR.htm"[/URL]
 matched[2]=0

 title[3]="Help Service SOR CS facilities"
 desc[3]="Service Order Request (SOR) for facilities help"
 links[3]="[URL unfurl="true"]http://EmpNet/web_forms/SOR/SOR[/URL] Home.htm"
 matched[3]=0

 title[4]="event handler onClick onLoad onMouseover onMouseout onUnload"
 desc[4]="Understanding Event Handlers in JavaScript"
 links[4]="[URL unfurl="true"]http://www.javascriptkit.com/event.htm"[/URL]
 matched[4]=0


 title[5]="object model tree accessing forms "
 desc[5]="Accessing and Validating Forms Using JavaScript (Part 1 of 2)"
 links[5]="[URL unfurl="true"]http://www.javascriptkit.com/form1.htm"[/URL]
 matched[5]=0

 title[6]="form validation onBlur onSubmit"
 desc[6]="Accessing and Validating Forms Using JavaScript (Part 2 of 2)"
 links[6]="[URL unfurl="true"]http://www.javascriptkit.com/form2.htm"[/URL]
 matched[6]=0


 title[7]="date object write html setTimeout function"
 desc[7]="Creating Time Dependent Scripts using JavaScript (Part 1 of 2)"
 links[7]="[URL unfurl="true"]http://www.javascriptkit.com/time1.htm"[/URL]
 matched[7]=0


 title[8]="live clock watermark"
 desc[8]="Creating Time Dependent Scripts using JavaScript (Part 2 of 2)"
 links[8]="[URL unfurl="true"]http://www.javascriptkit.com/time2.htm"[/URL]
 matched[8]=0


 title[9]="image preload rollover effect filter"
 desc[9]="Images and JavaScript- Apples and Apples (Part 1 of 2)"
 links[9]="[URL unfurl="true"]http://www.javascriptkit.com/image1.htm"[/URL]
 matched[9]=0


 title[10]="rollover effect"
 desc[10]="Images and JavaScript- Apples and Apples (Part 2 of 2)"
 links[10]="[URL unfurl="true"]http://www.javascriptkit.com/image2.htm"[/URL]
 matched[10]=0

 title[11]="new window open secondary reload close toolbar menubar status bar "
 desc[11]="Windows and JavaScript"
 links[11]="[URL unfurl="true"]http://www.javascriptkit.com/window.htm"[/URL]
 matched[11]=0

 title[12]="frames access object multiple"
 desc[12]="Loading Two frames with one link."
 links[12]="[URL unfurl="true"]http://www.javascriptkit.com/twoframes.htm"[/URL]
 matched[12]=0

 title[13]="selection list options array text vale"
 desc[13]="I'll hava a double combo please (Part 1 of 2)"
 links[13]="[URL unfurl="true"]http://www.javascriptkit.com/combos1.htm"[/URL]
 matched[13]=0

 title[14]="combo link box jump"
 desc[14]="I'll hava a double combo please (Part 2 of 2)"
 links[14]="[URL unfurl="true"]http://www.javascriptkit.com/combos2.htm"[/URL]
 matched[14]=0

 <!-- End list of Searchable items -->

function search(){
 var skeyword=document.searchengine.keywords.value.toLowerCase();
 var check=1;
 var pos=0;
 var i=0;
 var j=0;
 var itemp=0;
 var config='';

 while (true)
 {
 if (skeyword.indexOf("+") == -1 )
 {
 keywords[check]=skeyword;
 break;
 }
 pos=skeyword.indexOf("+");
 if (skeyword !="+") 
 {
 keywords[check]=skeyword.substring(0,pos);
 check++;
 }
 else
 {
 check--;
 break;
 }
 skeyword=skeyword.substring(pos+1, skeyword.length); 
 if (skeyword.length ==0)
 {
 check--;
 break;
 }

 }
 // the keywords have been put in keywords object.
 keywords[0]=check;

 // matching and storing the matches in matched
 for ( i=1; i<=keywords[0];i++)
 {
 for (j=1;j<=title[0];j++)
 {
 if (title[j].toLowerCase().indexOf(keywords[i]) > -1 )
 {
 matched[j]++;
 }
 } 
 }
 // putting all the indexes of the matched records in found

 for (i=1;i<=title[0];i++)
 {
 if (matched[i] > 0 )
 {
 found[0]++;
 // increment the found 
 found[found[0]]=i;

 } 
 }



 for (i=1;i<=found[0]-1;i++)
 {
 for(j=i+1;j<=found[0];j++)
 {
 if ( matched[found[i]]< matched[found[j]] )
 {
 temp= found[j];
 found[j]=found[i];
 found[i]=temp;
 }
 }
 }

 // end of sort

 output = self;
 output.document.write('<html>');
 output.document.write('<head>');
 output.document.write('<script>');
 output.document.write('window.onerror=new Function("return true")');
 output.document.write('<\/script>');
 output.document.write('<title>Search Results</title>');
 output.document.write('</head>');
 output.document.write('<BODY bgcolor=#ffffff text=#000000 link=#3366CC vlink=#3366CC><basefont face="Arial" font-size=10pt;>');

 output.document.write('<left><h3 style="margin-bottom: 3px"><font color=#0066CC>Search Results</h3></left>'); 
 output.document.write('<hr><br>');
 output.document.write(' The Keyword(s) you searched: '.small() )
 for (i=1; i<=keywords[0]; i++)
 {
 output.document.write( keywords[i].bold() +" ");
 }
 output.document.write('<br>');

 if (found[0]==0)
 {
 //alert(found[0]);
 output.document.write("<p style=color:black>No matches resulted in this search.<p>");
 }
 else
 {
 // data has been found
 output.document.write("<table border=0 cellspacing=20>");
 for (i=1; i<=found[0];i++)
 {
 output.document.write("<tr><td valign=top>");
 output.document.write("<p style=font-size:80%>" +i +".</p>");
 output.document.write("<td valign=top>");
 itemp=found[i];
 output.document.write(desc[itemp].small() +"<br>" +
 links[itemp].link(links[itemp]).small() +"<br>");
 temp= (matched[itemp]/keywords[0])*100
 } 
 found[0]=0;
 output.document.write("</table>");
 }
 output.document.write ("<form><center>"); 
 output.document.write ("<input type='submit' value='Start Another Search' onClick='location.href = 'EmpNet/search_engine.htm' "); 
 output.document.write ("<center></form>");

 output.document.write ("</body></basefont></html>"); 
 output.document.close();
 }
 </SCRIPT>
 
Hi

Interesting choice. That is a 2 decades old script that was a poorly written one even in that era. Why not use a modern one, like Tipue Search ?

Interesting customization. You rewrote the first 3 entries an left all other demo entries in place. Why not set [tt]title[teal][[/teal][purple]0[/purple][teal]]=[/teal][purple]3[/purple][/tt] ( or as many own entries you have ) to not include the demo entries in results ?

Interesting request. Can not imagine why would anybody prefer search results sorted alphabetically instead of relevance.

Anyway. This is what you asked for :
Code:
[gray]// change this line[/gray]
[b]if[/b] [teal]([/teal] matched[teal][[/teal]found[teal][[/teal]i[teal]]]<[/teal] matched[teal][[/teal]found[teal][[/teal]j[teal]]] )[/teal]
[gray]// to this[/gray]
[b]if[/b] [teal]([/teal] desc[teal][[/teal]found[teal][[/teal]i[teal]]] >[/teal] desc[teal][[/teal]found[teal][[/teal]j[teal]]] )[/teal]

Feherke.
feherke.ga
 
Hi feherke,

You're a wizard!

To answer your questions...
I tried Tipue Search but it was not fully compatible with our MS FrontPage2003 intranet site (someday we'll move to SharePoint or something else more up-to-date).

I didn't realize how the title[0]= functioned until I'd spent many, many hours tweaking other parts of the code, and until you suggested it, I hadn't thought of it.

Every entry in the results list was coming back with 100% relevance, so I eliminated that element from the search. Unless I'm confusing things with a totally different search engine I tried.

Thank you SO much for giving me the answer! Cheers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top