发表于 2015-6-29 15:04:37

lucene与sql server数据库实现索引的简单实例(vs.net2008)

  
  lucene建立索引,原理很简单,只要能够得到你想检索内容的文本形式,任何的数据库都可以建立你想实现的索引功能多数据库,多表都可以建立索引
  

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Collections;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Analysis.Standard;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }

    protected void Button2_Click(object sender, EventArgs e)
    {
      CreateIndex();
    }
    //获得查询结果
    public SqlDataReader ExecuteQuery(string sql)
    {
      string connstr = @"server=localhost\sqlexpress;uid=sa;pwd=123;database=lucenetest";
      SqlConnection con = new SqlConnection(connstr);
      
      if(con.State== ConnectionState.Closed)con.Open();
      SqlCommand command = new SqlCommand(sql, con);
      SqlDataReader datareader = command.ExecuteReader();
      return datareader;
    }
    //建立索引
    public IndexWriter CreateIndex()
    {
      string INDEX_STORE_PATH = Server.MapPath("index");//INDEX_STORE_PATH 为索引存储目录

      IndexWriter writer = null;
      try
      {
            writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true);
            SqlDataReader myred = ExecuteQuery("select wid,title,content,createdate from article");
            //建立索引字段
            while (myred.Read())
            {
                Document doc = new Document();
                doc.Add(new Field("tablename", "article", Field.Store.YES, Field.Index.UN_TOKENIZED));//存储,不索引
                doc.Add(new Field("wid", myred["wid"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(new Field("title", myred["title"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(new Field("indexcontent", myred["title"].ToString() + myred["content"].ToString(), Field.Store.NO, Field.Index.TOKENIZED));//不存储,索引,indexcontent实现了title和content,也就是标题和内容的索引
                doc.Add(new Field("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();
      }
      return writer;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
      string INDEX_STORE_PATH = Server.MapPath("index");//INDEX_STORE_PATH 为索引存储目录
      string keyword = TextBox2.Text;
      Hits myhit = null;
      IndexSearcher mysea = new IndexSearcher(INDEX_STORE_PATH);
      QueryParser q = new QueryParser("indexcontent", new StandardAnalyzer());
      Query query = q.Parse(keyword);
      
      myhit = mysea.Search(query);
      Response.Write("关于:" + keyword + "搜索到" + myhit.Length() + "个结果");
      
      if (myhit != null)
      {
            DataRow myrow;
            DataTable mytab = new DataTable();
            mytab.Columns.Add("wid");
            mytab.Columns.Add("title");
            mytab.Columns.Add("createdate");
            mytab.Columns.Add("tablename");
            mytab.Clear();
            for (int i = 0; i < myhit.Length(); i++)
            {
                Document doc = myhit.Doc(i);
                myrow = mytab.NewRow();
                myrow = doc.Get("wid").ToString();
                myrow = doc.Get("title").ToString();
                myrow = doc.Get("createdate").ToString();
                myrow = doc.Get("tablename").ToString();
                mytab.Rows.Add(myrow);
                myrow.AcceptChanges();
            }
            GridView1.DataSource = mytab;
            GridView1.DataBind();
      }
      else
      {
            Response.Write("Hits为空");
      }
      mysea.Close();
    }
}
  
点击下载实例:下载(vs.net2008,lucene.net2.0)
页: [1]
查看完整版本: lucene与sql server数据库实现索引的简单实例(vs.net2008)