Syntax highlighted source code generated using the .NET Documentation Tool. Note that the Indexing Service Companion also
contains sample code for ASP (VBScript) and VB.NET.
Code Behind Page Page /SearchIndexServer.aspx.cs (C#)
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 System.Data.OleDb;
using System.Data.SqlClient;
namespace BRETTB
{
///
<summary>
/// This is a very basic web
form that illustrates the use of the
/// Indexing Service Companion
and Index Server (or Indexing Service)
/// to return search results
for use with the .NET Framework.
/// The results are displayed
in a standard DataGrid, so it would
/// be possible to extend
this sample code to use paging or sorting
/// of the results as
required.
/// In this web form the
Indexing Service is queried using the Index Server
/// provider for
OLE-DB.
/// More information about
the Indexing Service Companion:
///
http://www.winnershtriangle.com/w/Products.IndexServerCompanion.asp
///
</summary>
public class
SearchIndexServer : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.TextBox TextBoxQuery;
protected
System.Web.UI.WebControls.ImageButton ImageButton1;
protected
System.Web.UI.WebControls.Panel PanelSearchResults;
protected
System.Web.UI.WebControls.Label LabelNumberOfResults;
protected
System.Web.UI.WebControls.Panel PanelSearchForm;
protected
System.Web.UI.WebControls.DataGrid DataGridSearchResults;
private
void Page_Load(object
sender, System.EventArgs e)
{
PanelSearchForm.Visible
= true;
PanelSearchResults.Visible
= true;
}
#region Web Form Designer
generated code
override
protected void OnInit(EventArgs
e)
{
//
//
CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
<summary>
///
Required method for Designer support - do not modify
///
the contents of this method with the code editor.
///
</summary>
private
void InitializeComponent()
{
this.ImageButton1.Click
+= new
System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
this.Load
+= new System.EventHandler(this.Page_Load);
}
#endregion
private
void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
PanelSearchResults.Visible
= true;
int NumberOfSearchResults = 0;
string QueryText = TextBoxQuery.Text;
//SitePath
and ServerBaseRef are required if the search results
//are
going to contain content not from the Indexing Service Companion
string SitePath = "c:\\inetput\\wwwroot\\brettbsite\\";
string ServerBaseRef = "http://www.brettb.com/";
//Index
Server Catalog name
string CatalogName = "MYCATALOG";
//Construct
the Indexing Service query
string SQL = "SELECT doctitle, FileName, VPath, Path, Write,
Size, Rank ";
SQL
+= "FROM " + CatalogName + "..SCOPE() ";
SQL
+= "WHERE CONTAINS(Contents, '\"" + QueryText + "\"') ";
SQL
+= "AND Path Like '%SampleContent%' ";
SQL
+= "ORDER BY Rank DESC";
OleDbConnection
IndexServerConnection = new
OleDbConnection("Provider=msidxs;");
OleDbCommand
DataCommand = new OleDbCommand(SQL, IndexServerConnection);
OleDbDataAdapter
IndexServerDataAdapter = new OleDbDataAdapter();
IndexServerDataAdapter.SelectCommand
= DataCommand;
DataSet
SearchResults = new DataSet();
IndexServerDataAdapter.Fill(SearchResults);
NumberOfSearchResults
= SearchResults.Tables[0].Rows.Count;
//Create
a new table in which the search results will be populated
//Although
you can use the 'SearchResults' results DataSet directly,
//creating
a new table allows you to filter results, construct custom
//rankings
and maybe integrate search results with those from other
//sources
(e.g. a standard database search)
DataTable
SearchResultsTable = new DataTable("SearchResults");
SearchResultsTable.Columns.Add(new DataColumn("DocumentRank", typeof(String)));
SearchResultsTable.Columns.Add(new DataColumn("DocumentURL", typeof(String)));
SearchResultsTable.Columns.Add(new DataColumn("DocumentTitle", typeof(String)));
//Iterate
through Indexing Service search results
foreach (DataRow SearchResultDataRow in
SearchResults.Tables[0].Rows)
{
string DocumentURL = "";
string DocumentTitle = "";
string doctitle =
SearchResultDataRow["doctitle"].ToString();
//This
document is from Indexing Service Companion generated content
if (doctitle.IndexOf("ISC_URL") != -1)
{
string[]
DocumentInformation = doctitle.Split(Convert.ToChar("\t"));
DocumentURL
= DocumentInformation[0];
DocumentTitle
= DocumentInformation[1];
DocumentURL
= DocumentURL.Replace("ISC_URL=", "");
}
else {
//This
is other file content from the Indexing Service Companion
if
(SearchResultDataRow["FileName"].ToString().IndexOf("o_") !=
-1)
{
Response.Write("original=" +
DocumentURL + "<br>");
DocumentURL
= this.CreateURLFromFileName(SearchResultDataRow["FileName"].ToString());
Response.Write("modified=" +
DocumentURL + "<br>");
}
//This
document is from other content
else
{
DocumentURL
= SearchResultDataRow["Path"].ToString();
DocumentURL
= DocumentURL.Replace(SitePath, ServerBaseRef);
}
DocumentTitle
= doctitle;
}
//Put
in a makeshift title if the document does not have a title
//(alternatively
you could use the document url)
if (DocumentTitle == "")
{
DocumentTitle
= "untitled document";
}
//Add
a new row to the search results table
DataRow
SearchResultsRow = SearchResultsTable.NewRow();
SearchResultsRow["DocumentRank"]
= SearchResultDataRow["Rank"].ToString();
SearchResultsRow["DocumentURL"]
= DocumentURL;
SearchResultsRow["DocumentTitle"]
= DocumentTitle;
//Add
the row for this particular document to the search results table
SearchResultsTable.Rows.Add(SearchResultsRow);
}
//Hide
the search results DataGrid if no matching documents were found
if (NumberOfSearchResults == 0)
{
DataGridSearchResults.Visible
= false;
}
//Associate
the search results table with the DataGrid
DataGridSearchResults.DataSource
= SearchResultsTable;
//Bind
the search results table to the DataGrid
DataGridSearchResults.DataBind();
//Show
how many search results were found
LabelNumberOfResults.Text
= NumberOfSearchResults + " document(s) were found matching the query '" +
QueryText + "'";
}
///
<summary>
///Non-HTML
files like Adobe Acrobat PDF files and Word
///documents
are stored with their original URLs partially
///encoded
in their filenames. This function will return the
///original
URL of the file.
///The
encoding done by the Indexing Service Companion removes
///characters
that cannot be present in Windows filenames
///(these
are: \/:*?"<>|)
///
</summary>
///
<param name="FileName"></param>
///
<returns></returns>
private
string CreateURLFromFileName(string FileName)
{
try
{
//Remove
o_ prefix from URL
FileName
= FileName.Substring(2, FileName.Length - 2);
//Remove
other encoded characters
FileName
= FileName.Replace("^f", "\\");
FileName
= FileName.Replace("^b", "/");
FileName
= FileName.Replace("^c", ":");
FileName
= FileName.Replace("^s", "*");
FileName
= FileName.Replace("^q", "?");
FileName
= FileName.Replace("^d", "\"");
FileName
= FileName.Replace("^l", "<");
FileName
= FileName.Replace("^g", ">");
FileName
= FileName.Replace("^p", "|");
FileName
= FileName.Replace("^f", "\\");
FileName
= FileName.Replace("^f", "\\");
FileName
= FileName.Replace("^f", "\\");
FileName
= FileName.Replace("^f", "\\");
}
catch
{
}
return FileName;
}
}
}