Пример #1
0
        public static IndexManager Instance()
        {
            if (_IndexWriter != null)
            {
                _IndexWriter.Dispose();
            }

            if (_FSDirectory != null)
            {
                _FSDirectory.Dispose();
            }

            return(_Instance);
        }
Пример #2
0
        public int Count(NuixLogRepo repo, string queryString)
        {
            // Lucene seems to be picky about lower case for some things?
            Regex notFix = new Regex(@"\bnot\b", RegexOptions.Compiled);

            queryString = notFix.Replace(queryString, "NOT");

            // Blank query means all items
            if (string.IsNullOrWhiteSpace(queryString))
            {
                return((int)repo.Database.TotalRecords);
            }
            else
            {
                luceneDirectory = FSDirectory.Open(IndexDirectory);
                analyzer        = getAnalyzer();
                IndexSearcher searcher = new IndexSearcher(luceneDirectory);

                Query query = parseQuery(queryString);

                var hitsFound = searcher.Search(query, int.MaxValue);
                int hitCount  = hitsFound.TotalHits;

                luceneDirectory.Dispose();
                searcher.Dispose();
                analyzer.Dispose();

                return(hitCount);
            }
        }
Пример #3
0
 public void Clear()
 {
     fSDirectory.Dispose();
     GC.Collect();
     GC.WaitForPendingFinalizers();
     GC.Collect();
 }
Пример #4
0
        private void button4_Click(object sender, EventArgs e)
        {
            var         indexPath = @"C:\Users\Victor\Desktop\LuceneNetDir";                                   //注意和磁盘上文件夹的大小写一致,否则会出现报错,将创建的分词内容放在该目录下。
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); //指定索引文件(打开索引目录) FS指的就是FileSystem
            var         isUpdate  = IndexReader.IndexExists(directory);                                        //IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。

            if (isUpdate)
            {
                //同时只能有一段代码对索引库进行写操作,当使用IndexWriter打开directory时会自动对索引库文件上锁。
                //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不解锁了吗?这个问题后面解决)
                if (IndexWriter.IsLocked(directory))
                {
                    IndexWriter.Unlock(directory);
                }
            }
            IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);//向索引库中写索引,这时在这里加锁。

            for (int i = 1; i < 10; i++)
            {
                var      txt      = File.ReadAllText(@"C:\Users\Victor\Desktop\LuceneNetDir\测试文件\" + i + ".txt", System.Text.Encoding.Default); //注意这个地方的编码
                Document document = new Document();                                                                                             //表示一篇文档
                //Field.Store.Yes:表示是否储存原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来。Field.Index. Not_ANALYZED:不进行分词保存。
                document.Add(new Field("number", i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));                                     //字段名"number"可更改

                //Field.Index.ANALYZED:进行分词保存,也就是将要进行全文搜索的字段设置分词保存(因为要进行模糊查询)
                //Lucene.Net.Documents.Field.TermVector.WITH_POSITION_OFFSETS:不仅保存分词还保存分词的距离,干扰词的数量
                document.Add(new Field("body", txt, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                writer.AddDocument(document);
            }
            writer.Dispose();    //会自动解锁
            directory.Dispose(); //不要忘了close,否则索引结果搜不到
        }
Пример #5
0
        private void button3_Click(object sender, EventArgs e)
        {
            string      indexPath = "lucenedir";                                                               //注意和磁盘上文件夹的大小写一致,否则会报错。将创建的分词内容放在该目录下。
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); //指定索引文件(打开索引目录) FS指的是就是FileSystem
            bool        isUpdate  = IndexReader.IndexExists(directory);                                        //IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。

            if (isUpdate)
            {
                //同时只能有一段代码对索引库进行写操作。当使用IndexWriter打开directory时会自动对索引库文件上锁。
                //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这个问题后面会解决)
                if (IndexWriter.IsLocked(directory))
                {
                    IndexWriter.Unlock(directory);
                }
            }
            IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);//向索引库中写索引。这时在这里加锁。

            for (int i = 1; i <= 5; i++)
            {
                string   txt      = File.ReadAllText(@"Test\" + i + ".txt", System.Text.Encoding.Default); //注意这个地方的编码
                Document document = new Document();                                                        //表示一篇文档。
                //Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
                document.Add(new Field("number", i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                //Field.Index. ANALYZED:进行分词保存:也就是要进行全文的字段要设置分词 保存(因为要进行模糊查询)

                //Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
                document.Add(new Field("body", txt, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                writer.AddDocument(document);
            }
            writer.Dispose();    //会自动解锁。
            directory.Dispose(); //不要忘了Close,否则索引结果搜不到
        }
Пример #6
0
        private static void UpdateIndex(List <string> folderParseResults, string indexFolderPath)
        {
            Console.WriteLine(" > Adding items to index ...");

            FSDirectory directory   = null;
            Analyzer    analyzer    = null;
            IndexWriter indexWriter = null;

            try
            {
                directory   = FSDirectory.Open(new System.IO.DirectoryInfo(indexFolderPath));
                analyzer    = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30);
                indexWriter = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
            }
            catch (Exception ex)
            {
                throw new ApplicationException("NOTE!", ex);
            }

            foreach (string item in folderParseResults)
            {
                Document document = new Document();
                document.Add(new Field("FilePathString", item, Field.Store.YES, Field.Index.ANALYZED));
                indexWriter.AddDocument(document);
            }

            indexWriter.Optimize();
            indexWriter.Dispose();
            analyzer.Dispose();
            directory.Dispose();
        }
Пример #7
0
        public override sealed void Dispose()
        {
            Debug("Lucene Closing directory.");
            _indexDirectory.Dispose();

            base.Dispose();
        }
Пример #8
0
 public void Create_End()
 {
     writer.Optimize();
     writer.Flush(true, true, true);
     writer.Dispose();
     luceneIndexDirectory.Dispose();
 }
Пример #9
0
        private void WatchIndexTask()
        {
            while (true)
            {
                if (IndexQueue.Count > 0)
                {
                    // 索引文档保存位置
                    FSDirectory directory = FSDirectory.Open(new DirectoryInfo(IndexPath), new NativeFSLockFactory());
                    bool        isUpdate  = IndexReader.IndexExists(directory); //判断索引库是否存在
                    if (isUpdate)
                    {
                        //  如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
                        //  Lucene.Net在写索引库之前会自动加锁,在close的时候会自动解锁
                        //  不能多线程执行,只能处理意外被永远锁定的情况
                        if (IndexWriter.IsLocked(directory))
                        {
                            IndexWriter.Unlock(directory);  //unlock:强制解锁,待优化
                        }
                    }
                    //  创建向索引库写操作对象  IndexWriter(索引目录,指定使用盘古分词进行切词,最大写入长度限制)
                    //  补充:使用IndexWriter打开directory时会自动对索引库文件上锁
                    IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate,
                                                         IndexWriter.MaxFieldLength.UNLIMITED);

                    while (IndexQueue.Count > 0)
                    {
                        IndexTask task;
                        IndexQueue.TryDequeue(out task);
                        long id = task.TaskId;

                        //  一条Document相当于一条记录
                        Document document = new Document();
                        //  每个Document可以有自己的属性(字段),所有字段名都是自定义的,值都是string类型
                        //  Field.Store.YES不仅要对文章进行分词记录,也要保存原文,就不用去数据库里查一次了
                        document.Add(new Field("id", id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                        //  需要进行全文检索的字段加 Field.Index. ANALYZED
                        //  Field.Index.ANALYZED:指定文章内容按照分词后结果保存,否则无法实现后续的模糊查询
                        //  WITH_POSITIONS_OFFSETS:指示不仅保存分割后的词,还保存词之间的距离
                        document.Add(new Field("title", task.Title, Field.Store.YES, Field.Index.ANALYZED,
                                               Field.TermVector.WITH_POSITIONS_OFFSETS));
                        document.Add(new Field("content", task.Content, Field.Store.YES, Field.Index.ANALYZED,
                                               Field.TermVector.WITH_POSITIONS_OFFSETS));
                        if (task.TaskType != TaskTypeEnum.Add)
                        {
                            //  防止重复索引,如果不存在则删除0条
                            writer.DeleteDocuments(new Term("id", id.ToString()));// 防止已存在的数据 => delete from t where id=i
                        }
                        //  把文档写入索引库
                        writer.AddDocument(document);
                    }

                    writer.Dispose();    // Dispose后自动对索引库文件解锁
                    directory.Dispose(); //  不要忘了Dispose,否则索引结果搜不到
                }
                else
                {
                    Thread.Sleep(2000);
                }
            }
        }
Пример #10
0
        public IList <long> Search(NuixLogRepo repo, string queryString)
        {
            // Lucene seems to be picky about lower case for some things?
            Regex notFix = new Regex(@"\bnot\b", RegexOptions.Compiled);

            queryString = notFix.Replace(queryString, "NOT");

            // Emtpy query is all items, we return a special collection that pretends to be a full list of ID values
            // but that is actually just based on the range of possible values.
            if (string.IsNullOrWhiteSpace(queryString))
            {
                return(new AllEntriesIDList((int)repo.Database.TotalRecords));
            }
            else
            {
                luceneDirectory = FSDirectory.Open(IndexDirectory);
                analyzer        = getAnalyzer();
                IndexSearcher searcher = new IndexSearcher(luceneDirectory);
                Query         query    = parseQuery(queryString);

                var hitsFound = searcher.Search(query, int.MaxValue);

                long[] ids = new long[hitsFound.TotalHits];
                for (int i = 0; i < hitsFound.TotalHits; i++)
                {
                    Document doc = searcher.Doc(hitsFound.ScoreDocs[i].Doc);
                    ids[i] = long.Parse(doc.Get("id"));
                }
                luceneDirectory.Dispose();
                searcher.Dispose();
                analyzer.Dispose();

                return(ids);
            }
        }
Пример #11
0
 public override void Dispose()
 {
     if (Enabled)
     {
         _tempStorageDir.Dispose();
     }
     _realDirectory.Dispose();
 }
Пример #12
0
 public void Dispose()
 {
     Info("Lucene Disposal.");
     _indexWriter.Dispose();
     _indexDirectory.Dispose();
     _standardAnalyzer.Close();
     _standardAnalyzer.Dispose();
 }
Пример #13
0
 /// <summary>
 /// Ends InWriteMode after closing Lucene index writing related objects.
 /// </summary>
 public void EndWrite()
 {
     writer.Flush(true, true, true);
     writer.Optimize(true);
     writer.Dispose();
     luceneDirectory.Dispose();
     analyzer.Dispose();
     InWriteMode = false;
 }
Пример #14
0
 public void Dispose()
 {
     analyzer.Close();
     if (currentIndexSearcherHolder != null)
     {
         currentIndexSearcherHolder.SetIndexSearcher(null);
     }
     writer.Dispose();
     directory.Dispose();
 }
Пример #15
0
        public void Dispose()
        {
            Log.App.DebugFormat("Fechando indice");

            writer.Dispose();
            directory.Dispose();

            writer    = null;
            directory = null;
        }
Пример #16
0
 public void Dispose()
 {
     try
     {
         _indexWriter?.Dispose();
         _fSDirectory?.Dispose();
     }
     catch (Exception ex)
     {
         Log.Error(ex, "索引失败");
     }
 }
Пример #17
0
        public void Dispose()
        {
            Info("Lucene Committing.");
            _indexWriter.Commit();

            Info("Lucene Optimizing.");
            _indexWriter.Optimize();

            _indexWriter.Dispose();
            _indexDirectory.Dispose();
            _standardAnalyzer.Close();
            _standardAnalyzer.Dispose();
        }
Пример #18
0
        public static void CreateIndex()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            IndexWriter writer = null;

            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(IndexPath), new NativeFSLockFactory());

            try
            {
                //该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。
                bool isExists = IndexReader.IndexExists(directory);
                if (isExists)
                {
                    //  如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁
                    //  Lucene.Net在写索引库之前会自动加锁,在close的时候会自动解锁
                    //  不能多线程执行,只能处理意外被永远锁定的情况
                    if (IndexWriter.IsLocked(directory))
                    {
                        //unlock:强制解锁,待优化
                        IndexWriter.Unlock(directory);
                    }
                }

                writer = new IndexWriter(directory, analyzer, !isExists, IndexWriter.MaxFieldLength.UNLIMITED);
                //添加索引
                for (int i = 1; i <= 5; i++)
                {
                    Document document = new Document();
                    string   path     = System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.FullName + @"\Data\Test\" + i + ".txt";
                    string   text     = File.ReadAllText(path, Encoding.Default);
                    //Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
                    document.Add(new Field("number", i.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));
                    // Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
                    document.Add(new Field("body", text, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
                    writer.AddDocument(document);
                }
                writer.Optimize();
                sw.Stop();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                writer?.Dispose();
                directory?.Dispose();
            }
        }
Пример #19
0
        public void Dispose()
        {
            if (indexReader != null)
            {
                indexReader.Dispose();
                indexReader = null;
            }

            if (indexDirectory != null)
            {
                indexDirectory.Dispose();
                indexDirectory = null;
            }
        }
Пример #20
0
 public LuceneHelper(string indexLocation, LuceneVersion matchVersion, Analyzer analyzer)
 {
     try
     {
         _indexLocation = indexLocation;
         _matchVersion  = matchVersion;
         _analyzer      = analyzer;
         _fSDirectory   = FSDirectory.Open(_indexLocation, new NativeFSLockFactory());
         if (IndexWriter.IsLocked(_fSDirectory))
         {
             IndexWriter.Unlock(_fSDirectory);
         }
         var indexConfig = new IndexWriterConfig(_matchVersion, _analyzer)
         {
         };
         _indexWriter = new IndexWriter(_fSDirectory, indexConfig);
     }
     catch (Lucene.Net.Store.LockObtainFailedException)
     {
         try
         {
             _indexLocation = indexLocation + "2";
             _matchVersion  = matchVersion;
             _analyzer      = analyzer;
             _fSDirectory?.Dispose();
             _fSDirectory = FSDirectory.Open(_indexLocation, new NativeFSLockFactory());
             if (IndexWriter.IsLocked(_fSDirectory))
             {
                 IndexWriter.Unlock(_fSDirectory);
             }
             var indexConfig = new IndexWriterConfig(_matchVersion, _analyzer)
             {
             };
             _indexWriter = new IndexWriter(_fSDirectory, indexConfig);
         }
         catch (Exception ex)
         {
             Log.Error(ex, "索引失败2");
         }
     }
     catch (Exception ex)
     {
         Log.Error(ex, "索引失败");
     }
 }
Пример #21
0
        public void FullTextConfigSerializeIndexLuceneFS()
        {
            FSDirectory directory = FSDirectory.Open(new DirInfo("test"));
            ConfigurationSerializationContext context = new ConfigurationSerializationContext();
            INode obj = context.Graph.CreateBlankNode();

            context.NextSubject = obj;
            directory.SerializeConfiguration(context);
            directory.Dispose();

            TestTools.ShowGraph(context.Graph);

            ConfigurationLoader.AutoConfigureObjectFactories(context.Graph);
            Object temp = ConfigurationLoader.LoadObject(context.Graph, obj);

            Assert.IsTrue(temp is FSDirectory, "Should have returned a FSDirectory instance");
            Assert.IsTrue(temp is Directory, "Should have returned a Directory instance");
        }
Пример #22
0
        private void CreateIndexContent()
        {
            string      indexPath = ConfigurationManager.AppSettings["Lucenedir"];                             //注意和磁盘上文件夹的大小写一致,否则会报错。将创建的分词内容放在该目录下。写到配置文件中
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); //指定索引文件(打开索引目录) FS指的是就是FileSystem
            bool        isUpdate  = IndexReader.IndexExists(directory);                                        //IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。

            if (isUpdate)
            {
                //同时只能有一段代码对索引库进行写操作。当使用IndexWriter打开directory时会自动对索引库文件上锁。
                //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这个问题后面会解决)
                if (IndexWriter.IsLocked(directory))
                {
                    IndexWriter.Unlock(directory);
                }
            }
            IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, IndexWriter.MaxFieldLength.UNLIMITED);//向索引库中写索引。这时在这里加锁。

            //如果队列中有数据  获取队列数据 写到lucene中
            while (queue.Count > 0)
            {
                FenCiViewModel viewModel = queue.Dequeue();

                writer.DeleteDocuments(new Term("Id", viewModel.Id.ToString()));//删除
                if (viewModel.LuceneTypeEnum == Model.Enum.LuceneTypeEnum.Delete)
                {
                    continue;
                }
                Document document = new Document();//表示一篇文档。
                //Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
                document.Add(new Field("Id", viewModel.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                //Field.Index. ANALYZED:进行分词保存:也就是要进行全文的字段要设置分词 保存(因为要进行模糊查询)

                //Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
                document.Add(new Field("Title", viewModel.Title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                document.Add(new Field("Content", viewModel.Content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                writer.AddDocument(document);
            }

            writer.Dispose();    //会自动解锁。
            directory.Dispose(); //不要忘了Close,否则索引结果搜不到
        }
Пример #23
0
        private void CreateIndexContent()
        {
            var         indexPath = @"C:\Users\Victor\Desktop\LuceneNetDir";                                   //注意和磁盘上文件夹的大小写一致,否则会出现报错,将创建的分词内容放在该目录下。
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); //指定索引文件(打开索引目录) FS指的就是FileSystem
            var         isUpdate  = IndexReader.IndexExists(directory);                                        //IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。

            if (isUpdate)
            {
                //同时只能有一段代码对索引库进行写操作,当使用IndexWriter打开directory时会自动对索引库文件上锁。
                //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不解锁了吗?这个问题后面解决)
                if (IndexWriter.IsLocked(directory))
                {
                    IndexWriter.Unlock(directory);
                }
            }
            IndexWriter writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);//向索引库中写索引,这时在这里加锁。

            while (queue.Count > 0)
            {
                var indexContent = queue.Dequeue();//数据出列
                writer.DeleteDocuments(new Term("Id", indexContent.Id.ToString()));

                if (indexContent.LuceneEnum == LuceneEnum.DeleteType)
                {
                    continue;//跳出本次循环,执行下一次while
                }

                var document = new Document();                                                                        //表示一篇文档
                                                                                                                      //Field.Store.Yes:表示是否储存原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来。Field.Index. Not_ANALYZED:不进行分词保存。
                document.Add(new Field("Id", indexContent.Id.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED)); //字段名"number"可更改

                //Field.Index.ANALYZED:进行分词保存,也就是将要进行全文搜索的字段设置分词保存(因为要进行模糊查询)
                //Lucene.Net.Documents.Field.TermVector.WITH_POSITION_OFFSETS:不仅保存分词还保存分词的距离,干扰词的数量
                document.Add(new Field("Title", indexContent.Title, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
                document.Add(new Field("Content", indexContent.Content, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
                writer.AddDocument(document);
            }

            writer.Dispose();    //会自动解锁
            directory.Dispose(); //不要忘了close,否则索引结果搜不到
        }
Пример #24
0
        private static RAMDirectory RamDir(Guid applicationId)
        {
            if (_RamDirs == null)
            {
                _RamDirs = new SortedList <Guid, RAMDirectory>();
            }

            if (!_RamDirs.ContainsKey(applicationId))
            {
                string path = DocFileInfo.index_folder_address(applicationId);
                if (!System.IO.Directory.Exists(path))
                {
                    System.IO.Directory.CreateDirectory(path);
                }

                FSDirectory dir = FSDirectory.Open(new DirectoryInfo(path));
                _RamDirs.Add(applicationId, new RAMDirectory(dir));
                dir.Dispose();
            }

            return(_RamDirs[applicationId]);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    // Manual release of managed resources.
                    if (_directory != null)
                    {
                        _directory.Close();
                        _directory.Dispose();
                    }

                    if (_reader != null)
                    {
                        _reader.Close();
                        _reader.Dispose();
                    }
                }
                // Release unmanaged resources.
                _disposed = true;
            }
        }
Пример #26
0
        private void CreateSearchContent()
        {
            string      indexPath = @"C:\Lucenedir";                                                           //注意和磁盘上文件夹的大小写一致,否则会报错。将创建的分词内容放在该目录下。写到配置文件中
            FSDirectory directory = FSDirectory.Open(new DirectoryInfo(indexPath), new NativeFSLockFactory()); //指定索引文件(打开索引目录) FS指的是就是FileSystem
            bool        isUpdate  = IndexReader.IndexExists(directory);                                        //IndexReader:对索引进行读取的类。该语句的作用:判断索引库文件夹是否存在以及索引特征文件是否存在。

            if (isUpdate)
            {
                //同时只能有一段代码对索引库进行写操作。当使用IndexWriter打开directory时会自动对索引库文件上锁。
                //如果索引目录被锁定(比如索引过程中程序异常退出),则首先解锁(提示一下:如果我现在正在写着已经加锁了,但是还没有写完,这时候又来一个请求,那么不就解锁了吗?这个问题后面会解决)
                if (IndexWriter.IsLocked(directory))
                {
                    IndexWriter.Unlock(directory);
                }
            }
            IndexWriter    writer = new IndexWriter(directory, new PanGuAnalyzer(), !isUpdate, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);//向索引库中写索引。这时在这里加锁。
            List <Article> list   = articleService.LoadEntities(t => true).ToList();

            foreach (var item in list)
            {
                Document document = new Document();//表示一篇文档。
                //Field.Store.YES:表示是否存储原值。只有当Field.Store.YES在后面才能用doc.Get("number")取出值来.Field.Index. NOT_ANALYZED:不进行分词保存
                document.Add(new Field("Id", item.ArticleId.ToString(), Field.Store.YES, Field.Index.NOT_ANALYZED));

                //Field.Index. ANALYZED:进行分词保存:也就是要进行全文的字段要设置分词 保存(因为要进行模糊查询)

                //Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS:不仅保存分词还保存分词的距离。
                document.Add(new Field("Title", item.Title, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                document.Add(new Field("Content", item.Content, Field.Store.YES, Field.Index.ANALYZED, Lucene.Net.Documents.Field.TermVector.WITH_POSITIONS_OFFSETS));
                writer.AddDocument(document);
            }


            writer.Dispose();    //会自动解锁。
            directory.Dispose(); //不要忘了Close,否则索引结果搜不到
        }
 public void Dispose()
 {
     _indexDir?.Dispose();
     _searcher?.Dispose();
     _analyzer?.Dispose();
 }
Пример #28
0
 public void Dispose()
 {
     indexReader?.Dispose();
     indexDirectory?.Dispose();
 }
Пример #29
0
 public void Dispose()
 {
     _writer?.Dispose();
     _directory?.Dispose();
 }
Пример #30
0
 public override void TearDown()
 {
     inputDir?.Dispose();
     inputDir = null; // LUCENENET specific
     base.TearDown();
 }