Web-based, fully customizable search

This agent is called in a browser and returns all documents that match the search critera passed in the URL. I

developed this in order to get a Web-based search function where the sort order is by date (without getting messed up with "relevance", and where I have full control of the output.)

Overview:
Call the agent like this:
http://www.mydomain.com/database.nsf/SearchAgent?OpenAgent&query=Ray,Ozzie

This searches the database for all documents with both the words "Ray" and "Ozzie".

The agent is doing a Search on the view SearchStories, which returns a document collection. The view is very simple, the first column is the date of the document (in this code it is called IssueDate) and the second one is the headline/title of the document. This allows you to search only a subset of documents in the database, if you don't want to allow certain system documents to show up in the search. This view is also used for the HREF link in the HTML code.

The agent loops through the collection, creating the HTML for later display on the result page and putting the HTML in an array. The first part of the HTML code contains a HTML comment, and this is what decides the sort order. By using , it will sort correctly on the date.

A QuickSort function is used to sort the array, and finally the result is displayed on the Web using print statements.

Note: I did not write the QuickSort function or the Replace function. Unfortunley I lost the information about the authors, but they deserve the credit for that code.

Code: Option Public
Option Base 1

Sub Initialize
 Const MaxHits = 250   ' Max number of hits
 Dim session As New NotesSession
 Dim db As NotesDatabase
 Dim collection As NotesDocumentCollection
 Dim view As NotesView
 Dim doc As NotesDocument
 Dim searchstring As String
 Dim result() As String 
 Dim i As Integer
 Dim j As Integer
	
 Set session = New NotesSession
 Set doc2 = session.DocumentContext
 URLstring = doc2.Request_Content(0)
 URLlength = Len(URLstring)
 ParamPos = Instr(URLstring, "query=") +6
 SearchString = Ucase(Mid(URLstring, ParamPos, URLlength-ParamPos+1))
 SearchString=strReplace(SearchString,"+","&")
	
 Set db=session.CurrentDatabase
 Set view = db.GetView( "SearchStories" )
 cnt = view.FTSearch(searchstring,2500)
	
 Print "<HTML><HEAD><TITLE>Search Results (" & cnt & " documents found)</TITLE></HEAD>"	
 Print "<BODY>"

' Here goes your HTML to display BEFORE the search results (footer)

 If cnt > maxHits Then
  Print |<FONT COLOR=#FF0000 SIZE=-2 FACE=Verdana,Arial,Helvetica STYLE="font-size: 10pt;
font-weight: normal; font-family: Arial, Helvetica; text-decoration: none;"><B>| & Cnt & |
articles found. Only the | & MaxHits & | first will be displayed.<BR>Change the search criteria
to reduce the number of hits.</B></FONT><BR>| Else Print | <TABLE WIDTH=100% CELLPADDING=0 CELLSPACING=0 BORDER=0 BGCOLOR=#FDB505><TR><TD>
<FONT SIZE=+1 FACE="Arial, Helvetica" STYLE="font-size: 11pt; font-weight: bold; font-family:
Arial, Helvetica; text-decoration: none;" COLOR=#DF4509><B>| Print "<IMG SRC=/worldupdateonline.nsf/blank.gif HEIGHT=1 WIDTH=3>" + Cnt + " articles found
containing " + strReplace(SearchString,"&",", ") Print "</B></FONT></TD></TR></TABLE>" End If Set doc = view.GetFirstDocument ' Fix the search string, replace space and ampersand with comma searchstring = strReplace(searchstring," ","") searchstring = strReplace(searchstring,"&",",") Redim result(cnt) For i = 1 To cnt ' This database uses two different forms, Story and Announcement. ' Depending on the form, we need to display different fields. ' The HTML below need to be adjusted for your documents and the fields they contain. If doc.form(0)="Story" Then result(i) = |<!--| & Year(doc.issuedate(0)) & "/" & Format$(Month(doc.issuedate(0)),"00")
& "/" & Format$(Day(doc.issuedate(0)),"00") & |--><TABLE WIDTH=100% CELLPADDING=3 CELLSPACING=0
BORDER=0><TR><TD WIDTH=60%><FONT SIZE=-2 FACE=Arial,Helvetica STYLE="font-size: 9pt; font-weight: normal;
font-family: Arial, Helvetica; text-decoration: none;"><B><A HREF=/worldupdateonline.nsf/0/|
& doc.UniversalID & "?OpenDocument&Highlight=" & searchstring & ">" & doc.Title(0) & |</A></B>
</FONT></TD><TD WIDTH=10% ALIGN=right><FONT SIZE=-2 FACE=Arial,Helvetica STYLE="font-size: 9pt;
font-weight: normal; font-family: Arial, Helvetica; text-decoration: none;">|
& Format$(Month
(doc.IssueDate(0)),"00") & "/" & Format$(Day(doc.IssueDate(0)),"00") & "/" & Format$(Year
(doc.IssueDate(0)),"0000") & |</FONT></TD><TD WIDTH=30% NOWRAP><FONT SIZE=-2 FACE=Arial,Helvetica
STYLE="font-size: 9pt; font-weight: normal; font-family: Arial, Helvetica; text-decoration: none;">|
& doc.Category(0) & "</FONT></TD></TR></TABLE>" Else result(i) = |<!--| & Year(doc.created) & "/" & Month(doc.created) & "/" & Day(doc.created)
& |--><TABLE WIDTH=100% CELLPADDING=3 CELLSPACING=0 BORDER=0><TR><TD WIDTH=60%><FONT SIZE=-2
FACE=Arial,Helvetica STYLE="font-size: 9pt; font-weight: normal; font-family: Arial, Helvetica;
text-decoration: none;"><B><A HREF=/worldupdateonline.nsf/0/| & doc.UniversalID & "?OpenDocument&
Highlight=" & searchstring & ">" & doc.Title(0) & |</A></B></FONT></TD><TD WIDTH=10% ALIGN=right>
<FONT SIZE=-2 FACE=Arial,Helvetica STYLE="font-size: 9pt; font-weight: normal; font-family: Arial,
Helvetica; text-decoration: none;">| & Format$(Month(doc.created),"00") & "/" & Format$
(Day(doc.created),"00") & "/" & Format$(Year(doc.created),"0000") & |</FONT></TD><TD WIDTH=30% NOWRAP>
<FONT SIZE=-2 FACE=Arial,Helvetica
STYLE="font-size: 9pt; font-weight: normal; font-family: Arial, Helvetica; text-decoration: none;">
Announcement</FONT></TD></TR></TABLE>| End If Set doc = view.GetNextDocument(doc) Next ' Sort the HTML strings in the array. Call quicksort(result, 1, cnt) j = i If cnt > MaxHits Then cnt = MaxHits End If ' Print out the sorted array of HTML strings For i = cnt To 1 Step -1 Print result(i) Next Print "" ' If more than a certain number of hits are returned, display a message. If cnt = MaxHits Then Print |<CENTER><FONT COLOR=#FF0000 SIZE=-2 FACE=Arial,Helvetica STYLE="font-size: 9pt; font-weight:
normal; font-family: Arial, Helvetica; text-decoration: none;">More than | & MaxHits & | documents
returned. Try to change the search criteria to reduce the number of hits.</FONT></CENTER>| End If ' Here goes your HTML to display AFTER the search results (footer) Print "</BODY></HTML>" End Sub Function strReplace(s As String, mita As String, milla As String) As String While Instr(s, mita) > 0 s = Left$(s, Instr(s, mita) - 1) + milla + Right$(s, Len(s) - Instr(s, mita) - Len(mita) + 1) Wend strReplace = s End Function Sub QuickSort(SortList As Variant, Byval First As Integer, Byval Last As Integer) Dim Low As Integer, High As Integer 'Use Integer for lists up to 32,767 entries. Dim Temp As Variant, TestElement As Variant 'Variant can handle any type of list. Low = First High = Last TestElement = SortList((First + Last) / 2) 'Select an element from the middle. Do Do While SortList(Low) < TestElement 'Find lowest element that is >= TestElement. Low = Low + 1 Loop Do While SortList(High) > TestElement 'Find highest element that is <= TestElement. High = High - 1 Loop If (Low <= High) Then 'If not done, Temp = SortList(Low) ' Swap the elements. SortList(Low) = SortList(High) SortList(High) = Temp Low = Low + 1 High = High - 1 End If Loop While (Low <= High) If (First < High) Then Quicksort SortList, First, High If (Low < Last) Then Quicksort SortList, Low, Last End Sub

This was first published in January 2001

Dig deeper on Domino Resources - Part 7

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchWinIT

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchEnterpriseLinux

SearchVirtualDataCentre.co.uk

Close