コード例 #1
0
        public bool IndexExist()
        {
            using var dir = FSDirectory.Open(musicConfiguration.IndexPath);
            var exist = DirectoryReader.IndexExists(dir);

            return(exist);
        }
コード例 #2
0
 public virtual void ResetIndexDirectory()
 {
     try
     {
         string folderPath = ConfigurationManager.AppSettings["lucene:BlobConnectionString"];
         var    serverPath = HttpRuntime.AppDomainAppPath;
         folderPath = serverPath + folderPath;
         var fsDirectory = FSDirectory.Open(folderPath);
         if (IndexWriter.IsLocked(fsDirectory))
         {
             IndexWriter.Unlock(fsDirectory);
         }
         string[] files = System.IO.Directory.GetFiles(folderPath);
         foreach (string file in files)
         {
             File.Delete(file);
         }
         if (!DirectoryReader.IndexExists(fsDirectory))
         {
             using (new IndexWriter(fsDirectory, new StandardAnalyzer(LuceneConfiguration.LuceneVersion), true, IndexWriter.MaxFieldLength.UNLIMITED))
             {
             }
         }
     }
     catch (Exception ex)
     {
         _logger.Error("ResetIndexDirectory error", ex);
     }
 }
コード例 #3
0
        public void VerifyThatInitializationWorks()
        {
            Assert.IsNotNull(this.IndexDirectory);
            Assert.IsNotNull(this.RamIndexDirectory);
            Assert.IsNotNull(this.Analyzer);
            Assert.IsNotNull(this.IndexWriter);
            Assert.IsNotNull(this.IndexWriterSuggest);
            Assert.IsNotNull(this.SearcherManager);
            Assert.IsNotNull(this.QueryParser);

            // Check that indexes exist.
            Assert.IsTrue(DirectoryReader.IndexExists(this.IndexDirectory));
            Assert.IsTrue(DirectoryReader.IndexExists(this.RamIndexDirectory));

            // Check all files have been indexed in the file system directory and there is no doubles.
            var readerFsd = this.IndexWriter.Reader;

            Assert.That(this.IndexWriter.NumDocs(), Is.EqualTo(5));
            Assert.That(readerFsd.GetDocCount("name"), Is.EqualTo(5));
            Assert.That(readerFsd.GetDocCount("path"), Is.EqualTo(5));

            // Check all file names and file paths have been indexed in the ram directory and there is no doubles.
            var readerRam = this.IndexWriterSuggest.Reader;

            Assert.That(this.IndexWriterSuggest.NumDocs(), Is.EqualTo(5));
            Assert.That(readerRam.GetDocCount("name"), Is.EqualTo(5));
            Assert.That(readerRam.GetDocCount("path"), Is.EqualTo(5));

            this.documentationSearches.IndexWriter.Dispose();
        }
コード例 #4
0
        /// <summary>
        /// Constructor with the given index directory and callback to notify when the indexes were updated.
        /// </summary>
        /// <exception cref="System.IO.IOException"></exception>
        public IndexAndTaxonomyReplicationHandler(Directory indexDirectory, Directory taxonomyDirectory, Func <bool?> callback)
        {
            this.indexDirectory    = indexDirectory;
            this.taxonomyDirectory = taxonomyDirectory;
            this.callback          = callback;

            currentVersion       = null;
            currentRevisionFiles = null;

            bool indexExists    = DirectoryReader.IndexExists(indexDirectory);
            bool taxonomyExists = DirectoryReader.IndexExists(taxonomyDirectory);

            if (indexExists != taxonomyExists)
            {
                throw new InvalidOperationException(string.Format("search and taxonomy indexes must either both exist or not: index={0} taxo={1}", indexExists, taxonomyExists));
            }

            if (indexExists)
            {
                IndexCommit indexCommit    = IndexReplicationHandler.GetLastCommit(indexDirectory);
                IndexCommit taxonomyCommit = IndexReplicationHandler.GetLastCommit(taxonomyDirectory);

                currentRevisionFiles = IndexAndTaxonomyRevision.RevisionFiles(indexCommit, taxonomyCommit);
                currentVersion       = IndexAndTaxonomyRevision.RevisionVersion(indexCommit, taxonomyCommit);

                WriteToInfoStream(
                    string.Format("constructor(): currentVersion={0} currentRevisionFiles={1}", currentVersion, currentRevisionFiles),
                    string.Format("constructor(): indexCommit={0} taxoCommit={1}", indexCommit, taxonomyCommit));
            }
        }
コード例 #5
0
        public static BoboMultiReader NewIndexReader(Directory idxDir)
        {
            if (!DirectoryReader.IndexExists(idxDir))
            {
                return(null);
            }

            long start = System.Environment.TickCount;

            DirectoryReader directoryReader = DirectoryReader.Open(idxDir);
            BoboMultiReader reader;

            try
            {
                reader = BoboMultiReader.GetInstance(directoryReader);
            }
            catch (IOException ioe)
            {
                throw ioe;
            }
            finally
            {
                directoryReader.Dispose();
            }

            long end = System.Environment.TickCount;

            if (logger.IsDebugEnabled())
            {
                logger.Debug("New index loading took: " + (end - start));
            }

            return(reader);
        }
コード例 #6
0
 public IndexReadyCallback(Directory indexDir)
 {
     this.indexDir = indexDir;
     if (DirectoryReader.IndexExists(indexDir))
     {
         reader         = DirectoryReader.Open(indexDir);
         lastGeneration = reader.IndexCommit.Generation;
     }
 }
 private static void InitDirectory(Directory directory)
 {
     if (!DirectoryReader.IndexExists(directory))
     {
         using (new IndexWriter(_directory, new StandardAnalyzer(LuceneVersion), true, IndexWriter.MaxFieldLength.UNLIMITED))
         {
         }
     }
 }
コード例 #8
0
        public static bool IndexExists(string luceneIndex)
        {
            luceneIndex.RequireNotNullOrEmpty(nameof(luceneIndex));

            using var dir = FSDirectory.Open(luceneIndex);
            var indexExist = DirectoryReader.IndexExists(dir);

            return(indexExist);
        }
コード例 #9
0
        public bool IndexExistsOrEmpty()
        {
            if (!DirectoryReader.IndexExists(_directory))
            {
                return(false);
            }

            return(DirectoryReader.Open(_directory).NumDocs > 0);
        }
コード例 #10
0
ファイル: ParrotEyes.cs プロジェクト: vbouzoukos/parrotlucene
        public ParrotEyes(Wings wings, string indexname) : base(wings, indexname)
        {
            if (!DirectoryReader.IndexExists(LuceneDirectory))
            {
                throw new NoIndexException(IndexName, LuceneDirectory.Directory.FullName);
            }

            indexReader = DirectoryReader.Open(LuceneDirectory);
            searcher    = new IndexSearcher(indexReader);
        }
コード例 #11
0
        public void Ini()
        {
            if (UseAzureDirectory)
            {
                var azureBlobConnectionString   = config.GetValue <string>("AzureDirectoryConnectionString");
                var azureDirectoryCachePath     = ReplacePathTokens(config.GetValue <string>("AzureDirectoryCachePath"));
                var azureDirectoryContainerName = config.GetValue <string>("AzureDirectoryContainerName");
                //if empty string, ensure value is null
                if (string.IsNullOrEmpty(azureDirectoryContainerName))
                {
                    azureDirectoryContainerName = null;
                }
                var cloudStorageAccount = CloudStorageAccount.Parse(azureBlobConnectionString);
                Directory = new AzureDirectory(cloudStorageAccount, azureDirectoryCachePath, containerName: azureDirectoryContainerName);
            }
            else
            {
                if (!System.IO.Directory.Exists(INDEXPATH))
                {
                    System.IO.Directory.CreateDirectory(INDEXPATH);
                }
                Directory = FSDirectory.Open(INDEXPATH);
            }
            bool create = !DirectoryReader.IndexExists(Directory);

            lock (write_lock)
            {
                try
                {
                    if (CanWrite)
                    {
                        SetWriter(create);
                        if (Writer != null && !UseAzureDirectory)
                        {
                            CloseWriter();
                        }
                        //Writer.Optimize();
                    }
                }
                catch (Lucene.Net.Store.LockObtainFailedException ex)
                {
                    logger.Log(ex);
                }
                catch (Exception ex)
                {
                    throw;
                    //logger.Log(ex);
                }
                finally
                {
                    //CloseWriter();
                }
            }
            SetSearcher();
        }
コード例 #12
0
 protected override void Dispose(bool disposing)
 {
     if (disposing)
     {
         isOpen = false;
         if (checkIndexOnClose && DirectoryReader.IndexExists(this))
         {
             TestUtil.CheckIndex(this, crossCheckTermVectorsOnClose);
         }
         base.Dispose(disposing);
     }
 }
コード例 #13
0
        private static SearcherManager BuildSearcherManagerReader(Directory directory, Analyzer analyzer)
        {
            if (!DirectoryReader.IndexExists(directory))
            {
                // this index doesn't exist... make it!
                using (new IndexWriter(directory, new IndexWriterConfig(LeoLuceneVersion.Version, analyzer)
                {
                    OpenMode = OpenMode.CREATE_OR_APPEND
                })) { }
            }

            return(new SearcherManager(directory, null));
        }
コード例 #14
0
 public IndexAndTaxonomyReadyCallback(MockDirectoryWrapper indexDir, MockDirectoryWrapper taxoDir)
 {
     this.indexDir = indexDir;
     this.taxoDir = taxoDir;
     config = new FacetsConfig();
     config.SetHierarchical("A", true);
     if (DirectoryReader.IndexExists(indexDir))
     {
         indexReader = DirectoryReader.Open(indexDir);
         lastIndexGeneration = indexReader.IndexCommit.Generation;
         taxoReader = new DirectoryTaxonomyReader(taxoDir);
     }
 }
コード例 #15
0
ファイル: BaseSearchService.cs プロジェクト: kissxrl/dht-1
        private static Directory GetIndexDirectory(string subIndex)
        {
            var directory = FSDirectory.Open(subIndex);

            if (!DirectoryReader.IndexExists(directory))//判断是否存在索引文件夹
            {
                System.IO.Directory.CreateDirectory(subIndex);
            }
            if (IndexWriter.IsLocked(directory))//判断是否被锁定,如果锁定则解锁
            {
                IndexWriter.Unlock(directory);
            }
            return(directory);
        }
コード例 #16
0
ファイル: YafSearch.cs プロジェクト: sigeshitou/YAFNET
        /// <summary>
        /// The get searcher.
        /// </summary>
        /// <returns>
        /// The <see cref="IndexSearcher"/>.
        /// </returns>
        private IndexSearcher GetSearcher()
        {
            if (!DirectoryReader.IndexExists(FSDirectory.Open(SearchIndexFolder)))
            {
                return(null);
            }

            this.searcherManager = this.indexWriter != null
                                       ? new SearcherManager(this.indexWriter, false, null)
                                       : new SearcherManager(FSDirectory.Open(SearchIndexFolder), null);

            this.searcherManager.MaybeRefreshBlocking();

            return(this.searcherManager.Acquire());
        }
コード例 #17
0
 /// <summary>
 /// Returns the last <see cref="IndexCommit"/> found in the <see cref="Directory"/>, or
 /// <c>null</c> if there are no commits.
 /// </summary>
 /// <exception cref="IOException"></exception>
 public static IndexCommit GetLastCommit(Directory directory)
 {
     try
     {
         // IndexNotFoundException which we handle below
         return(DirectoryReader.IndexExists(directory)
             ? DirectoryReader.ListCommits(directory).Last()
             : null);
     }
     catch (IndexNotFoundException)
     {
         // ignore the exception and return null
     }
     return(null);
 }
コード例 #18
0
        public Task DeleteAll()
        {
            if (_isDisposed)
            {
                throw new ObjectDisposedException("LuceneIndex");
            }

            if (DirectoryReader.IndexExists(_directory))
            {
                var writer = GetWriter();
                writer.DeleteAll();
            }

            return(Task.FromResult(0));
        }
コード例 #19
0
        /// <summary>
        /// Sets a different index as the spell checker index or re-open
        /// the existing index if <code>spellIndex</code> is the same value
        /// as given in the constructor. </summary>
        /// <param name="spellIndexDir"> the spell directory to use </param>
        /// <exception cref="ObjectDisposedException"> if the Spellchecker is already closed </exception>
        /// <exception cref="IOException"> if spellchecker can not open the directory </exception>
        // TODO: we should make this final as it is called in the constructor
        public virtual void SetSpellIndex(Directory spellIndexDir)
        {
            // this could be the same directory as the current spellIndex
            // modifications to the directory should be synchronized
            lock (modifyCurrentIndexLock)
            {
                EnsureOpen();
                if (!DirectoryReader.IndexExists(spellIndexDir))
                {
#pragma warning disable 612, 618
                    using var writer = new IndexWriter(spellIndexDir, new IndexWriterConfig(LuceneVersion.LUCENE_CURRENT, null));
#pragma warning restore 612, 618
                }
                SwapSearcher(spellIndexDir);
            }
        }
コード例 #20
0
 /// <summary>
 /// Deletes all
 /// </summary>
 /// <returns>True if operation was successful</returns>
 public bool ClearLuceneIndex()
 {
     if (DirectoryReader.IndexExists(LuceneDirectory))
     {
         try
         {
             // remove older index entries
             writer.DeleteAll();
             writer.Commit();
         }
         catch (Exception)
         {
             return(false);
         }
     }
     return(true);
 }
コード例 #21
0
 /// <summary>
 /// Returns the last <see cref="IndexCommit"/> found in the <see cref="Directory"/>, or
 /// <c>null</c> if there are no commits.
 /// </summary>
 /// <exception cref="IOException"></exception>
 public static IndexCommit GetLastCommit(Directory directory)
 {
     try
     {
         // IndexNotFoundException which we handle below
         if (DirectoryReader.IndexExists(directory))
         {
             var commits = DirectoryReader.ListCommits(directory);
             return(commits[commits.Count - 1]);
         }
     }
     catch (IndexNotFoundException)
     {
         // ignore the exception and return null
     }
     return(null);
 }
コード例 #22
0
        public virtual void TestCommit()
        {
            // Verifies that nothing is committed to the underlying Directory, if
            // commit() wasn't called.
            Directory dir = NewDirectory();
            var       ltw = new DirectoryTaxonomyWriter(dir, OpenMode.CREATE_OR_APPEND, NO_OP_CACHE);

            Assert.False(DirectoryReader.IndexExists(dir));
            ltw.Commit(); // first commit, so that an index will be created
            ltw.AddCategory(new FacetLabel("a"));

            IndexReader r = DirectoryReader.Open(dir);

            Assert.AreEqual(1, r.NumDocs, "No categories should have been committed to the underlying directory");
            r.Dispose();
            ltw.Dispose();
            dir.Dispose();
        }
コード例 #23
0
        public HashSet <String> GetAllValuesByField(string field_name)
        {
            HashSet <String> uniqueTerms = new HashSet <string>();

            if (DirectoryReader.IndexExists(indexDirectory))
            {
                var reader = DirectoryReader.Open(indexDirectory);
                for (int i = 0; i < reader.MaxDoc; i++)
                {
                    HashSet <string> field_names = new HashSet <string>();
                    field_names.Add(field_name);
                    Document doc         = reader.Document(i, field_names);
                    String   field_value = doc.Get(field_name);
                    uniqueTerms.Add(field_value);
                }
            }

            return(uniqueTerms);
        }
コード例 #24
0
        /// <summary>
        /// Creates the index for a given type if it doesn't exist
        /// </summary>
        /// <param name="indexName"></param>
        /// <returns></returns>
        public Task CreateIndexIfNotExists(string indexName)
        {
            bool exists = DirectoryReader.IndexExists(GetDirectory(indexName));

            if (exists)
            {
                return(Task.FromResult(0));
            }

            return(Task.Run(() =>
            {
                using (var analyzer = new StandardAnalyzer(_luceneConfig.LuceneVersion))
                {
                    var config = new IndexWriterConfig(_luceneConfig.LuceneVersion, analyzer);
                    using (new IndexWriter(GetDirectory(indexName), config))
                    {
                    }
                }
            }));
        }
コード例 #25
0
        /// <summary>
        /// Create a new instance, loading from a previously built
        ///  AnalyzingInfixSuggester directory, if it exists.  This directory must be
        ///  private to the infix suggester (i.e., not an external
        ///  Lucene index).  Note that <seealso cref="#close"/>
        ///  will also close the provided directory.
        /// </summary>
        ///  <param name="minPrefixChars"> Minimum number of leading characters
        ///     before PrefixQuery is used (default 4).
        ///     Prefixes shorter than this are indexed as character
        ///     ngrams (increasing index size but making lookups
        ///     faster). </param>
        public AnalyzingInfixSuggester(Version matchVersion, Directory dir, Analyzer indexAnalyzer, Analyzer queryAnalyzer, int minPrefixChars)
        {
            if (minPrefixChars < 0)
            {
                throw new System.ArgumentException("minPrefixChars must be >= 0; got: " + minPrefixChars);
            }

            this.queryAnalyzer  = queryAnalyzer;
            this.indexAnalyzer  = indexAnalyzer;
            this.matchVersion   = matchVersion;
            this.dir            = dir;
            this.minPrefixChars = minPrefixChars;

            if (DirectoryReader.IndexExists(dir))
            {
                // Already built; open it:
                writer      = new IndexWriter(dir, GetIndexWriterConfig(matchVersion, GramAnalyzer, IndexWriterConfig.OpenMode.APPEND));
                searcherMgr = new SearcherManager(writer, true, null);
            }
        }
コード例 #26
0
        /// <summary>
        /// Constructor with the given index directory and callback to notify when the
        /// indexes were updated.
        /// </summary>
        public IndexReplicationHandler(Directory indexDirectory, Func <bool?> callback) // LUCENENET TODO: API - shouldn't this be Action ?
        {
            this.InfoStream     = InfoStream.Default;
            this.callback       = callback;
            this.indexDirectory = indexDirectory;

            currentVersion       = null;
            currentRevisionFiles = null;

            if (DirectoryReader.IndexExists(indexDirectory))
            {
                IList <IndexCommit> commits = DirectoryReader.ListCommits(indexDirectory);
                IndexCommit         commit  = commits[commits.Count - 1];

                currentVersion       = IndexRevision.RevisionVersion(commit);
                currentRevisionFiles = IndexRevision.RevisionFiles(commit);

                WriteToInfoStream(
                    string.Format("constructor(): currentVersion={0} currentRevisionFiles={1}", currentVersion, currentRevisionFiles),
                    string.Format("constructor(): commit={0}", commit));
            }
        }
コード例 #27
0
 public static bool OpenIndex()
 {
     OpenDirectory();
     if (Dir != null)
     {
         if (DirectoryReader.IndexExists(Dir))
         {
             OpenAnalyzers();
             OpenReader();
             OpenParser();
             return(true);
         }
         else
         {
             return(false);
         }
     }
     else
     {
         return(false);
     }
 }
コード例 #28
0
        /// <summary>
        /// Create a new instance, loading from a previously built
        /// <see cref="AnalyzingInfixSuggester"/> directory, if it exists.  This directory must be
        /// private to the infix suggester (i.e., not an external
        /// Lucene index).  Note that <see cref="Dispose()"/>
        /// will also dispose the provided directory.
        /// </summary>
        ///  <param name="minPrefixChars"> Minimum number of leading characters
        ///     before <see cref="PrefixQuery"/> is used (default 4).
        ///     Prefixes shorter than this are indexed as character
        ///     ngrams (increasing index size but making lookups
        ///     faster). </param>
        ///  <param name="commitOnBuild"> Call commit after the index has finished building. This
        ///  would persist the suggester index to disk and future instances of this suggester can
        ///  use this pre-built dictionary. </param>
        // LUCENENET specific - LUCENE-5889, a 4.11.0 feature. (Code moved from other constructor to here.)
        public AnalyzingInfixSuggester(LuceneVersion matchVersion, Directory dir, Analyzer indexAnalyzer,
                                       Analyzer queryAnalyzer, int minPrefixChars, bool commitOnBuild)
        {
            if (minPrefixChars < 0)
            {
                throw new ArgumentOutOfRangeException("minPrefixChars must be >= 0; got: " + minPrefixChars);// LUCENENET specific - changed from IllegalArgumentException to ArgumentOutOfRangeException (.NET convention)
            }

            this.m_queryAnalyzer = queryAnalyzer;
            this.m_indexAnalyzer = indexAnalyzer;
            this.matchVersion    = matchVersion;
            this.dir             = dir;
            this.minPrefixChars  = minPrefixChars;
            this.commitOnBuild   = commitOnBuild;

            if (DirectoryReader.IndexExists(dir))
            {
                // Already built; open it:
                writer        = new IndexWriter(dir, GetIndexWriterConfig(matchVersion, GetGramAnalyzer(), OpenMode.APPEND));
                m_searcherMgr = new SearcherManager(writer, true, null);
            }
        }
コード例 #29
0
        public void Initialize(String directoryProviderName, IDictionary <string, string> properties, ISearchFactoryImplementor searchFactory)
        {
            DirectoryInfo indexDir = DirectoryProviderHelper.DetermineIndexDir(directoryProviderName, (IDictionary)properties);

            try
            {
                indexName = indexDir.FullName;
                directory = FSDirectory.Open(indexDir.FullName);

                if (DirectoryReader.IndexExists(directory))
                {
                    return;
                }

                var config = new IndexWriterConfig(LuceneVersion.LUCENE_48, new StandardAnalyzer(LuceneVersion.LUCENE_48));
                var writer = new IndexWriter(directory, config);
                writer.Dispose();
            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }
        }
コード例 #30
0
        public void Initialize(string directoryProviderName, IDictionary <string, string> properties, ISearchFactoryImplementor searchFactory)
        {
            this.properties            = properties;
            this.directoryProviderName = directoryProviderName;

            // source guessing
            source = DirectoryProviderHelper.GetSourceDirectory(Environment.SourceBase, Environment.Source, directoryProviderName, (IDictionary)properties);
            if (source == null)
            {
                throw new ArgumentException("FSMasterDirectoryProvider requires a viable source directory");
            }

            log.Debug("Source directory: " + source);
            indexDir  = DirectoryProviderHelper.DetermineIndexDir(directoryProviderName, (IDictionary)properties);
            indexName = indexDir.FullName;
            log.Debug("Index directory: " + indexDir);
            try
            {
                // NB Do we need to do this since we are passing the create flag to Lucene?
                directory = FSDirectory.Open(indexName);
                if (!DirectoryReader.IndexExists(directory))
                {
                    log.DebugFormat("Index directory not found, creating '{0}'", indexDir.FullName);
                    indexDir.Create();
                    indexName = indexDir.FullName;
                    var         config = new IndexWriterConfig(LuceneVersion.LUCENE_48, new StandardAnalyzer(LuceneVersion.LUCENE_48));
                    IndexWriter iw     = new IndexWriter(directory, config);
                    iw.Dispose();
                }
            }
            catch (IOException e)
            {
                throw new HibernateException("Unable to initialize index: " + directoryProviderName, e);
            }

            this.searchFactory = searchFactory;
        }