zoukankan      html  css  js  c++  java
  • Lucene.NET入门实例

    今天从网上找了一些关于Lucene.NET的资料,自己做个demo测试了一下。

    Lucene.NET 是开源的项目,从网上很容易找到源代码,新建一个web测试页面和一个控制台程序,控制台程序用来创建lucene的索引

    控制台代码如下:

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Lucene.Net.Analysis.Standard;
    using Lucene.Net.Analysis;
    using Lucene.Net.Index;
    using Lucene.Net.Documents;
    using System.IO;
    using Lucene.Net.Analysis.Cn;
    using Lucene.Net.Analysis.CJK;
    using System.Data.SqlClient;
    using System.Data;
    namespace Test
    {
        
    class TokenizerTest
        {
            
    public static string connstr = "server=.;database=test;uid=sa;pwd=";

            
    public static SqlConnection mycon;
            
    static void Main(string[] args)
            {
                SqlDataReader myred 
    = OpenTable();

                IndexWriter writer 
    = CreateIndex(myred);
                Console.WriteLine(
    "索引创建完成");
                Console.Read();
            }
            
    public static SqlDataReader OpenTable()
            {

                mycon 
    = new SqlConnection(connstr);
                mycon.Open();
                SqlCommand mycom 
    = new SqlCommand("select * from Article", mycon);
                DataTable tb 
    = new DataTable();
                SqlDataAdapter adp 
    = new SqlDataAdapter(mycom);
                adp.Fill(tb);
                DataTable d 
    = tb;
                
    return mycom.ExecuteReader();

            }

            
    //对数据库中的字段建立索引

            
    public static 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("summary", myred["summary"].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;

            }
        }

    } 

    创建好索引之后,就可以使用了测试页面代码:

    protected void Search_Click(object sender, EventArgs e)
        {
            DataRow dr;
            DataTable luctb 
    = new DataTable();
            luctb.Columns.Add(
    "id");
            luctb.Columns.Add(
    "title");
            luctb.Columns.Add(
    "summary");
            Stopwatch sw 
    = new Stopwatch();
            sw.Start();
            Hits myhit 
    = seacher(this.tj.Text.Trim());

            
    for (int i = 0; i < myhit.Length(); i++)
            {

                Document doc 
    = myhit.Doc(i);

                dr 
    = luctb.NewRow();

                dr[
    0= doc.Get("id").ToString();

                dr[
    1= doc.Get("title").ToString();

                dr[
    2= doc.Get("summary").ToString();

                luctb.Rows.Add(dr);

                dr.AcceptChanges();

            }
            sw.Stop();
            
    this.Label1.Text = "" + luctb.Rows.Count + "" + sw.ElapsedMilliseconds.ToString();
            
    this.gview.DataSource = luctb;

            
    this.gview.DataBind();
            DataTable sqltb 
    = new DataTable();

            mycon 
    = new SqlConnection(connstr);
            mycon.Open();
            Stopwatch swsql 
    = new Stopwatch();
            swsql.Start();
            SqlCommand mycom 
    = new SqlCommand(string.Format("select Id,title,summary from Article where summary like '%{0}%' or title like '%{0}%'"this.tj.Text.Trim()), mycon);
            DataTable tb 
    = new DataTable();
            SqlDataAdapter adp 
    = new SqlDataAdapter(mycom);
            adp.Fill(tb);
            swsql.Stop();
            
    this.Label2.Text = "" + tb.Rows.Count + "" + swsql.ElapsedMilliseconds.ToString();
            
    this.gviewsql.DataSource = tb;

            
    this.gviewsql.DataBind();

        }

    代码中分别利用lucene和sql语句从Article查询title和summary两个字段中包含要查询字符的记录。表中共20w+条数据,

     测试结果:lucene耗时:共35行16毫秒sql耗时:共35行2331毫秒

  • 相关阅读:
    15.[JavaScript]第8章对象和数组[上,下, 中]
    centos 7 firewall(防火墙)开放端口/删除端口/查看端口
    Docker使用docker-compose.yml构建Asp.Net Core和Mysql镜像并与Mysql数据库通信
    Docker使用Dockerfile构建Asp.Net Core镜像
    Docker使用Mysql镜像命令
    指定的 CGI 应用程序遇到错误,服务器终止了该进程。
    常见SMTP发送失败原因列表
    MVC View中获取action、controller、area名称、参数
    status 返回当前请求的http状态码
    asp:GridView控件使用FindControl方法获取控件的问题
  • 原文地址:https://www.cnblogs.com/dlz1123/p/1963996.html
Copyright © 2011-2022 走看看