最简单的Lucene.Net-2.0-004-11Mar07索引+搜索

来源:互联网 发布:剑三不合法的脸数据 编辑:程序博客网 时间:2024/05/21 14:58

这几天看demo完成了一个简单的索引+搜索。网上大多数是dotLucene.net 的例子,而Lucene.Net 2.0 的很少,其实连 http://www.dotlucene.net/ 都关掉了,由于刚刚接触Lucene.net ,网上很多人说 Lucene.Net 从 2.0 开始就开始收费,其实,现在Lucene.net 又开始免费了。Lucene.net 现在成为apache 的开源项目 http://incubator.apache.org/projects/lucene.net.html 。我想也正因此,dotLucene.net 才关闭的吧。回想起这几天研究Lucene.net 很是奇怪,刚开始的2天,还在 dotlucene.net 上面找资料,可是这个星期一一看,竟然出现关闭页面。开始很紧张,以为唯一一个 .net 的开源Lucene也没了。后来一看作者的介绍才发现竟然在apache.org 有一个开源的项目。而且按照项目介绍,Lucene.net 是完全按照java版来翻译的,连索引文件都可以通用。这也坚定了我研究最新版的Lucene.net 的决心。

In addition to the APIs and classes port to C#, the algorithm of Java Lucene is ported to C# Lucene. This means an index created with Java Lucene is back-and-forth compatible with the C# Lucene; both at reading, writing and updating. In fact a Lucene index can be concurrently searched and updated using Java Lucene and C# Lucene processes.

好了,废话少说,经过1天的摸索,终于调试出了第一个demo程序了。

 

点击建立索引之后,在程序所在的Txt 目录里的所有文本格式的文件都会被索引。(虽然说是文本,其实不管后缀是什么都会索引)

在文本框里(txtSch)输入关键词,点搜索,能输出结果(txtR ),并显示文件的所在位置。

建立索引页面代码

private void CreateIndex()
   {
    try
    {
     //建立一个索引文件
     IndexWriter writer=new IndexWriter(Application.StartupPath+"//Index",new StandardAnalyzer(),true);
     IndexDirectory(writer,new FileInfo(Application.StartupPath+"//Txt"));
     writer.Optimize();
     writer.Close();
     Result("建立索引成功!");
    }
    catch
    {
     Result("出错啦!");
    }

   }

   //利用递归找到该目录下所有htm文件,用于建立索引
   private void IndexDirectory(IndexWriter writer, FileInfo file)
   {
    if (System.IO.Directory.Exists(file.FullName))
    {
     String[] files = System.IO.Directory.GetFileSystemEntries(file.FullName);
     // an IO error could occur
     if (files != null)
     {
      for (int i = 0; i < files.Length; i++)
      {
       IndexDirectory(writer, new FileInfo(files[i])); //递归找出所有的文件
      }
     }
    }
    else
    {
     try
     {
      writer.AddDocument(FileDocument.Document(file));
      Result(file.FullName+": 索引!");
     }
     catch
     {

     }
    }
   }

   //输出结果
   private void Result(string str)
   {
    txtR.Text=str+"/r/n"+txtR.Text;
    txtR.Update();
   }

   private void button2_Click(object sender, System.EventArgs e)
   {
    if(txtSch.Text=="" || txtSch.Text==null)
    {
     MessageBox.Show("请填写完成!");
    }
    else
    {
     Thread t=new Thread(new ThreadStart(Search));
     t.Start();
    }
   }

搜索代码
private void Search()
   {
    string q=txtSch.Text;
    QueryParser parser=new QueryParser("contents",new StandardAnalyzer());
    Query query=parser.Parse(q);
    IndexSearcher sch=new IndexSearcher(Application.StartupPath+"//Index");
    Hits hit=sch.Search(query);
    StringBuilder strR=new StringBuilder();
    strR.Append("一共找到"+hit.Length()+" 个结果!/r/n");
    for (int i=0; i<hit.Length();i++)
    {
    
     strR.Append(i+" "+hit.Id(i)+" "+hit.Score(i)+"/r/n");
     Document d=hit.Doc(i);
     strR.Append(i+" "+d.Get("path")+"/r/n");
     strR.Append(d.Get("contents")+"/r/n");
    }
    txtR.Text=strR.ToString();
    sch.Close();
   
   }

接下来的工作就是根据这个简单的demo程序来分析里面的源代码了。然后将中文分词加入进入。现在搜索出来的结果匹配得很不准确。

原创粉丝点击