利用Lucene.net实现检索并对检索关键字高亮显示

Lucene.NET是一个全文搜索框架,而不是应用产品。

关于其中用到的两个dll文件,可到http://d.download.csdn.net/down/2352061/taomanman免费下载。

 直接贴代码了:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="xxjs.aspx.cs" Inherits="XXJS_xxjs" %>

<%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %>

<!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 runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="height:40px">
            <asp:Button ID="btnCreateIndex" Text="建立索引" runat="server" onclick="btnCreateIndex_Click" />
            <asp:Label ID="lbStatus" runat="server"></asp:Label>
        </div>
        <table>
            <tr>
                <td><asp:TextBox ID="tbSearchContent" runat="server" Height="20px" Width="155px"></asp:TextBox></td>
                <td><asp:Button ID="btnSearch" runat="server" Text="搜索" onclick="btnSearch_Click"/></td>
            </tr>
        </table>
        <div id="msg" runat="server" bordercolorlight="#99cc99" 
            bordercolordark="#669933" bgcolor="#dde3ce" style="background-color: #C0C0C0"></div>
            <div>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
                    Width="100%" GridLines="None" AllowPaging="true" 
                    onpageindexchanging="GridView1_PageIndexChanging">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <table width="100%" border="0" align="center" cellpadding="5" cellspacing="1">
                  <tr>
                   <td bgcolor="#ffffff" >
                    <font size="2"><asp:HyperLink ID="HyperLink1" runat="server" Text='<%#Eval("标题") %>' NavigateUrl='<%#Eval("链接地址") %>'></asp:HyperLink></font>
                   </td>
                  </tr>
                  <tr>
                      <td>
                          <font size="2"><%# this.getSubString(Eval("内容").ToString())%></font>
                      </td>
                  </tr>
                 </table>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
            </div>
          </div>
        </form>
    </body>
</html>
----------------------------------------------------------------------------------------------------------------------------------------------------

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 USTC;

using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Search;
using Lucene.Net.QueryParsers;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Store;
using Lucene.Net.Highlight;
using Lucene.Net.Analysis;

public partial class XXJS_xxjs : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //只在画面第一次读取的时候取得资料
            VSDataTable = new DataTable();
        }
        PageDataBind(); //绑定资料
    }

    protected void btnCreateIndex_Click(object sender, EventArgs e)
    {
        CreateIndex();
    }

    //建立索引
    public IndexWriter CreateIndex()
    {
        string INDEX_STORE_PATH = Server.MapPath("index");  //INDEX_STORE_PATH 为索引存储目录
        //IndexReader reader = IndexReader.Open(INDEX_STORE_PATH);
        //Term term = new Term(field, key);
        //reader.DeleteDocuments(term);
        //reader.Close();
        IndexWriter writer = null;
        try
        {
            writer = new IndexWriter(INDEX_STORE_PATH, new StandardAnalyzer(), true);
            DM dm = new DM();
            string strSQL = "select c_title,c_text,c_url from v_searches";
            DataSet ds = dm.getsql(strSQL);
            //建立索引字段
            foreach (DataRowView drv in ds.Tables[0].DefaultView)
            {
                Document doc = new Document();
                doc.Add(new Field("标题", drv["c_title"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(new Field("内容", drv["c_text"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(new Field("链接地址", drv["c_url"].ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                doc.Add(new Field("标题及内容", drv["c_title"].ToString() + drv["c_text"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));//不存储,索引,标题及内容实现了标题和内容的索引
                writer.AddDocument(doc);
            }
            writer.Optimize();
            writer.Close();

            this.lbStatus.Text = "索引建立成功!";
        }
        catch
        {
            this.lbStatus.Text = "索引已经创建过,无需重新创建!";
        }
        return writer;
    }

    //信息检索
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindLuceneData();
    }

    public void BindLuceneData()
    {
        string INDEX_STORE_PATH = Server.MapPath("index");//INDEX_STORE_PATH 为索引存储目录 
        string keyword = tbSearchContent.Text;  //搜索关键字
        if (keyword != "")
        {
            Hits myhit = null;
            IndexSearcher mysea = new IndexSearcher(INDEX_STORE_PATH);
            Analyzer analyzer = new StandardAnalyzer();    //定义分词器 
            QueryParser q = new QueryParser("标题及内容", analyzer);
            Query query1 = q.Parse(keyword);

            //高亮显示
            BooleanQuery bquery = new BooleanQuery();
            Highlighter highlighter = new Highlighter(new SimpleHTMLFormatter("<font color=/"red/">", "</font>"), new QueryScorer(query1));//使用高亮关键字显示替代关键字
            highlighter.SetTextFragmenter(new SimpleFragmenter(100));

            DateTime start = DateTime.Now;
            myhit = mysea.Search(query1);
            DateTime end = DateTime.Now;
            long time = end.Millisecond - start.Millisecond;
            this.msg.InnerHtml = "<font size=2 >关于 <b><font color=blue>" + keyword + "</font><b/> 共搜索到<b><font color=red>" + myhit.Length() + "</font></b>个相关结果.搜索耗时:<font color=red>" + time + "</font>毫秒.</font></br>";

            if (myhit != null)
            {
                DataRow myrow;
                DataTable mytab = new DataTable();
                mytab.Columns.Add("标题");
                mytab.Columns.Add("内容");
                mytab.Columns.Add("链接地址");
                mytab.Clear();
                for (int i = 0; i < myhit.Length(); i++)
                {
                    Document doc = myhit.Doc(i);
                    myrow = mytab.NewRow();
                    myrow[0] = doc.Get("标题").ToString();
                    myrow[1] = doc.Get("内容").ToString();
                    myrow[2] = doc.Get("链接地址").ToString();

                    Lucene.Net.Analysis.TokenStream titleStream = analyzer.TokenStream("标题", new System.IO.StringReader(doc.Get("标题").ToString()));
                    string titleResult = highlighter.GetBestFragments(titleStream, doc.Get("标题").ToString(), 0, "...");
                    Lucene.Net.Analysis.TokenStream contentStream = analyzer.TokenStream("内容", new System.IO.StringReader(doc.Get("内容").ToString()));
                    string contentResult = highlighter.GetBestFragments(contentStream, doc.Get("内容").ToString(), 0, "...");
                    if (!string.IsNullOrEmpty(titleResult))
                    {
                        myrow[0] = titleResult;
                    }
                    if (!string.IsNullOrEmpty(contentResult))
                    {
                        myrow[1] = contentResult;
                    }
                    mytab.Rows.Add(myrow);
                    myrow.AcceptChanges();
                }

                GridView1.DataSource = mytab;
                GridView1.DataBind();
                ViewState["MyDataTable"] = mytab;
            }
            else
            {
                Response.Write("Hits为空");
            }
            mysea.Close();
        }
        else
        {
            return;
        }
    }
        
    //分页事件
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.DataSource = VSDataTable;
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataBind();
    }
    
    /// <summary>
    /// 存放在ViewState的DataTable
    /// </summary>
    private DataTable VSDataTable
    {
        get { return ViewState["MyDataTable"] as DataTable; }
        set { ViewState["MyDataTable"] = value; }
    }

    /// <summary>
    /// 普通的绑定资料
    /// </summary>
    private void PageDataBind()
    {
        GridView1.DataSource = VSDataTable;
        GridView1.DataBind();
    }

    /// <summary>
    /// 绑定资料加换页
    /// </summary>
    /// <param name="getPageIndex">新页面Index</param>
    private void PageDataBind(Int32 getPageIndex)
    {
        GridView1.DataSource = VSDataTable;
        GridView1.PageIndex = getPageIndex;
        GridView1.DataBind();
    }

    public string getSubString(string str)
    {
        if (str.Length > 200)
            str = str.Substring(0, 197)+"  ...";
       return str;
    }

}

你可能感兴趣的:(Lucene)