[转]用Lucene.net对数据库建立索引及搜索
用Lucene.net对数据库建立索引及搜索。
简要说一下过程,首先是从数据库里取出内容,在磁盘上建立了索引文件。之后每次进行数据检索时从索引文件中检索数据。
字段名称 | 字段类型 | 字段含义 |
id | varchar | 编号 |
title | vachar | 标题 |
content | text | 内容 |
aspx文件:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestLLLL._Default" %> <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> < html xmlns="http://www.w3.org/1999/xhtml"> < head > < title >使用Lucene.net建立简单的数据库搜索程序</ title > < meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR"> < meta content="C#" name="CODE_LANGUAGE"> < meta content="JavaScript" name="vs_defaultClientScript"> < meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema"> </ head > < body ms_positioning="GridLayout"> < form id="Form1" method="post" runat="server"> < table width="100%" border="0"> < tr > < td > < asp:TextBox ID="TextBox1" runat="server"></ asp:TextBox > < asp:Button ID="Search" runat="server" Text="搜索" onclick="Search_Click"></ asp:Button > </ td > </ tr > </ table > < table width="100%" border="0"> < tr > < td > < asp:DataGrid ID="SearGrid" runat="server" AutoGenerateColumns="False"> < Columns > < asp:TemplateColumn > < HeaderTemplate > 搜索结果: </ HeaderTemplate > < ItemTemplate > < table width="100%" border="0"> < tr > < td > id:<%# DataBinder.Eval(Container.DataItem,"id") %> </ td > </ tr > < tr > < td > 标题: <%# DataBinder.Eval(Container.DataItem,"title") %> </ td > </ tr > < tr > < td > 内容: <%# DataBinder.Eval(Container.DataItem,"content") %> </ td > </ tr > < tr > < td > </ td > </ tr > </ table > </ ItemTemplate > </ asp:TemplateColumn > </ Columns > </ asp:DataGrid > </ td > </ tr > </ table > </ form > </ body > </ html > |
cs源文件
namespace TestLLLL { public partial class _Default : System.Web.UI.Page { string connstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath( "~/db1.mdb" ); protected void Page_Load( object sender, EventArgs e) { //OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/check.mdb")); if (!IsPostBack) { //打开数据库表 OleDbDataReader myred = OpenTable(); //建立索引 IndexWriter writer = CreateIndex(myred); } } public OleDbDataReader OpenTable() { OleDbConnection mycon = new OleDbConnection(connstr); mycon.Open(); OleDbCommand mycom = new OleDbCommand( "select id,title,content from userblog order by id" , mycon); return mycom.ExecuteReader(); } public IndexWriter CreateIndex(OleDbDataReader myred) { IndexWriter writer = new IndexWriter( @"c:/index/" , new ChineseAnalyzer(), true ); try { //建立索引字段 while (myred.Read()) { Document doc = new Document(); doc.Add(Field.Keyword( "id" , myred[ "id" ].ToString())); doc.Add(Field.Text( "title" , myred[ "title" ].ToString())); doc.Add(Field.Text( "content" , myred[ "content" ].ToString())); writer.AddDocument(doc); } writer.Optimize(); writer.Close(); } catch (Exception e) { Response.Write(e); } return writer; } public Hits seacher( string queryKey, string queryContent) { Hits hits = null ; try { IndexSearcher mysea = new IndexSearcher( "c:/index/" ); Query query = QueryParser.Parse(queryContent, queryKey, new ChineseAnalyzer()); hits = mysea.Search(query); } catch (Exception e) { Response.Write(e); } return hits; } protected void Search_Click( object sender, EventArgs e) { DataRow myrow; DataTable mytab = new DataTable(); mytab.Columns.Add( "id" ); mytab.Columns.Add( "title" ); mytab.Columns.Add( "content" ); mytab.Clear(); Hits myhit = seacher( "title" , TextBox1.Text); Document doc; if (myhit != null ) { for ( int i = 0; i < myhit.Length(); i++) { doc = myhit.Doc(i); myrow = mytab.NewRow(); myrow[0] = doc.Get( "id" ).ToString(); myrow[1] = doc.Get( "title" ).ToString(); myrow[2] = doc.Get( "content" ).ToString(); mytab.Rows.Add(myrow); myrow.AcceptChanges(); } } this .SearGrid.DataSource = mytab; this .SearGrid.DataBind(); } } } |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步