lucene.net mysql_lucene与sql server数据库实现索引的简单实例(vs.net2008) | 学步园

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();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值