示例#1
0
        protected virtual Directory CreateIndex()
        {
            var index = new RAMDirectory();

            var indexWriterAnalyzer = CreateAnalyzer();
            var config = IndexUtils.CreateWriterConfig(indexWriterAnalyzer);

            using (var writer = new IndexWriter(index, config))
            {
                void indexDocumentGroup(IEnumerable <Document> documents)
                {
                    foreach (var doc in documents)
                    {
                        writer.AddDocument(doc);
                    }

                    Interlocked.Increment(ref GroupsAddedToIndex);
                    IndexingProgress?.Invoke();
                }

                IndexUtils.ForEach(GetDocumentGroupsToIndex(), indexDocumentGroup);

                writer.Flush(triggerMerge: true, applyAllDeletes: false);
                writer.Commit();
            }

            return(index);
        }
示例#2
0
        public Directory CreateIndex()
        {
            if (IsLoading || IsLoaded)
            {
                throw new InvalidOperationException();
            }

            IsLoading = true;

            var index = new RAMDirectory();

            var indexWriterAnalyzer = Adapter.CreateAnalyzer();
            var config = IndexUtils.CreateWriterConfig(indexWriterAnalyzer);

            using (var writer = new IndexWriter(index, config))
            {
                void indexDocumentGroup(IEnumerable <Document> documents)
                {
                    if (_aborted)
                    {
                        return;
                    }

                    foreach (var doc in documents)
                    {
                        if (_aborted)
                        {
                            return;
                        }

                        writer.AddDocument(doc);
                    }

                    Interlocked.Increment(ref GroupsAddedToIndex);
                    IndexingProgress?.Invoke();
                }

                IndexUtils.ForEach(GetDocumentGroupsToIndex(), indexDocumentGroup);

                if (_aborted)
                {
                    return(null);
                }

                writer.Flush(triggerMerge: true, applyAllDeletes: false);
                writer.Commit();
            }

            IsLoading = false;

            return(index);
        }
示例#3
0
        public Directory CreateIndex()
        {
            if (IsLoaded)
            {
                throw new InvalidOperationException();
            }

            IsLoading = true;

            var index = new RAMDirectory();

            IReadOnlyList <(string UserField, string Language)> tasks =
                _adapter.GetUserFields()
                .Where(_adapter.IsIndexedInSpellchecker)
                .SelectMany(f => _adapter.GetFieldLanguages(f).Select(l => (f, l)))
                .ToReadOnlyList();

            TotalFields = tasks.Count;
            var indexedWordsByField = new Dictionary <string, HashSet <string> >(Str.Comparer);

            void getContentToIndex((string UserField, string Language) task)
            {
                var values = _adapter.IsStoredInSpellchecker(task.UserField, task.Language)
                                        ? GetObjectsToIndex().SelectMany(c =>
                                                                         _adapter.GetSpellcheckerValues(c, task.UserField, task.Language))
                                        : GetValuesCache(task.UserField, task.Language);

                var spellcheckerField = _adapter.GetSpellcheckerFieldIn(task.UserField, task.Language);

                HashSet <string> indexedWords;

                lock (indexedWordsByField)
                {
                    if (!indexedWordsByField.TryGetValue(spellcheckerField, out indexedWords))
                    {
                        indexedWords = new HashSet <string>(Str.Comparer);
                        indexedWordsByField.Add(spellcheckerField, indexedWords);
                    }
                }

                lock (indexedWords)
                    foreach (string value in values)
                    {
                        indexedWords.Add(value);
                    }
            }

            IndexUtils.ForEach(tasks, getContentToIndex);

            TotalFields    = indexedWordsByField.Count;
            _indexedFields = 0;

            _spellchecker.BeginIndex(index);

            IndexUtils.ForEach(
                indexedWordsByField,
                indexField,
                suppressParallelism: true);

            if (_abort)
            {
                return(null);
            }

            _spellchecker.EndIndex();

            IsLoading = false;
            IndexingProgress?.Invoke();

            IsLoaded = true;

            return(index);
        }