usingSystem;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Data.SqlClient;usingSystem.Text;usingSystem.IO;usingSystem.Collections;usingLucene.Net.Documents;usingLucene.Net.Index;usingLucene.Net.Search;usingLucene.Net.QueryParsers;usingLucene.Net.Analysis.Standard;publicpartialclass_Default : System.Web.UI.Page
{protectedvoidPage_Load(objectsender, EventArgs e)
{ }protectedvoidButton2_Click(objectsender, EventArgs e)
{
CreateIndex();
}//获得查询结果publicSqlDataReader ExecuteQuery(stringsql)
{stringconnstr=@"server=localhost\sqlexpress;uid=sa;pwd=123;database=lucenetest";
SqlConnection con=newSqlConnection(connstr);if(con.State==ConnectionState.Closed)con.Open();
SqlCommand command=newSqlCommand(sql, con);
SqlDataReader datareader=command.ExecuteReader();returndatareader;
}//建立索引publicIndexWriter CreateIndex()
{stringINDEX_STORE_PATH=Server.MapPath("index");//INDEX_STORE_PATH 为索引存储目录IndexWriter writer=null;try{
writer=newIndexWriter(INDEX_STORE_PATH,newStandardAnalyzer(),true);
SqlDataReader myred=ExecuteQuery("select wid,title,content,createdate from article");//建立索引字段while(myred.Read())
{
Document doc=newDocument();
doc.Add(newField("tablename","article", Field.Store.YES, Field.Index.UN_TOKENIZED));//存储,不索引doc.Add(newField("wid", myred["wid"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(newField("title", myred["title"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.Add(newField("indexcontent", myred["title"].ToString()+myred["content"].ToString(), Field.Store.NO, Field.Index.TOKENIZED));//不存储,索引,indexcontent实现了title和content,也就是标题和内容的索引doc.Add(newField("createdate", myred["createdate"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
writer.AddDocument(doc);
}
myred.Close();
myred.Dispose();//writer.Optimize();
writer.Close();
TextBox1.Text="建立索引成功";
}catch(Exception e)
{
TextBox1.Text=e.ToString();
}returnwriter;
}protectedvoidButton1_Click(objectsender, EventArgs e)
{stringINDEX_STORE_PATH=Server.MapPath("index");//INDEX_STORE_PATH 为索引存储目录stringkeyword=TextBox2.Text;
Hits myhit=null;
IndexSearcher mysea=newIndexSearcher(INDEX_STORE_PATH);
QueryParser q=newQueryParser("indexcontent",newStandardAnalyzer());
Query query=q.Parse(keyword);
myhit=mysea.Search(query);
Response.Write("关于:"+keyword+"搜索到"+myhit.Length()+"个结果
");if(myhit!=null)
{
DataRow myrow;
DataTable mytab=newDataTable();
mytab.Columns.Add("wid");
mytab.Columns.Add("title");
mytab.Columns.Add("createdate");
mytab.Columns.Add("tablename");
mytab.Clear();for(inti=0; i
{
Document doc=myhit.Doc(i);
myrow=mytab.NewRow();
myrow[0]=doc.Get("wid").ToString();
myrow[1]=doc.Get("title").ToString();
myrow[2]=doc.Get("createdate").ToString();
myrow[3]=doc.Get("tablename").ToString();
mytab.Rows.Add(myrow);
myrow.AcceptChanges();
}
GridView1.DataSource=mytab;
GridView1.DataBind();
}else{
Response.Write("Hits为空");
}
mysea.Close();
}
}