Close() public method

public Close ( ) : void
return void
Ejemplo n.º 1
0
        /// <summary>
        /// Gets the similar words.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="word">The word.</param>
        /// <returns></returns>
        private static string[] SuggestSimilar(IndexReader reader, string fieldName, string word)
        {
            var spell = new SpellChecker.Net.Search.Spell.SpellChecker(reader.Directory());

            spell.IndexDictionary(new LuceneDictionary(reader, fieldName));
            var similarWords = spell.SuggestSimilar(word, 2);

            // now make sure to close the spell checker
            spell.Close();

            return(similarWords);
        }
Ejemplo n.º 2
0
        public SuggestionQueryResult ExecuteSuggestionQuery(string indexName, SuggestionQuery suggestionQuery)
        {
            if (suggestionQuery == null) throw new ArgumentNullException("suggestionQuery");
            if (string.IsNullOrWhiteSpace(suggestionQuery.Term)) throw new ArgumentNullException("suggestionQuery.Term");
            if (string.IsNullOrWhiteSpace(indexName)) throw new ArgumentNullException("indexName");
            if (string.IsNullOrWhiteSpace(suggestionQuery.Field)) throw new ArgumentNullException("suggestionQuery.Field");
            if (suggestionQuery.MaxSuggestions <= 0) suggestionQuery.MaxSuggestions = 10;
            if (suggestionQuery.Accuracy <= 0 || suggestionQuery.Accuracy > 1) suggestionQuery.Accuracy = 0.5f;

            suggestionQuery.MaxSuggestions = Math.Min(suggestionQuery.MaxSuggestions,
                                                      _database.Configuration.MaxPageSize);

            var currentSearcher = _database.IndexStorage.GetCurrentIndexSearcher(indexName);
            IndexSearcher searcher;
            using(currentSearcher.Use(out searcher))
            {
                var indexReader = searcher.GetIndexReader();

                var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(new RAMDirectory(), GetStringDistance(suggestionQuery));
                try
                {
                    spellChecker.IndexDictionary(new LuceneDictionary(indexReader, suggestionQuery.Field));
                    spellChecker.SetAccuracy(suggestionQuery.Accuracy);

                    var suggestions = spellChecker.SuggestSimilar(suggestionQuery.Term, 
                        suggestionQuery.MaxSuggestions,
                        indexReader,
                        suggestionQuery.Field, 
                        true);

                    return new SuggestionQueryResult
                    {
                        Suggestions = suggestions
                    };
                }
                finally
                {
                    spellChecker.Close();
                    // this is really stupid, but it doesn't handle this in its close method!
                    GC.SuppressFinalize(spellChecker);
                }
            }
            
        }
 public void Dispose()
 {
     spellChecker.Close();
     GC.SuppressFinalize(spellChecker);//stupid! but it doesn't do it on its own
 }
        /// <summary>
        /// Gets the similar words.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="fieldName">Name of the field.</param>
        /// <param name="word">The word.</param>
        /// <returns></returns>
        private string[] SuggestSimilar(IndexReader reader, string fieldName, string word)
        {
            var spell = new SpellChecker(reader.Directory());
            spell.IndexDictionary(new LuceneDictionary(reader, fieldName));
            var similarWords = spell.SuggestSimilar(word, 2);

            // now make sure to close the spell checker
            spell.Close();

            return similarWords;
        }
Ejemplo n.º 5
0
        public virtual long CreateIndex()
        {
            try
            {
                var watch = new System.Diagnostics.Stopwatch();
                watch.Start();

                if (System.IO.Directory.Exists(_indexFilesPath))
                {
                    try
                    {
                        using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                            directory.ClearLock(directory.GetLockId());
                    }
                    catch
                    {
                    }

                    FileUtils.DeleteDirRecursively(new DirectoryInfo(_indexFilesPath));
                }

                System.IO.Directory.CreateDirectory(_indexFilesPath);

                if (System.IO.Directory.Exists(_spellFilesPath))
                {
                    try
                    {
                        using (var directory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath)))
                            directory.ClearLock(directory.GetLockId());
                    }
                    catch
                    {
                    }

                    FileUtils.DeleteDirRecursively(new DirectoryInfo(_spellFilesPath));
                }

                System.IO.Directory.CreateDirectory(_spellFilesPath);


                var allPosts = _postService.GetAsQueryable().Where(p => p.Published)
                               .Include(p => p.Descriptions)
                               .Include(p => p.Tags)
                               .Include(p => p.Categories);
                var languages = _languagesService.GetAsEnumerable();

                var analyzer = new StandardAnalyzer(Version);
                using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                {
                    using (var writer =
                               new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED))
                    {
                        foreach (var post in allPosts)
                        {
                            writer.AddDocument(MapPost(post, post.PostType));//Default values

                            foreach (var language in languages.OrderByDescending(p => p.IsDefault))
                            {
                                var localizedMap = MapPost(post, language, post.PostType);
                                if (localizedMap != null)
                                {
                                    writer.AddDocument(localizedMap);    //Localized values
                                }
                            }
                        }

                        writer.Optimize();
                        writer.Commit();
                    }
                }

                using (var directory = FSDirectory.Open(new DirectoryInfo(_indexFilesPath)))
                {
                    using (var spellDirectory = FSDirectory.Open(new DirectoryInfo(_spellFilesPath)))
                    {
                        using (var indexReader = IndexReader.Open(directory, readOnly: true))
                        {
                            using (var spellChecker = new SpellChecker.Net.Search.Spell.SpellChecker(spellDirectory))
                            {
                                // Create SpellChecker Index
                                spellChecker.ClearIndex();
                                spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Title"));
                                spellChecker.IndexDictionary(new LuceneDictionary(indexReader, "Description"));

                                spellChecker.Close();
                            }
                        }
                    }
                }

                watch.Stop();

                MethodCache.ExpireTag(CacheTags.Search);

                return(watch.ElapsedMilliseconds);
            }
            catch (Exception e)
            {
                MethodCache.ExpireTag(CacheTags.Search);
                _eventPublisher.Publish(new CreateSearchIndexesFailEvent(e));
                throw;
            }
        }