Introduction
My company asked me to implement a search engine function to our intranet website. They don't need too fancy one, just simply allow user to search the pages. Because our website is Microsoft based, the first thing come to my mind is Index Server. I had no experience on using Index server before, so I looked for the Internet and found some articles about it. The tutorial that made me to start my fiirst test drive of using Index Server is http://www.xefteri.com/articles/show.cfm?id=2. It contained an easy-to-follow tutorial to start. As I tried to start to use the code to my the intranet website, I found that it is not very convenient to implement. So I decided to modify it to become a class.
Here are some screen shot of this project.
Details
The class contains 5 main functions. They are:
- getArrAllData(byref msg) - return result content as an array
- getFirstLink (scriptName) - return a link to return the first page. Current file name is required.
- getPreviousLink (scriptName) - return a link to return previous page. Current file name is required.
- getNextLink (scriptName) - return a link to the next page. Current file name is required.
- getLastLink (scriptName) - return a link to the last page. Current file name is required.
To use the class, you need to instantiate the class and initialize the required value.
<P><!-- #include file="class/cls_text_search.asp" -->
<%
dim msg
msg = ""</P>
<P> dim arrData
dim objTxtSearch
set objTxtSearch = New cls_text_search
objTxtSearch.pageTitle = "Page Title - "
objTxtSearch.catalog = "catalogName"
objTxtSearch.query = Request("query")
objTxtSearch.fileExts = "asp,html,pdf,doc,xls,ppt,txt,htm"
objTxtSearch.ignoreFileWithPaths = "/xxx.html"
objTxtSearch.includeFolderWithPaths = "/,/doc,/PDFs"
objTxtSearch.recordsPerPage = "5"
objTxtSearch.page = Request("page")
objTxtSearch.order = Request("order")</P>
<P> on error resume next
arrData = objTxtSearch.getArrAllData(msg)
if err.number <> 0 then
msg = err.description
end if</P>
<P>%></P>
Then you can retrieve the navigation links from the class
<P> <%
Dim firstLink, previousLink, nextLink, lastLink
firstLink = objTxtSearch.getFirstLink (Request.ServerVariables("SCRIPT_NAME"))
previousLink = objTxtSearch.getPreviousLink (Request.ServerVariables("SCRIPT_NAME"))
nextLink = objTxtSearch.getNextLink (Request.ServerVariables("SCRIPT_NAME"))
lastLink = objTxtSearch.getLastLink (Request.ServerVariables("SCRIPT_NAME"))</P>
<P> ' Navigation Link
if firstLink = "" then Response.Write "First | " else Response.Write "<a href='" & firstLink & "' >First</a> | "
if previousLink = "" then Response.Write "Previous | " else Response.Write "<a href='" & previousLink & "' >Previous</a> | "
if nextLink = "" then Response.Write "Next | " else Response.Write "<a href='" & nextLink & "' >Next</a> | "
if lastLink = "" then Response.Write "Last | " else Response.Write "<a href='" & lastLink & "' >Last</a>"
%></P>
Of course, you need to extract the search result through the array variable from Index Server.
<%
Dim strDocTitle, strFilename, strVPath, intSize, datWrite, strCharacterization, numRank
Dim numrows, rowcounter
numrows = UBound(arrData,2)
'-- now loop through the records
For rowcounter= 0 To numrows
'-- row values held in variables for ease of use
strDocTitle = arrData(0, rowcounter)
strFilename = arrData(1, rowcounter)
strVPath = arrData(2, rowcounter)
intSize = FormatNumber(arrData(3, rowcounter))
datWrite = arrData(4, rowcounter)
strCharacterization = arrData(5, rowcounter)
numRank = arrData(6, rowcounter)
If IsNull(strCharacterization) Or Trim(strCharacterization) = "" Then
strCharacterization = " "
End If
%>
<tr><td colspan="2"></td>
<td>
<a href="<%=strVPath%>"><%= objTxtSearch.getCustomTitle (strDocTitle, strFilename)%></a>
</td>
</tr>
<tr><td valign="top">
<%
Dim starslocation, NormRank, stars
starslocation = "images/rank/"
'-- show proper image for ranking
NormRank = numRank/10
If NormRank > 80 Then
stars = "rankbtn5.gif"
ElseIf NormRank > 60 Then
stars = "rankbtn4.gif"
ElseIf NormRank > 40 Then
stars = "rankbtn3.gif"
ElseIf NormRank > 20 Then
stars = "rankbtn2.gif"
Else
stars = "rankbtn1.gif"
End If
'-- Chr(37) = %
'-- write correct image and percentage ranking
%>
<img src="<%=starslocation & stars %>">
<br>
<%= NormRank & Chr(37) %>
</td>
<td> </td>
<td valign="top">
<%= strCharacterization %><br><br><i>
<%
'-- write file size or show error in case
' we have a NULL value returned
If Trim(intSize) = "" Or IsNull(intSize) Then
Response.Write("(size unknown) - ")
Else
Response.Write("size " & objTxtSearch.FileSize(intSize) & " - ")
End If
If Trim(datWrite) = "" Or IsNull(datWrite) Then
Response.Write("(time unknown)")
Else
Response.Write(objTxtSearch.myFixDate(datWrite) & " GMT")
End If
%>
</td>
</tr>
<tr><td colspan="3"> </td></tr>
<%
Next
%>
That's it! All logic is in the class, there is no need to modify it. And you can just reuse the class when your next project need to add a search engine function. I have included the full source code of this project, hope you will like it.
References
The Windows 2000 Indexing Service - http://www.xefteri.com/articles/show.cfm?id=2
Your free search Engine - Microsoft Indexing Server -http://www.codeproject.com/aspnet/IndexingServer.asp
Adding a search facility to your website - http://www.codeproject.com/asp/indexserver.asp