You can ask as well how good a web site is when no search is available. And to answer that question we'll enable search for our Acme Hyper.Net web application.
Let’s create a search form, where a user can enter a search word. To do this, go to the “Project” menu in Visual Studio .Net and select “Add HTML page…” Name the form Search.htm.
Having an empty design view, let’s switch to the HTML view and make it look as shown below in Code-Snippet 9.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<title>Search - HNAcme</title>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 7.0">
<style>
BODY {
background-color: #01478e;
margin-top:6px;
margin-left:6px;
margin-right:0px;
margin-bottom:0px;
}
IMG {
border:0;
}
.Text
{
font-family:Verdana,Arial,sans-serif;
font-size:12px;
color : White;
}
.noborder{
border-left:0px;
border-right:0px;
border-top:0px;
border-bottom:0px;
font-family:Verdana,Arial,sans-serif;
font-size:11px;
vertical-align: baseline;
margin:0;
}
A.search{
font-family:Verdana,Arial,sans-serif;
font-size:12px;
color:White;
text-decoration:none;
}
A.search:hover {
text-decoration:underline;
}
</style>
</HEAD>
<body onload="searchform.txtSearch.focus()">
<script language="JavaScript">
function chk() {
if (searchform.txtSearch.value=="") {
alert('Please enter a search string!')
searchform.txtSearch.focus()
return(false)
}
return (true)
}
function startsearch() {
var zeit = new Date();
source = searchform.txtSearch.value;
source = ReplaceSpecialChar( source, "&", "%26");
source = ReplaceSpecialChar( source, " ", "%20");
source = ReplaceSpecialChar( source, "ä", "^auml;");
source = ReplaceSpecialChar( source, "Ä", "^Auml;");
source = ReplaceSpecialChar( source, "ö", "^ouml;");
source = ReplaceSpecialChar( source, "Ö", "^Ouml;");
source = ReplaceSpecialChar( source, "ü", "^uuml;");
source = ReplaceSpecialChar( source, "Ü", "^Uuml;");
source = ReplaceSpecialChar( source, "ß", "^szlig;");
source = ReplaceSpecialChar( source, "å", "^aring;");
source = ReplaceSpecialChar( source, "Å", "^Aring;");
source = ReplaceSpecialChar( source, "ø", "^oslash;");
source = ReplaceSpecialChar( source, "Ø", "^Oslash;");
source = ReplaceSpecialChar( source, "æ", "^aelig;");
source = ReplaceSpecialChar( source, "Æ", "^AElig;");
source = ReplaceSpecialChar( source, "+", "^plus^");
url = "searchresults.aspx?SearchText=" + source;
window.open( url, "main");
return(true);
}
function ReplaceSpecialChar( source, find, replace) {
pos = source.indexOf( find);
while (pos > -1) {
source = source.substring( 0, pos) + replace + (pos + find.length < source.length ? source.substring( pos + find.length, source.length) : "");
pos = source.indexOf( find);
}
return source;
}
</script>
<form name="searchform" onsubmit="{if (chk()) startsearch(); return false;}" ID="Form1">
<TABLE id="TableSearch" cellSpacing="0" cellPadding="0" border="0">
<TR>
<TD nowrap class="search"><input type="text" name="txtSearch" style="FONT-SIZE:11px;WIDTH:150px;FONT-FAMILY:Arial;HEIGHT:18px" maxlength="200" ID="Text1"></TD>
<TD nowrap class="search" align="left"><NOBR> <A onclick="{if (chk()) startsearch(); return false;}" href="#" class="search">Search</A></NOBR></TD>
</TR>
</TABLE>
</form>
</body>
</HTML>
Code-Snippet 9 – Search.html HTML View
The outcome of this code should be reflected in the design view, as shown in the following picture.
Figure 12 – Search.htm Design View
In this way we’ve provided the user with an entry search form, but we haven’t yet created a search output form where the results of the search can be presented. For this we’ll make use of the Search class found in the Hyper.Net.Framework.BOM namespace, and the XML capabilities of the HNFSDK to later merge the outcome with an Xslt to yield the kind of output we need.
Let’s begin by creating a new aspx web page called searchresults.aspx. This is an empty page that will not contain any web server controls. All the work will be done in the code behind instead.
Note that in the Search.htm page, a JavaScript function is in charge of collecting the information, formatting the search keywords to some ANSI standards, and later redirecting the result of the search criteria to the searchresults.aspx page in the proper frame.
Now let’s focus on the code behind of our searchresults.aspx page. Code-Snippet 10 shows how to setup the Search object to properly retrieve the result out of the database and render the result.
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hyper.Net.Framework.BOM;
using Hyper.Net.Framework.Common;
using System.Xml;
using System.Xml.Xsl;
using System.IO;
namespace HNAcme{
public class searchresults : System.Web.UI.Page{
protected System.Web.UI.HtmlControls.HtmlGenericControl resultPane;
private void Page_Load(object sender, System.EventArgs e){
string _searchText = Request.QueryString["SearchText"].Replace("^plus^","+");
Hyper.Net.Framework.BOM.Search mySearch =
new Hyper.Net.Framework.BOM.Search("", "", "","NSM", -1);
mySearch.SearchCriteria = publication.IsEffective == 1 &
publication.Audience == "Production”;
Hyper.NetField[] _searchEntry = new Hyper.NetField[6];
_searchEntry[0] = topic.TopicID;
_searchEntry[1] = topic.Heading;
_searchEntry[2] = publication.Category;
_searchEntry[3] = publication.LastModifiedBy;
_searchEntry[4] = publication.PublicationDate;
_searchEntry[5] = fts_topic.Rank;
mySearch.AddNewOutputNode("TopicHeading",
_searchEntry, "{0} {1} {2} {3} {4:d} {5}");
// comment out the following two lines if your database does not support
// full-text searches, or does not have enabled any catalog.
Hyper.NetFuzzySearch _fts = new Hyper.NetFuzzySearch();
_fts.TopicSearchText = _searchText;
mySearch.FuzzySearch = _fts;
mySearch.Execute();
string _xsltPath = Server.MapPath("SearchToHtml.xslt");
string _htmlTrans = Tools.XmlXlstTransformer(mySearch.Xml, _xsltPath);
resultPane.InnerHtml = string.Format("<font style=\"font-size: 8pt; font-
family: Verdana;\">Total number of documents found: <b>{0}</b></font><hr>,
Tools.GetTotalXmlElementsOf(mySearch.Xml,"TopicHeading")) + _htmlTrans;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e){
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent(){
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Code-Snippet 10 – searchresults.aspx.cs code behind
A thorough inspection of the code reveals that we’re calling an XSLT document we haven’t created yet. To solve this problem let’s create the following SearchToHtml.xslt file and make sure it looks as shown in Code-Snippet 11.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/TREENODES">
<TABLE cellSpacing="0" cellPadding="2" width="100%" border="1" style="font-size:
8pt; font-family: Verdana;">
<xsl:for-each select="//TopicHeading">
<TR>
<TD bgcolor="#eee8aa">
<a class="topiclink">
<xsl:attribute name="href">
<xsl:value-of select="Topic.aspx" />
<xsl:text>topic.aspx?DOC_UNID=</xsl:text>
<xsl:value-of select="@topic_TopicID" />
</xsl:attribute>
<xsl:value-of disable-output-escaping="yes" select="@topic_Heading" />
</a>
</TD>
<td bgcolor="#adff2f">Pub. Date: <xsl:value-of
select="@publication_PublicationDate" />
</td>
</TR>
<TR>
<TD bgcolor="#ffd700" colspan="2">
<b>Rank: </b>
<xsl:value-of disable-output-escaping="yes" select="@fts_topic_Rank" />
</TD>
</TR>
<TR>
<TD bgcolor="#f0ffff">
<b>Author: </b>
<xsl:value-of disable-output-escaping="yes"
select="@publication_LastModifiedBy" />
</TD>
<TD bgcolor="#f0ffff">
<b>Category: </b><br/>
<xsl:value-of disable-output-escaping="yes" select="@publication_Category" />
</TD>
</TR>
</xsl:for-each>
</TABLE>
</xsl:template>
</xsl:stylesheet>
Code-Snippet 11 – SearchToHtml.xslt file
One of the major features introduced in this version of the HNFSDK is the ability to create full-text searches, or what are also called fuzzy searches. If your database supports full text indexing and has search catalogs, you can make use of this great feature. For a complete reference of fuzzy searches please refer to the online HNFSDK help.