最简单的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程序来分析里面的源代码了。然后将中文分词加入进入。现在搜索出来的结果匹配得很不准确。
- 最简单的Lucene.Net-2.0-004-11Mar07索引+搜索
- 基于Lucene.Net包装的傻瓜索引搜索系统
- Lucene索引和搜索核心类的简单介绍
- lucene 4.3 索引的简单创建和搜索代码展示
- Lucene整理--索引的搜索
- Lucene.NET使用入门(二)【简单的搜索网站示例】
- 用Lucene.Net轻松创建最简单的搜索引擎
- 用Lucene.Net轻松创建最简单的搜索引擎
- Lucene.Net索引的建立
- Lucene索引的简单使用
- lucene搜索简单的实例
- Lucene以及索引和搜索的流程
- lucene多索引上的搜索
- 基于lucene的案例开发:搜索索引
- lucene的建立索引,搜索,中文分词
- Lucene搜索已经创建好的索引
- 使用Lucene开发简单的站内新闻搜索引擎(索引的搜索)
- c#使用Lucene.net创建索引,实现搜索的代码示例
- 写在母亲节
- C#页面的执行过程
- Lucene.net中文分词探究
- Lucene.net中文分词探究(2)
- 不信谣,不传谣,不造谣
- 最简单的Lucene.Net-2.0-004-11Mar07索引+搜索
- Java连接SQL Server 2000
- WindwoXp下配置CVSNT+TortoiseCVS
- 在DotLucene/Lucene.net中, 增加自己的中文分词Analyzer
- 08年上半年程序员考试考完了啊!!!!
- 我的blog
- Java基础方面
- C++之父元旦专访(8+13个问题,关于C++的学习&使用和未来)(转帖)
- 用JS解决网站防挂iframe木马的方案。