Lucene.NET使用入门(一)【实现快速搜索】

要想成就一个伟人,不仅要有聪明的头脑,还要有执着的信念,滴水穿石的雄心。

Product.cs实体类:

 /// 
    /// 商品类
    /// 
    public class Product
    {
        /// 
        /// 商品编号
        /// 
        private int _id;

        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        /// 
        /// 商品名称
        /// 
        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public Product() { }
        public Product(int id,string name)
        {
            this.Id = id;
            this.Name = name;
        }

    }

ProductDAL.cs代码:

 public static class ProductDAL
    {
        /// 
        ///取出所有的商品
        /// 
        /// 商品集合
        public static List GetProductInfo()
        {
            List list = new List();
            //连接数据库
            for (int i = 1; i <= 100;i++ )
            {
                Product product = new Product(i,"商品"+i.ToString());
                list.Add(product);
            }
            return list;
        }
    }

ProductBLL.cs代码:

  public static class ProductBLL
    {
        /// 
        /// 取出所有的商品
        /// 
        /// 商品集合
        public static List GetProductInfo()
        {
            return ProductDAL.GetProductInfo();
        }
    }

CreateLucene.cs代码:

/// 
/// CreateIndex 的摘要说明
/// 
public class CreateLucene
{
    public CreateLucene()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    /// 
    /// 创建索引
    /// 
    /// 商品集合
    public static void CreateIndex(List list)
    {
        //建立分子器
        Analyzer analyzer = new StandardAnalyzer();
        IndexWriter indexwriter = new IndexWriter("All_Product", analyzer, true);
        for (int i = 0, count = list.Count; i < count;i++ )
        { 
            Product product=list[i];
            Document document = new Document();
            document.Add(new Field("productId",product.Id.ToString(),Field.Store.YES,Field.Index.TOKENIZED));
            document.Add(new Field("productName",product.Name,Field.Store.YES,Field.Index.TOKENIZED));

            indexwriter.AddDocument(document);
        }
        indexwriter.Optimize();
        indexwriter.Close();
    }
}

SearchProduct.cs代码:

/// 
/// SearchProduct 的摘要说明
/// 
public class SearchProduct
{
    public SearchProduct()
    { }
    public static List SearchProductInfo(string key)
    {
        Analyzer analyzer = new StandardAnalyzer();
        List list = new List();

        IndexSearcher indexsearcher = new IndexSearcher("All_Product");

        QueryParser queryParser = new QueryParser("productName", analyzer);
        Query query = queryParser.Parse(key);
        //采样
        Hits hits = indexsearcher.Search(query);

        if (hits.Length() > 0)
        {
            for (int i = 0, count = hits.Length(); i < count;i++ )
            {
                Document document = hits.Doc(i);
                Product product = new Product();
                product.Id =Convert.ToInt32(document.Get("productId"));
                product.Name = document.Get("productName");

                list.Add(product);
            }
        }
        indexsearcher.Close();
        return list;
    }
}

Global.asax内容:

showproduct.aspx.cs

//此处用到的也是标准分词,调用盘古分词搜索的体验会更好些
public partial class _Default : System.Web.UI.Page 
{
    /// 
    /// 商品名称关键字
    /// 
    private string NameKey
    {
        get
        {
            if (this.Request["txtNameKey"] != null)
            {
                return this.Request["txtNameKey"];
            }
            return "";
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    /// 
    /// 得到商品集合
    /// 
    /// 商品集合
    protected List GetProductInfo()
    {
        List list = new List();
        if (this.NameKey != "")
        {
            list = SearchProduct.SearchProductInfo(this.NameKey);
        }

        return list;
    }
}

showproduct.aspx的内容



<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>无标题页title>
head>
<body>
    <form id="form1" method="post" action="showproduct.aspx">
    <div>
        <input type="text" name="txtNameKey" /><input type="submit" value="搜索" />
    div>
    form>
    <hr />
    <div>
        <h2>
            信息显示
        h2>
    div>
    <div>
   <%
       List list = base.GetProductInfo();
       if (list != null && list.Count != 0)
       {
           foreach (Product pro in list)
           {
                %>
                <%=pro.Id%>,<%=pro.Name%><br />
                <%

            }
        }
    %>
    div>
body>
html>

外部DLL引用如图:
这里写图片描述
Lucene.Net点击下载

运行结果如图:

Lucene.NET使用入门(一)【实现快速搜索】_第1张图片

你可能感兴趣的:(Lucene.NET框架)