Thursday, September 4, 2008

ASP.NET Site Search

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
//Programmed by Renju.R
//Refer Microsoft Indexing Service
public partial class _Default : System.Web.UI.Page
{
public static DataView tempds = new DataView();
public static int resultCount = 0;
public DataRow drIndexed;
public DataTable dtPub = new DataTable();
public DataSet dsPub = new DataSet();
public DataRow dr2;
public DataTable dt2 = new DataTable();
public DataSet ds2 = new DataSet();
public DataSet ds = new DataSet();
public DataRow dr;
public DataTable dt = new DataTable();
public static StringBuilder stb = new StringBuilder();
// public static OleDbConnection oldbCon = new OleDbConnection("Provider=MSIDXS.1;Integrated Security .='';Data Source=Reader");
public static OleDbConnection oldbCon = new OleDbConnection("Provider=MSIDXS.1;");
public int count;
protected void Page_Load(object sender, EventArgs e)
{
}
public void FillResult()
{
//ds.Merge(dtTemp, false, MissingSchemaAction.Add);
//ds.Merge(ds2.Tables[0]);
//ds.Tables[0].Merge(ds2.Tables[0]);
//tempds = ds.Tables[0].Copy().DefaultView;
MergeSearch();
Searchfill();
}
protected void Button1_Click(object sender, EventArgs e)
{
string searchText = TextBox1.Text.ToString();
//OleDbCommand cmd = new OleDbCommand("select doctitle, filename, Path ,Attrib, rank, characterization from scope() where FREETEXT('" + searchText + "') and filename <> 'Default.aspx' order by rank desc", oldbCon);
OleDbCommand cmd = new OleDbCommand("select doctitle, filename, Path ,Attrib, rank, characterization from saravanan.Reader..scope() where FREETEXT('" + searchText + "') and filename <> 'Default.aspx' order by rank desc", oldbCon);
cmd.CommandType = CommandType.Text;
if (oldbCon.State == ConnectionState.Closed)
{
oldbCon.Open();
}
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(ds);
resultCount = ds.Tables[0].Rows.Count;
ds2.Tables.Add(dt2);
dt2.Columns.Add("new");
/////
//ds.Tables.Add(dt);
//dt.Columns.Add("search");
////
dsPub.Tables.Add(dtPub);
dtPub.Columns.Add("text");
foreach (DataRow drTemp in ds.Tables[0].Rows)
{
ReadDocuments((string)drTemp["path"]);
}
FillResult();
}
void Searchfill()
{
DataList1.DataSource = ds.Tables[0].DefaultView;
DataList1.DataBind();
}
public DataSet MergeSearch()
{
ds.Tables[0].Columns.Add("search");
int cnt = 0;
// ds.AcceptChanges();
//int mCount = ds2.Tables[0].Rows.Count;
foreach (DataRow drLoop in ds2.Tables[0].Rows)
{
DataRow drTest= ds.Tables[0].Rows[cnt];
drTest["search"] = drLoop["new"];
//dt.Rows.Add(drTest);
// ds.Tables[0].Rows.Add(drTest);
cnt++;
}
return ds;
}
public DataSet MergedValues(string str)
{
DataRow dr5 = ds2.Tables[0].NewRow();
dr5["new"] = (string)str;
dt2.Rows.Add(dr5);
return ds2;
}
public DataSet SearchResult(string text)
{
DataRow drResult = dsPub.Tables[0].NewRow();
drResult["text"] = (string)text;
CreateExcerpt((string)text, TextBox1.Text.ToString(),ds);
dtPub.Rows.Add(drResult);
return dsPub;
}
private string parseHtml(string html)
{
string temp = Regex.Replace(html, "<[^>]*>", "");
return temp.Replace(" ", " ");
}
public string CreateExcerpt(string source, string keyword,DataSet ds)
{
count = 0;
string excerpt = string.Empty;
int charsBeforeAndAfter = 100;
int index = source.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase);
if (index >= 0)
{
int excerptStartIndex = 0;
int excerptEndIndex = source.Length - 1;
if (index > (charsBeforeAndAfter - 1))
excerptStartIndex = index - charsBeforeAndAfter;
if ((index + keyword.Length + charsBeforeAndAfter) < (source.Length - 1))
excerptEndIndex = index + keyword.Length + charsBeforeAndAfter;
excerpt = source.Substring(excerptStartIndex, excerptEndIndex - excerptStartIndex + 1);
index = excerpt.IndexOf(keyword, StringComparison.CurrentCultureIgnoreCase);
excerpt = excerpt.Insert(index + keyword.Length, "");
excerpt = excerpt.Insert(index, "");
excerpt = string.Format("...{0}...", excerpt);
foreach (DataRow dr in ds.Tables[0].Rows)
{
if(count ==0)
MergedValues(excerpt);
count++;
}
}

//stb.Append(excerpt.ToString());
//stb.Append("
");
//stb.Append("
");
return excerpt;
}
public void ReadDocuments(string docPath)
{
FileStream fs = File.OpenRead(docPath);
string dumptext;
using(StreamReader sr = new StreamReader(docPath, System.Text.Encoding.Default))
{
dumptext = parseHtml(sr.ReadToEnd());
}
SearchResult((string)dumptext);


}
}

0 comments: