[Lucene]使用Lucene.net索引目录文件实例代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Index;
using Lucene.Net.Store;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Search;
using Lucene.Net.Analysis.Cn;
using Lucene.Net.Documents;
using Lucene.Net.QueryParsers;
using System.IO;
namespace Search
{
    class Program
    {
        static void Main(string[] args)
        {
            //
            StandardAnalyzer analyzer=new StandardAnalyzer();
            //建立一个内存目录
            //Lucene.Net.Store.RAMDirectory ramDir = new Lucene.Net.Store.RAMDirectory();
            Lucene.Net.Store.Directory ramDir = FSDirectory.GetDirectory("../index/", true);
           
            //建立一个索引书写器
            IndexWriter ramWriter = new IndexWriter(ramDir,analyzer , true);
            //ramWriter.SetMaxFieldLength(25000);
            ////要索引的词,这就相当于一个个的要索引的文件
            //string[] words = { "中华人民共和国", "人民共和国", "人民", "共和国" };
            ////循环数组,创建文档,给文档添加字段,并把文档添加到索引书写器里
            //Document doc = null;
            //for (int i = 0; i < words.Length; i++)
            //{
            //    doc = new Document();
            //    doc.Add(new Field("contents", words[i], Field.Store.YES, Field.Index.TOKENIZED));
            //    ramWriter.AddDocument(doc);
            //}
            IndexDirectory(ramWriter, new System.IO.FileInfo("../tmp/"));    
            //索引优化
            ramWriter.Optimize();
            //TokenStream st = analyzer.TokenStream("contents", new StringReader());
            //关闭索引读写器,一定要关哦,按理说应该把上面的代码用try括主,在finally里关闭索引书写器
            ramWriter.Close();
            //构建一个索引搜索器
            IndexSearcher searcher = new IndexSearcher(ramDir);
            //用QueryParser.Parse方法实例化一个查询
            //Query query = QueryParser.Parse("中华人民", "contents", new ChineseAnalyzer());
            QueryParser parser=new QueryParser("contents",analyzer);
            Query query = parser.Parse("唐");
            //获取搜索结果
            Hits hits = searcher.Search(query);
          
           
            //判断是否有搜索到的结果,当然你也可以遍历结果集并输出
            if (hits.Length() != 0)
                Console.WriteLine("有");
            else
                Console.WriteLine("没有");
            for (int i = 0; i < hits.Length(); i++)
            {
                Document doc = hits.Doc(i);
                Console.Write(doc.Get("contents"));
                //String[] str = hits.Doc(i).GetValues("contents");
                //for (int j = 0; j < str.Length; j++)
                //{
                //    Console.Write(str[j]);
                //}
                //TextReader reader=doc.GetField("contents").ReaderValue();
                //Console.WriteLine(reader.ReadLine());
                       
               
            }
           
            searcher.Close();
            ramDir.Close();
            Console.Read();
        }
        /// <summary>
        /// 生成指定文件或目录的索引
        /// </summary>
        /// <param name="writer"></param>
        /// <param name="file"></param>
        public static 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 if (file.Extension == ".htm")//文件扩展名符合直接创建索引
            {
                IndexFile(file, writer);
            }
        }
        /// <summary>
        /// 创建文件的索引
        /// </summary>
        /// <param name="file">文件路径</param>
        /// <param name="writer">索引编写器</param>
        private static void IndexFile(FileInfo file, IndexWriter writer)
        {
            Console.Out.WriteLine("adding " + file);
            try
            {
                //创建新文档
                Document doc = new Document();
                //添加Field
                doc.Add(new Field("filename", file.FullName,Field.Store.YES,Field.Index.TOKENIZED));
                //读取文件内容
                string values;
                using(StreamReader reader=new StreamReader(file.FullName,Encoding.UTF8))
                {
                    values = reader.ReadToEnd();
                }
                //创建Field
                doc.Add(new Field("contents",values,Field.Store.YES,Field.Index.TOKENIZED));
                //写入索引
                writer.AddDocument(doc);
            }
            catch (FileNotFoundException fnfe)
            {
            }
        }
    }
}

赞(0) 打赏
分享到: 更多 (0)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏