Lucene.net全文搜索示例Demo

使用组建及工具 Lucene.net2.0,中文分词器版本1.3.2.2,数据库SQL Sever 2000
1.程序流程:
?????? (1)打开数据库;
?????? (2)对每个字段建立索引;
?????? (3)根据索引进行全文搜索;
2.Article表结构
?????? [字段名称]???????????? [字段类型]???????????? [备注]
????????id????????????????????int???????????????????? 编号
????????title???????????????? Varchar(100)????????????标题
????????content?????????????? Text????????????????????内容
3.Default.aspx文件
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

http://www.w3.org/1999/xhtml" >

????Lucene.net Test


????

????

????????
????????
????????

????

????



4.Default.aspx.cs文件
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 Lucene.Net;
using Lucene.Net.Index;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using Lucene.Net.Search;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Analysis.Cn;
public partial class _Default : System.Web.UI.Page
{
????public string connstr = "server=.;database=xkCms;uid=sa;pwd=sa";
????public SqlConnection mycon;
????protected void Page_Load(object sender, EventArgs e)
????{
????????if (!Page.IsPostBack) {
????????????SqlDataReader myred = OpenTable();
????????????IndexWriter writer = CreateIndex(myred);
????????}
????}
????public SqlDataReader OpenTable() {
????????mycon = new SqlConnection(connstr);
????????mycon.Open();
????????SqlCommand mycom = new SqlCommand("select id,title,content from XK_Content",mycon);
????????return mycom.ExecuteReader();
????}
????//对数据库中的字段建立索引
????public IndexWriter CreateIndex(SqlDataReader myred) {
????????IndexWriter writer = new IndexWriter("D:/index/",new ChineseAnalyzer(),true);?? //索引的存储位置
????????try
????????{
????????????while (myred.Read())
????????????{
????????????????Document doc = new Document();
????????????????doc.Add(new Field("id", myred["id"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
????????????????doc.Add(new Field("title", myred["title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
????????????????doc.Add(new Field("content", myred["content"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
????????????????writer.AddDocument(doc);
????????????}
????????????writer.Optimize(); //优化索引
????????????writer.Close();
????????????myred.Close();
????????}
????????catch (Exception e) {
????????????Response.Write(e);
????????????myred.Close();
????????}
????????return writer;
????}
????//根据关键词搜索索引
????public Hits seacher(String queryString) {
????????Hits hits = null;
????????try
????????{
????????????IndexSearcher mysea = new IndexSearcher("D:/index/");
????????????QueryParser queryParser = new QueryParser("content", new ChineseAnalyzer());
????????????Query query = queryParser.Parse(queryString);
????????????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");
????????Hits myhit = seacher(this.tj.Text.Trim());
????????if (myhit != null)
????????{
????????????for (int i = 0; i < myhit.Length(); i++)
????????????{
????????????????Document 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.gview.DataSource = mytab;
????????????this.gview.DataBind();
????????}
????????else {
????????????Response.Write("查询失败!");
????????}
????}
}

你可能感兴趣的:(asp.net)