Lucene的使用(一)

Lucene是一个全文检索类库(Library),基本原理是索引检索.在搜索引擎领域,Lucene的影响非常大.在这里就不介绍了,这方面网上文章很多.这里主要通过一个本地文件检索程序来简单学习一下Lucene的基本使用.

 

利用Lucene来建立检索系统主要有以下几步:首先是建立索引,然后再对索引进行检索,当然分析器贯穿始终.

(1)建立索引

 

ContractedBlock.gif ExpandedBlockStart.gif Code
       //为目录建立索引
        private static void IndexDirectory(IndexWriter writer, FileInfo file)
        {
            
if (Directory.Exists(file.FullName))//如果是目录,则对目录下文件建立索引
            {
                String[] files 
= Directory.GetFileSystemEntries(file.FullName);
                
if (files != null
                {
                    
for (int i = 0; i < files.Length; i++)
                    {
                        IndexDirectory(writer, 
new FileInfo(files[i]));  //递归
                    }
                }
            }
            
else //如果是文件,则直接建立索引
            {
                IndexFile(writer, file);
            }
        }
        
//为文件建立索引
        private static void IndexFile(IndexWriter writer, FileInfo file)
        {
            Lucene.Net.Store.Directory ramDirctory 
= new Lucene.Net.Store.RAMDirectory();
            IndexWriter ramWriter 
= new IndexWriter(ramDirctory, new StandardAnalyzer(), true);
            Console.Out.WriteLine(
"创建索引" + file.FullName);
            filecount
++;//文件个数加1

            Document doc 
= new Document();
            Field field_name 
= new Field("name", file.Name, Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field_name);
            Field field_path 
= new Field("path", file.FullName, Field.Store.YES, Field.Index.UN_TOKENIZED);
            doc.Add(field_path);
            ramWriter.AddDocument(doc);
            ramWriter.Close();
            writer.AddIndexes(
new Lucene.Net.Store.Directory[] { ramDirctory });
        }

 

(2)利用索引查询

 

ContractedBlock.gif ExpandedBlockStart.gif Code
//根据文件名查找
        private static void Search(string filename)
        {
            IndexSearcher searcher 
= new IndexSearcher(Dest_Index_Path);
            Term term 
= new Term("name", filename);
            Query query 
= null;
            
if (type == 0)
            {
                query 
= new PrefixQuery(term);
            }
            
else if (type == 1)
            {
                query 
= new FuzzyQuery(term);
            }
            
else
            {
                
throw new Exception("查询类别参数不对!");
            }
            Hits hits 
= searcher.Search(query);
            Console.WriteLine(
"满足条件的查找结果:");
            
for (int i = 0; i < hits.Length(); i++)
            {
                Console.WriteLine(
"路径为:" + hits.Doc(i).GetField("path").StringValue());
            }
        }

 

我对Lucene.Net的api文档下的2465个文件做了一个测试.运行程序输入:MySearcher -file MSDN.

结果如下:

 

另附源代码:

源代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值