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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Site search

Status
Not open for further replies.

timcadieux

Programmer
May 25, 2005
129
BE
Hey folkz, wanted to know if anyone knows of a good, fast Site Search app that I could run on my ASP site? Preferably one that does paging.
 
here you go buddy....complete working code...

Code:
<html><head><title>Search our Site</title></head><body>

<%
'/////////////////////////////////////////////////////////////////////////////////
'// Initialisation

' Declare variables.
dim target, firstRow, rowCount, intRowColor

' Get the request parameters.
target	 = Request("target")	' The search request
firstRow = Request("fr")		' First row of results to display
rowCount = Request("rc")		' Record Count - number of rows to show

' Set default values if none found
if firstRow = "" or not IsNumeric(firstRow) Then 
	firstRow = 1
else
	firstRow = CInt(firstRow)
End If
if rowCount = "" or not IsNumeric(rowCount) Then 
	rowCount = 10
else
	rowCount = CInt(rowCount)
End If

Dim ScriptName, ServerName
ScriptName = Request.ServerVariables("SCRIPT_NAME")
ServerName = Request.ServerVariables("SERVER_NAME")

' Construct base URL for navigation buttons
dim URL
URL = ScriptName & "?target=" & Server.URLEncode(target)
URL = URL & "&rc=" & Server.URLEncode(rowCount)

'/////////////////////////////////////////////////////////////////////////////////
'// The search form

%>
<form name="searchForm" action="<%=ScriptName%>">
   <b>Search for:</b>
   <INPUT TYPE="text" NAME="target" SIZE="45" MAXLENGTH="100" VALUE="<%=target%>">
   <INPUT TYPE="submit" VALUE=" Search ">
</form>

<hr size=2 noshade><br>
   
<% 
DoSearch target

'/////////////////////////////////////////////////////////////////////////////////
'// Perform the search

sub DoSearch(target)

   on error resume next
   if target <> "" then

      dim strQuery
      ' strQuery = "$contents " & target ' for free text search
      strQuery = "(#filename *.asp) AND " &_
	             "(NOT #vpath *\_vti*)          AND (NOT #vpath *\cgi-bin*) AND " &_
			     "(NOT #vpath *\IISOrigBackup*) AND (NOT #vpath *\scripts*) AND " &_
			     "(NOT #vpath *\_private*) AND " &_
			     "(" & target & ")"
   
   [red]   ' Create the Index Server query object, setting the columns, the sort to
      ' descending, the max records to 300, and the query string to the given
      ' target. Note that the query string specifies, with NOT and the
      ' #vpath operators, that any files in the *\_vti path, should be excluded.
      ' (_vti* directories contain FrontPage Extension files, and we don't want
      ' users browsing them.)[/red]
   
      dim ixQuery   ' Index Server query object.
      set ixQuery = Server.CreateObject("ixsso.Query")
      if (Err.description <> "") Then
         Response.Write ("<p><b>Query object Error: " & Err.description & ".</b></p>" & vbCRLF)
         Exit sub
      end if
   
      ixQuery.Columns    = "doctitle, vpath, filename, size, write, characterization, rank"
      ixQuery.SortBy     = "rank[d], doctitle"
      ixQuery.MaxRecords = 300
      ixQuery.Query      = strQuery
      ' ixQuery.Catalog = "MyCatalog" ' Specify you catalog here if it's not the default

      ' Create a search utility object to allow us to specify the search type as 'deep',
      ' meaning it will search recursively down through the directories
      dim util      
      set util = Server.CreateObject("ixsso.Util")
      util.AddScopeToQuery ixQuery, Server.MapPath("/"), "deep"
      if (Err.description <> "") Then
         Response.Write ("<p><b>Search Utility Error: " & Err.description & ".</b></p>" & vbCRLF)
         Exit sub
      end if
   
      ' Run the query (i.e. create the recordset).
      dim queryRS   ' Query recordset.
      set queryRS = ixQuery.CreateRecordSet("nonsequential")

      ' Check the query result. If it timed out or return no records, then show
      ' an appropriate message. Otherwise, show the hits.
      if (Err.description <> "") Then
         Response.Write ("<p><b>Search Recordset Error: " & Err.description & ".</b></p>" & vbCRLF)
         Exit sub
      Else
         if queryRS is Nothing Then
            Response.Write ("<p>Query returned no matches.</p>" & vbCRLF)      
         elseif (ixQuery.QueryTimedOut) then
            Response.Write ("<p><b>Error: " & timedOut_Text & ".</b></p>" & vbCRLF)
         elseif (queryRS.EOF or queryRS.BOF or queryRS.RecordCount <= 0) then
            Response.Write ("<p>No matches found.</p>" & vbCRLF)
         else
            queryRS.PageSize = rowCount
            call showHits(queryRS)
            if (Err.number <> 0) Then
               Response.Write ("<p><b>Record Display Error: " & Err.description & ".</b></p>" & vbCRLF)
            End If 
         end if
      End If
   
      ' Clean up
      queryRS.close
      set queryRS = nothing
      set ixQuery = nothing
      set util = nothing
      
   End if 
end Sub

' showHits(): Displays the query hits from the query recordset.
'
sub showHits(queryRS)
   dim recordNumber  ' record number
   dim docTitle      ' document title
   dim endRow        ' last row being displayed
   dim prevRow       ' row to display for "prev" option
   dim nextRow       ' row to display for "next" option
   dim lastRow       ' row to display for "last" option
   dim remainder     ' remainder (used to determine if last page is short)
   dim recordCount   ' numner of records returned
  
   recordCount = queryRS.RecordCount
   if firstRow > recordCount Then firstRow = 1 
		
   endRow = firstRow + RowCount-1                      ' Last row on page to show
   if endRow > recordCount Then endRow = recordCount
	
   prevRow = firstRow - RowCount                       ' Start of previous page's rows
   if PrevRow < 1 Then PrevRow = 1
	
   nextRow = endRow + 1                                ' Start of next pages rows. May be > CommentCount		
	
   remainder = recordCount mod RowCount
   if remainder = 0 Then
      lastRow = recordCount - RowCount + 1
   else
      lastRow = recordCount - remainder + 1
   End If
   if lastRow < 1 Then lastRow = 1                     ' Start of last pages rows

   ' Go to the top of the record set, then move forward to the record that
   ' corresponds to the first row.
   queryRS.MoveFirst()

   if (firstRow > 1) then
      queryRS.Move(CInt(firstRow) - 1)

   end if

   ' Show the summary info.: # of records found and range showing.
%>
<table border="0" cellspacing="0" cellpadding="0">
<tr><td colspan=2>

<table border=0 width=100%><tr>
<td nowrap><b>Found:</b> <%=queryRS.RecordCount%> &nbsp;&nbsp;&nbsp;<b>Showing:</b> <%=firstRow%> - <%=endRow%></td> 
<td align=right nowrap>
<% if firstRow <> "1" Then %>
<a href="<%=URL&"&fr=1"%>">First</a> |
<% Else %>
First |
<% End If %>
<% if firstRow <> "1" Then %>
<a href="<%=URL&"&fr="&prevRow%>">Prev</a> |
<% Else %>
Prev |
<% End If %>
<% if firstRow + RowCount <= recordCount Then %>
<a href="<%=URL&"&fr="&nextRow%>">Next</a> |
<% Else %>
Next |
<% End If %>
<% if firstRow + RowCount <= recordCount Then %>
<a href="<%=URL&"&fr="&lastRow%>">Last</a>
<% Else %>
Last
<% End If %>
</td></tr>
</table></td>

</tr>

<tr><td colspan=2>&nbsp;</td></tr>
<%
' Show the records.

recordNumber = firstRow
'Dim AllText

'AllText = true

'response.write AllText
'Response.write request("target")

do while ((not queryRS.EOF) and (recordNumber <= endRow))

	' Get the document title. If it's blank, set it to "Untitled".
	docTitle = queryRS("doctitle")

	if docTitle = "" then docTitle = "Untitled"

	' Show the record #, link to the document, URL, and characterization.
	'Response.Write "<tr>"
        
        if intRowColor= 0 then
         Response.Write "<tr bgcolor=""#E5E5E5"">"
         intRowColor = 1
         Else
         Response.Write "<tr bgcolor=""#FAFAFA"">"
         intRowColor = 0
         End If
         
	Response.Write "<td valign=top>" & recordNumber & ".</td>"
	Response.Write "<td valign=top>"
	
Response.Write "<a href='" & queryRS("vpath") & "'>" & HighLight(docTitle,request("target"),"<font size=4 color=red>","</font>") & "</a><br>"
	
'Response.Write "<b>URL: </b> [URL unfurl="true"]http://"[/URL] & HighLight(ServerName & queryRS("vpath"),request("target"),"<font size=4 'color=red>","</font>") & "<br>"
	
Response.Write HighLight(Server.HTMLEncode(queryRS("characterization")),request("target"),"<font size=4 color=red>","</font>")

Response.Write "<Br/>"
Response.Write "<Br/>"

Response.Write "</td>"
	Response.Write "</tr>"


	recordNumber = recordNumber + 1

	queryRS.MoveNext()


loop


   ' Display the navigation links.
%>
<tr><td colspan=2>&nbsp;</td></tr>

<tr><td colspan=2 align=center>
<% if firstRow <> "1" Then %>
<a href="<%=URL&"&fr=1"%>">First</a> |
<% Else %>
First |
<% End If %>
<% if firstRow <> "1" Then %>
<a href="<%=URL&"&fr="&prevRow%>">Prev</a> |
<% Else %>
Prev |
<% End If %>
<% if firstRow + RowCount <= recordCount Then %>
<a href="<%=URL&"&fr="&nextRow%>">Next</a> |
<% Else %>
Next |
<% End If %>
<% if firstRow + RowCount <= recordCount Then %>
<a href="<%=URL&"&fr="&lastRow%>">Last</a>
<% Else %>
Last
<% End If %>
</td></tr>

</table>

<% end sub %>

<%
Function Highlight(strText, strFind, strBefore, strAfter)
	Dim nPos
	Dim nLen
	Dim nLenAll
	
	nLen = Len(strFind)
	nLenAll = nLen + Len(strBefore) + Len(strAfter) + 1

	Highlight = strText

	If nLen > 0 And Len(Highlight) > 0 Then
		nPos = InStr(1, Highlight, strFind, 1)
		Do While nPos > 0
			Highlight = Left(Highlight, nPos - 1) & _
				strBefore & Mid(Highlight, nPos, nLen) & strAfter & _
				Mid(Highlight, nPos + nLen)

			nPos = InStr(nPos + nLenAll, Highlight, strFind, 1)
		Loop
	End If
End Function
%>
</body>
</html>
 
Thank you very much! -)

One question though, i get this error.
Search Recordset Error: Service is not running. .



 
Did you read the post thoroughly??

Please read the text highlighted in [red]RED[/red]

you need to start the index servicing in your IIS manager...

-DNG
 
oops, sorry, i guess i was too enthusiastic! unfortunatly, i don't have access to that. I'm wondering if a spider/database solution is the way to go?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top