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); }
public void Dispose() { // Spellchecker first, because it uses _index Spellchecker.Dispose(); State?.Dispose(); Disposed?.Invoke(); }
internal void LoadIndex(DirectoryReader indexReader) { _reader = indexReader; Spellchecker = new Spellchecker(_adapter.IsAnyField); LoadSpellcheckerIndex(); IsLoaded = true; }
public void LoadIndexes() { var newState = CreateState(); LoadIndex(newState); if (newState.IsLoaded) { Spellchecker.LoadIndex(newState); } }
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(); }
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; }
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()); }
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)); }
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); }
protected abstract LuceneSpellcheckerState <TId, TDoc> CreateState(LuceneSearcherState <TId, TDoc> searcherState, Spellchecker spellchecker, bool loaded);