Esempio n. 1
0
        protected virtual IReadOnlyList <string> GetValuesCache(string userField, string lang)
        {
            var spellcheckerField = _adapter.GetSpellcheckerFieldIn(userField, lang);
            IReadOnlyList <string> values;

            lock (_valuesCache)
                if (_valuesCache.TryGetValue(spellcheckerField, out values))
                {
                    return(values);
                }

            values = _adapter.IsStoredInSpellchecker(userField, lang)
                                ? Spellchecker?.ReadAllValuesFrom(discriminant: spellcheckerField)
                                : _reader?.Invoke(readAllValuesFrom, spellcheckerField);

            if (values == null)
            {
                return(ReadOnlyList.Empty <string>());
            }

            lock (_valuesCache)
                _valuesCache[spellcheckerField] = values;

            return(values);
        }
Esempio n. 2
0
        public void Dispose()
        {
            // Spellchecker first, because it uses _index
            Spellchecker.Dispose();
            State?.Dispose();

            Disposed?.Invoke();
        }
Esempio n. 3
0
        internal void LoadIndex(DirectoryReader indexReader)
        {
            _reader = indexReader;

            Spellchecker = new Spellchecker(_adapter.IsAnyField);
            LoadSpellcheckerIndex();

            IsLoaded = true;
        }
Esempio n. 4
0
        public void LoadIndexes()
        {
            var newState = CreateState();

            LoadIndex(newState);

            if (newState.IsLoaded)
            {
                Spellchecker.LoadIndex(newState);
            }
        }
Esempio n. 5
0
        protected virtual void IndexField(KeyValuePair <string, HashSet <string> > pair)
        {
            var words = pair.Value.OrderBy(Str.Comparer).ToReadOnlyList();

            foreach (string word in words)
            {
                Spellchecker.IndexWord(pair.Key, word);
            }

            Interlocked.Increment(ref _indexedFields);
            IndexingProgress?.Invoke();
        }
Esempio n. 6
0
        protected LuceneSpellcheckerState(
            Spellchecker spellchecker,
            LuceneSearcherState <TId, TDoc> searcherState,
            IDocumentAdapter <TId, TDoc> adapter,
            Func <int> maxCount,
            bool loaded)
        {
            _spellchecker = spellchecker;
            _reader       = searcherState.Reader;
            _adapter      = adapter;
            _maxCount     = maxCount;

            IsLoaded = loaded;
        }
Esempio n. 7
0
        private IReadOnlyList <string> suggestAllFieldValues(string value, string language)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException($"empty {nameof(value)}", nameof(value));
            }

            var numericValues = new HashSet <string>();

            bool valueIsInt   = value.IsInt();
            bool valueIsFloat = value.IsFloat();

            if (valueIsInt || valueIsFloat)
            {
                foreach (var userField in _adapter.GetUserFields())
                {
                    bool matchesNumericType = _adapter.IsFloatField(userField) || _adapter.IsIntField(userField) && valueIsInt;

                    if (!matchesNumericType)
                    {
                        continue;
                    }

                    var cache          = GetValuesCache(userField, language);
                    var similarNumbers = getNumericallySimilarValues(cache, value);
                    numericValues.UnionWith(similarNumbers);
                }
            }

            var enumerable = numericValues
                             .OrderBy(Str.Comparer)
                             .Take(MaxCount);

            if (IsLoaded)
            {
                var spellcheckerValues = Spellchecker.SuggestSimilar(null, value, MaxCount);

                enumerable = enumerable
                             .Concat(spellcheckerValues.Where(v => !numericValues.Contains(v)))
                             .ToReadOnlyList();
            }

            return(enumerable.ToReadOnlyList());
        }
Esempio n. 8
0
        private IReadOnlyList <string> suggestValues(string userField, string language, string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                var cache = GetValuesCache(userField, language);
                return(new ListSegment <string>(cache, 0, MaxCount));
            }

            if (_adapter.IsNumericField(userField))
            {
                var cache = GetValuesCache(userField, language);
                return(getNumericallySimilarValues(cache, value).Take(MaxCount).ToReadOnlyList());
            }

            if (!IsLoaded)
            {
                return(ReadOnlyList.Empty <string>());
            }

            var spellcheckerField = _adapter.GetSpellcheckerFieldIn(userField, language);

            return(Spellchecker.SuggestSimilar(spellcheckerField, value, MaxCount));
        }
Esempio n. 9
0
        protected virtual Directory LoadSpellcheckerIndex()
        {
            var index = new RAMDirectory();

            IsLoading = true;

            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);
                    }

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

            IndexUtils.ForEach(tasks, getContentToIndex);

            TotalFields    = indexedWordsByField.Count;
            _indexedFields = 0;

            Spellchecker.BeginIndex(index);

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

            Spellchecker.EndIndex();

            IsLoading = false;
            IndexingProgress?.Invoke();

            return(index);
        }
Esempio n. 10
0
 protected abstract LuceneSpellcheckerState <TId, TDoc> CreateState(LuceneSearcherState <TId, TDoc> searcherState, Spellchecker spellchecker, bool loaded);