How to Customize Search Results for Web Browsers Using LotusScript

Mindwatering Incorporated

Author: Tripp W Black

Created: 05/31/2001 at 11:46 AM

 

Category:
Notes Developer Tips
Agents, LotusScript

Title: How to Customize Search Results for Web Browsers Using LotusScript
Product Area: Domino Server
Product: Domino Server 4.6x
Topic: Internet \\ Web Applications \\ Application Design
Number: 163599
Date: 08/21/2000



Problem:

Full text searches performed by both Web and Notes clients, return search results in one of three sort orders:


Relevance
Modified date (newest first)
Modified date (oldest first)

While it is possible to design a search results form ($$SearchTemplateDefault) to customize the appearance of the results for web clients, the sort order is still limited to one of the 3 options above. Also a $$SearchTemplateDefault form will not display the number of documents found by the search as the default search results page does.

Solution:

The script below gives an example of how this might be done using LotusScript. This agent was written to run in the WebQuerySave event of a custom designed search form. This search form has two keyword fields. The first field named "Criteria" is the value we are looking to find, and the second field named "SortOrder" determines how the search results will be returned. When selecting the Submit button on the form, the agent below does the following:

1. The agent gets a collection of documents based on the value of "Criteria". In this example, a field level query is being done, checking the field named "Form" for the value specified in "Criteria". This is just an example, the field you query could be anything you choose.

2. Given the value of "SortOrder", the agent runs one of three procedures: "authorsort", "datesort" or "formsort". AuthorSort and DateSort walk through the document collection and put relevant field information together as a string within an array that will be sorted. For example, if "authorsort" is executed, a string is created by concatenating the value for doc.Authors, doc.Created and doc.universalID for each document in the collection, where the values for each document are a different element in the array. In this case, we want our results sorted by Author, so doc.Authors is the first part of the string. For datesort, doc.created is the first part of the string.

3. Once the array has been created, the bub_sort Sub is called. This Sub sorts the values in the array (see document # 155719 in the Related Documents section below for more information on Bubble Sort).

4. After the array is sorted, the agent uses LotusScript Print statements to create the HTML page that is the search results we see in the a web client. The count property of the document collection allows us to display the number of documents that were found.

5. The For Loop then processes each element in the array, parsing the values and assigning them to the variables: Auth$ (author), CreatedDate2$, and UNID$.

6. Once values from the current doc in the collection have been assigned to variables, LotusScript Print statements are used to generate the HTML for a particular table row. Each row in the table uses the values from the same array element, that is, the same Notes document.

7. A clickable link is created in the first column of the table using the UNID$ and the <A HREF> tag. In this example, the document author's name is printed as clickable. Clicking on the link opens the associated document just like clicking on a link in the first column of any web generated view.

8. The For Loop continues until there are no more documents in the collection.

9. A link to return to the search form is printed at the bottom of the page and that's it.

This is just an example and provides a potential answer for how to do "Dynamic Views" over the web, and how to define your own sort order for search results.

'SearchDB:

Option Public

Dim db As notesdatabase
Dim coll As notesdocumentcollection
Dim doc As notesdocument
Dim foundDoc As notesdocument
Dim searchFormula As String
Dim url As String
Dim Authname As NotesName

Sub Initialize
Dim session As New notessession
Dim datetime As New notesdatetime("01/01/90")
Dim myarray1() As Variant
Set db=session.currentdatabase
Set doc=session.documentcontext

'// This agent is searching by Form but this could be any field you define in searchFormula.
'// The user chooses the value to search for from a custom designed search form, in this example doc.criteria(0)
'// The field "criteria" is a keyword field on the search form.
searchFormula="Form=""" + doc.criteria(0) + """"
Set coll=db.search(searchFormula,dateTime,0)

If coll.count=0 Then
Print "<h1>No Documents Found</h1><p>"
Print "<b><a href=/"+db.filename+"/dynwebsearch?OpenForm>Return to Search Form</a></b>"
Exit Sub
End If

Redim myarray1(coll.count-1)
a%=0
Set founddoc=coll.getfirstdocument
Set authname=session.createname(founddoc.authors(0))

If doc.sortby(0)="Author" Then
Goto authorsort
Elseif doc.sortby(0)="Creation Date" Then
Goto datesort
Else
Goto formsort
End If

authorsort:
'// Assembles data from founddoc for sorting by bub_sort. Format Date as yyyy/mm/dd hh:mm for proper sorting.
myarray1(a%)=Authname.common+"DATE="+Cstr(Format(founddoc.created,"yyyy/mm/dd hh:mm"))+"UNID="+founddoc.universalID
a%=a%+1
Set founddoc=coll.getnextdocument(founddoc)
While Not (founddoc Is Nothing)
Set AuthName=session.createname(founddoc.authors(0))
myarray1(a%)=AuthName.common+"DATE="+Cstr(Format(founddoc.created,"yyyy/mm/dd hh:mm"))+"UNID="+founddoc.universalID
a%=a%+1
Set founddoc=coll.getnextdocument(founddoc)
Wend
Call bub_sort(myarray1)

'//Print HTML Header
Print "<BODY BGCOLOR=#FFFFFF>"
Print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% ><TR><TD><B>"
Print "<FONT SIZE=+3>Search Results &nbsp; <img src=/"+db.filename+"?OpenIcon><HR></FONT></B></TD></TR></TABLE>"
Print "<H3><I>" + Cstr(coll.count)+ " documents found </I> - Sorted by Author</H3></I><P>"
Print "<TABLE BORDER=0 WIDTH=100% CELLSPACING=0 CELLPADDING=0>"

For x=0 To coll.count-1
'// Parse out the Author, UNID, and Date from myarray(x)
StartUNID%=Instr(myarray1(x),"UNID=")
StartDATE%=Instr(myarray1(x),"DATE=")
Auth$=Left(myarray1(x),StartDATE%-1)
UNID$=Right(myarray1(x),32) '// UNID is always 32 long
CreatedDate$=Mid$(myarray1(x), StartDATE%+5, 16) '// 16 because of Format(date,"yyyy/mm/dd hh:mm")
'// Now re-format CreateDate$ as mm-dd-yy
yr$=Left$(CreatedDate$,4)
mnth$=Mid$(CreatedDate$,6,2)
dy$=Mid$(CreatedDate$,9,2)
CreatedDate2$=mnth$+"-"+dy$+"-"+yr$
Print "<TR>"
'// Create a clickable link. The link is /Database/View/DocumentUNID?OpenDocument
Print "<TD><a href=/"+db.filename+"/All/"+unid$+"?OpenDocument>"+Auth$+"</a></TD>"
Print "<TD>"+CreatedDate2$+"</TD></TR>"
Next

Print "</TABLE>"
Print "<p><HR>"
Print "<p><b><a href=/"+db.filename+"/"+doc.form(0)+"?OpenForm>"
Print "<IMG BORDER=0 WIDTH=21 HEIGHT=21 SRC=/icons/actn101.gif> Search again</a></b>"
Exit Sub

datesort:
'// Assembles data from founddoc for sorting by bub_sort
'// Useful as default FTI search sorts by Modified date
myarray1(a%)=Cstr(Format(founddoc.created,"yyyy/mm/dd hh:mm"))+"AUTH="+authname.common+"UNID="+founddoc.universalID
a%=a%+1
Set founddoc=coll.getnextdocument(founddoc)
While Not (founddoc Is Nothing)
Set AuthName=session.createname(founddoc.authors(0))
myarray1(a%)=Cstr(Format(founddoc.created,"yyyy/mm/dd hh:mm"))+"AUTH="+AuthName.common+"UNID="+founddoc.universalID
a%=a%+1
Set founddoc=coll.getnextdocument(founddoc)
Wend
Call bub_sort(myarray1)

'//Print HTML Header
Print "<BODY BGCOLOR=#FFFFFF>"
Print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% ><TR><TD><B>"
Print "<FONT SIZE=+3>Search Results &nbsp; <img src=/"+db.filename+"?OpenIcon><HR></FONT></B></TD></TR></TABLE>"
Print "<H3><I>" + Cstr(coll.count)+ " documents found </I> - Sorted by Creation Date</H3></I><P>"
Print "<TABLE BORDER=0 WIDTH=100% CELLSPACING=0 CELLPADDING=0>"

For x=0 To coll.count-1
'// Parse out the Author, UNID, and Date from myarray(x)
StartAuth%=Instr(myarray1(x),"AUTH=")
CreatedDate$=Left(myarray1(x),16)
StartUNID%=Instr(myarray1(x),"UNID=")
UNID$=Mid(myarray1(x),StartUNID%+5,32)
Auth$=Mid$(myarray1(x),StartAuth%+5,Len(myarray1(x))-Len(CreatedDate$)-42) '// 42 = 32 for UNID + 10 for ("AUTH=" & UNID=")
'// Now re-format CreateDate$ as mm-dd-yy
yr$=Left$(CreatedDate$,4)
mnth$=Mid$(CreatedDate$,6,2)
dy$=Mid$(CreatedDate$,9,2)
CreatedDate2$=mnth$+"-"+dy$+"-"+yr$

Print "<TR>"
Print "<TD><a href=/"db.filename+"/All/"+unid$+"?OpenDocument>"+CreatedDate2$+"</a></TD>"
Print "<TD>"+Auth$+"</TD></TR>"
Next

Print "</TABLE>"
Print"<p><HR>"
Print "<p><b><a href=/"+db.filename+"/"+doc.form(0)+"?OpenForm>"
Print "<IMG BORDER=0 WIDTH=21 HEIGHT=21 SRC=/icons/actn101.gif> Search again</a></b>"
Exit Sub

formsort:
'//No need to sort as all docs have same sort value (Form)
'// Db.Search returns docs in Date Created order
Print "<BODY BGCOLOR=#FFFFFF>"
Print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 WIDTH=100% ><TR><TD><B>"
Print "<FONT SIZE=+3>Search Results &nbsp; <img src=/"+db.filename+"?OpenIcon><HR></FONT></B></TD></TR></TABLE>"
Print "<H3><I>" + Cstr(coll.count)+ " documents found </I> - Sorted by Form</H3></I><P>"
Print "<TABLE BORDER=0 WIDTH=100% CELLSPACING=0 CELLPADDING=0>"

Set foundDoc=coll.getfirstdocument
URL="/"+db.filename+"/All/"+founddoc.universalID+"?OpenDocument"
Set AuthName=session.createname(founddoc.authors(0))

Print "<TR>"
Print "<TD><a href="+URL+">"+founddoc.form(0)+"</a></TD>"
Print "<TD>"+Cstr(Format(founddoc.created,"mm-dd-yyyy hh:mm"))+"</TD>"
Print "<TD>"+AuthName.common+"</TD></TR>"

Set foundDoc=coll.getnextdocument(foundDoc)
While Not (foundDoc Is Nothing)
URL="/"+db.filename+"/All/"+founddoc.universalID+"?OpenDocument"
Set AuthName=session.createname(founddoc.authors(0))
Print "<TR>"
Print "<TD><a href="+URL+">"+founddoc.form(0)+"</a></TD>"
Print "<TD>"+Cstr(Format(founddoc.created,"mm-dd-yyyy hh:mm"))+"</TD>"
Print "<TD>"+AuthName.common+"</TD></TR>"
Set foundDoc=coll.getnextdocument(foundDoc)
Wend
Print "</TABLE>"
Print "<p><HR>"
Print "<p><b><a href=/"+db.filename+"/"+doc.form(0)+"?OpenForm>"
Print "<IMG BORDER=0 WIDTH=21 HEIGHT=21 SRC=/icons/actn101.gif> Search again</a></b>"
Exit Sub
End Sub

Sub bub_sort(myarray1 As Variant)
'returns a sorted array based on a complexity of O(n^2) using a bubble sort
If Not Isarray(myarray1) Then
' Print "bub_sort: not receiving array"
Exit Sub
End If

Dim top, bot, cur, cur2 As Integer
top=Ubound (myarray1)
bot=Lbound (myarray1)

If top=bot Then
Print "bub_sort: only one element"
Exit Sub
End If

Dim tmp_element As Variant

For cur=bot To top
cur2=cur
Do While cur2 > bot 'bubble up
If (myarray1(cur2) > myarray1(cur2-1)) Then
Exit Do
Else
'swap
tmp_element=myarray1(cur2)
myarray1(cur2)=myarray1(cur2-1)
myarray1(cur2-1)=tmp_element
End If
cur2=cur2-1
Loop
Next

End Sub


Supporting Information:

Example of LotusScript Query Syntax:
(not web URL)
searchqry = |Form="Appointment" & PUNID="| & pDoc.UniversalID & |"|



Lotus Related Documents:

How to Perform a Web Search and Show the Results Using LotusScript
Document #: 163403

How to Sort Values in Field (Sort Field) in LotusScript with Bubble Sort Algorithm
Document #: 155719

previous page