Lucene.net核心类简介


StandardAnalyzer示例(不用背代码,拷过来知道改哪里即可,我复制粘贴的代码你也一样复制粘贴)
 Analyzer analyzer = new StandardAnalyzer();
            TokenStream tokenStream = analyzer.TokenStream("",new StringReader("北京,Hi欢迎你们大家"));
            Lucene.Net.Analysis.Token token = null;
            while ((token = tokenStream.Next()) != null)
            {
                Console.WriteLine(token.TermText());
            }

 

1、对数据进行索引
 string indexPath = “d:/temp”;//注意和磁盘上文件夹的大小写一致,否则会报错。
FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath),new NativeFSLockFactory());
bool isExists= IndexReader.IndexExists(directory);
            if (isExists)
            {
                //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
        //Lucene.Net在写索引库之前会自动加锁,在close的时候会自动解锁
        //不能多线程执行,只能处理意外被永远锁定的情况
        if (IndexWriter.IsLocked(directory))
                {
                  IndexWriter.Unlock(directory);//强制解锁
                }
        }
 IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isExists, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);
            for (int i = 1000; i < 1100; i++)
            {
                string txt = File.ReadAllText(@"C:\MxDownload\dnt_3.1.0_sqlserver\upload_files\文章\" + i + ".txt");
                Document document = new Document();//一条Document相当于一条记录
                document.Add(new Field(“number”, i.ToString(), Field.Store.YES, Field.Index. NOT_ANALYZED));//每个Document可以有自己的属性(字段),所有字段名都是自定义的,值都是string类型
                document.Add(new Field("body", txt, Field.Store.YES, Field.Index. ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                writer.AddDocument(document);//insert into...插入一条记录,有两个字段:number和body
                Console.WriteLine("索引"+i+"完毕");
            }
            writer.Close();
            directory.Close();//不要忘了Close,否则索引结果搜不到


 

posted @ 2017-02-20 18:23  双鱼小毅  阅读(103)  评论(0)    收藏  举报