62,046
社区成员
发帖
与我相关
我的任务
分享
#region 搜索引擎
public DataTable Search(int CurrentPage, int Pagesize, string Key)
{
Analyzer analyzer = new StandardAnalyzer();
DataTable mytab = new DataTable();
string INDEX_STORE_PATH = Server.MapPath("seacherIndex"); //Index 为索引存储目录
string keyword = Key;
IndexSearcher mysea = new IndexSearcher(INDEX_STORE_PATH);//索引查询器
try
{
QueryParser q = new QueryParser("SearchAll", new StandardAnalyzer());
Query query = q.Parse(keyword);
QueryScorer scorer = new QueryScorer(query);
SimpleHTMLFormatter formatter = new SimpleHTMLFormatter("<font color=red>", "</font>");
Highlighter highlighter = new Highlighter(formatter, scorer);
Hits myhit = mysea.Search(query);
if (myhit != null)
{
DataRow myrow;
mytab.Columns.Add("productId", typeof(Int32));
mytab.Columns.Add("productName", typeof(string));
mytab.Columns.Add("Price", typeof(decimal));
mytab.Columns.Add("pInfo", typeof(string));
mytab.Columns.Add("pClassId", typeof(Int32));
mytab.Columns.Add("pTitle", typeof(string));
mytab.Columns.Add("pKey", typeof(string));
mytab.Columns.Add("pDescription", typeof(string));
mytab.Columns.Add("pButtom", typeof(string));
mytab.Columns.Add("pDate", typeof(DateTime));
mytab.Columns.Add("eDate", typeof(DateTime));
mytab.Columns.Add("pSourse", typeof(string));
mytab.Columns.Add("ptag", typeof(string));
mytab.Columns.Add("pSort", typeof(Int32));
mytab.Columns.Add("SearchAll", typeof(string));
mytab.Clear();
int StartIndex = CurrentPage * Pagesize - Pagesize;
int count = CurrentPage * Pagesize > myhit.Length() ? myhit.Length() : CurrentPage * Pagesize;
ViewState["count"] = myhit.Length();
for (int i = StartIndex; i < count; i++)
{
Document doc = myhit.Doc(i);
myrow = mytab.NewRow();
myrow[0] = doc.Get("productId");
myrow[1] = (highlighter.GetBestFragment(new StandardAnalyzer(), "", doc.Get("productName")));
if (myrow[1].ToString() =="")
{
myrow[1] = doc.Get("productName");
}
myrow[2] = doc.Get("Price");
myrow[3] = doc.Get("pInfo");
myrow[4] = doc.Get("pClassId");
myrow[5] = doc.Get("pTitle");
myrow[6] = doc.Get("pKey");
myrow[7] = doc.Get("pDescription");
myrow[8] = doc.Get("pButtom");
myrow[9] = doc.Get("pDate");
myrow[10] = doc.Get("eDate");
myrow[11] = doc.Get("pSourse");
myrow[12] = doc.Get("ptag");
myrow[13] = doc.Get("pSort");
myrow[14] = doc.Get("SearchAll");
mytab.Rows.Add(myrow);
myrow.AcceptChanges();
}
this.ProductDetails1.Key = Server.UrlEncode(Key);
}
mysea.Close();
}
catch
{
mysea.Close();
}
return mytab;
}
#endregion