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"); suggestionQuery.MaxSuggestions = Math.Min(suggestionQuery.MaxSuggestions, database.Configuration.MaxPageSize); if (suggestionQuery.MaxSuggestions <= 0) suggestionQuery.MaxSuggestions = SuggestionQuery.DefaultMaxSuggestions; if (suggestionQuery.Accuracy.HasValue && (suggestionQuery.Accuracy.Value <= 0f || suggestionQuery.Accuracy.Value > 1f)) suggestionQuery.Accuracy = SuggestionQuery.DefaultAccuracy; if (suggestionQuery.Accuracy.HasValue == false) suggestionQuery.Accuracy = SuggestionQuery.DefaultAccuracy; if (suggestionQuery.Distance.HasValue == false) suggestionQuery.Distance = StringDistanceTypes.Default; var definition = database.IndexDefinitionStorage.GetIndexDefinition(indexName); var indexExtensionKey = MonoHttpUtility.UrlEncode(suggestionQuery.Field + "-" + suggestionQuery.Distance + "-" + suggestionQuery.Accuracy); var indexExtension = database.IndexStorage.GetIndexExtensionByPrefix(indexName, indexExtensionKey) as SuggestionQueryIndexExtension; IndexSearcher currentSearcher; using (database.IndexStorage.GetCurrentIndexSearcher(definition.IndexId, out currentSearcher)) { if (currentSearcher == null) { throw new InvalidOperationException("Could not find current searcher"); } var indexReader = currentSearcher.IndexReader; if (indexExtension != null) return indexExtension.Query(suggestionQuery, indexReader); var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension( database.WorkContext, Path.Combine(database.Configuration.IndexStoragePath, "Raven-Suggestions", indexName, indexExtensionKey), GetStringDistance(suggestionQuery.Distance ?? StringDistanceTypes.Default), indexReader.Directory() is RAMDirectory, suggestionQuery.Field, suggestionQuery.Accuracy ?? 0.5f); database.IndexStorage.SetIndexExtension(indexName, indexExtensionKey, suggestionQueryIndexExtension); long _; var task = Task.Factory.StartNew(() => suggestionQueryIndexExtension.Init(indexReader)); database.Tasks.AddTask(task, new TaskBasedOperationState(task), new TaskActions.PendingTaskDescription { Payload = indexName, TaskType = TaskActions.PendingTaskType.SuggestionQuery, StartTime = SystemTime.UtcNow }, out _); // wait for a bit for the suggestions to complete, but not too much (avoid IIS resets) task.Wait(15000, database.WorkContext.CancellationToken); return suggestionQueryIndexExtension.Query(suggestionQuery, indexReader); } }
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 indexExtensionKey = suggestionQuery.Field + "/" + suggestionQuery.Distance + "/" + suggestionQuery.Accuracy; var indexExtension = _database.IndexStorage.GetIndexExtension(indexName, indexExtensionKey) as SuggestionQueryIndexExtension; if (indexExtension != null) return indexExtension.Query(suggestionQuery); IndexSearcher currentSearcher; using(_database.IndexStorage.GetCurrentIndexSearcher(indexName,out currentSearcher)) { var indexReader = currentSearcher.GetIndexReader(); var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension(GetStringDistance(suggestionQuery), suggestionQuery.Field, suggestionQuery.Accuracy); suggestionQueryIndexExtension.Init(indexReader); _database.IndexStorage.SetIndexExtension(indexName, indexExtensionKey, suggestionQueryIndexExtension); return suggestionQueryIndexExtension.Query(suggestionQuery); } }
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 indexExtensionKey = MonoHttpUtility.UrlEncode(suggestionQuery.Field + "-" + suggestionQuery.Distance + "-" + suggestionQuery.Accuracy); var indexExtension = _database.IndexStorage.GetIndexExtension(indexName, indexExtensionKey) as SuggestionQueryIndexExtension; IndexSearcher currentSearcher; using (_database.IndexStorage.GetCurrentIndexSearcher(indexName, out currentSearcher)) { var indexReader = currentSearcher.IndexReader; if (indexExtension != null) { return(indexExtension.Query(suggestionQuery, indexReader)); } var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension( Path.Combine(_database.Configuration.IndexStoragePath, "Raven-Suggestions", indexName, indexExtensionKey), indexReader, GetStringDistance(suggestionQuery.Distance), suggestionQuery.Field, suggestionQuery.Accuracy); suggestionQueryIndexExtension.Init(indexReader); _database.IndexStorage.SetIndexExtension(indexName, indexExtensionKey, suggestionQueryIndexExtension); return(suggestionQueryIndexExtension.Query(suggestionQuery, indexReader)); } }
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 indexExtensionKey = suggestionQuery.Field + "/" + suggestionQuery.Distance + "/" + suggestionQuery.Accuracy; var indexExtension = _database.IndexStorage.GetIndexExtension(indexName, indexExtensionKey) as SuggestionQueryIndexExtension; if (indexExtension != null) { return(indexExtension.Query(suggestionQuery)); } var currentSearcher = _database.IndexStorage.GetCurrentIndexSearcher(indexName); try { var indexReader = currentSearcher.GetIndexReader(); var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension(GetStringDistance(suggestionQuery), suggestionQuery.Field, suggestionQuery.Accuracy); suggestionQueryIndexExtension.Init(indexReader); _database.IndexStorage.SetIndexExtension(indexName, indexExtensionKey, suggestionQueryIndexExtension); return(suggestionQueryIndexExtension.Query(suggestionQuery)); } finally { currentSearcher.GetIndexReader().DecRef(); } }
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 indexExtensionKey = MonoHttpUtility.UrlEncode(suggestionQuery.Field + "-" + suggestionQuery.Distance + "-" + suggestionQuery.Accuracy); var indexExtension = ( database.IndexStorage.GetIndexExtension(indexName, indexExtensionKey) ?? database.IndexStorage.GetIndexExtensionByPrefix(indexName, MonoHttpUtility.UrlEncode(suggestionQuery.Field +"-"+suggestionQuery.Distance)) ?? database.IndexStorage.GetIndexExtensionByPrefix(indexName, MonoHttpUtility.UrlEncode(suggestionQuery.Field)) ) as SuggestionQueryIndexExtension; IndexSearcher currentSearcher; using (database.IndexStorage.GetCurrentIndexSearcher(indexName, out currentSearcher)) { if (currentSearcher == null) { throw new InvalidOperationException("Could not find current searcher"); } var indexReader = currentSearcher.IndexReader; if (indexExtension != null) return indexExtension.Query(suggestionQuery, indexReader); var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension( database.WorkContext, Path.Combine(database.Configuration.IndexStoragePath, "Raven-Suggestions", indexName, indexExtensionKey), indexReader.Directory() is RAMDirectory, GetStringDistance(suggestionQuery.Distance), suggestionQuery.Field, suggestionQuery.Accuracy); database.IndexStorage.SetIndexExtension(indexName, indexExtensionKey, suggestionQueryIndexExtension); long _; var task = Task.Factory.StartNew(() => suggestionQueryIndexExtension.Init(indexReader)); database.AddTask(task, new object(), out _); // wait for a bit for the suggestions to complete, but not too much (avoid IIS resets) task.Wait(15000, database.WorkContext.CancellationToken); return suggestionQueryIndexExtension.Query(suggestionQuery, indexReader); } }
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 indexExtensionKey = MonoHttpUtility.UrlEncode(suggestionQuery.Field + "-" + suggestionQuery.Distance + "-" + suggestionQuery.Accuracy); var indexExtension = _database.IndexStorage.GetIndexExtension(indexName, indexExtensionKey) as SuggestionQueryIndexExtension; IndexSearcher currentSearcher; using (_database.IndexStorage.GetCurrentIndexSearcher(indexName, out currentSearcher)) { if (currentSearcher == null) { throw new InvalidOperationException("Could not find current searcher"); } var indexReader = currentSearcher.IndexReader; if (indexExtension != null) return indexExtension.Query(suggestionQuery, indexReader); var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension( Path.Combine(_database.Configuration.IndexStoragePath, "Raven-Suggestions", indexName, indexExtensionKey), indexReader, GetStringDistance(suggestionQuery.Distance), suggestionQuery.Field, suggestionQuery.Accuracy); suggestionQueryIndexExtension.Init(indexReader); _database.IndexStorage.SetIndexExtension(indexName, indexExtensionKey, suggestionQueryIndexExtension); return suggestionQueryIndexExtension.Query(suggestionQuery, indexReader); } }
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"); } suggestionQuery.MaxSuggestions = Math.Min(suggestionQuery.MaxSuggestions, database.Configuration.MaxPageSize); if (suggestionQuery.MaxSuggestions <= 0) { suggestionQuery.MaxSuggestions = SuggestionQuery.DefaultMaxSuggestions; } if (suggestionQuery.Accuracy.HasValue && (suggestionQuery.Accuracy.Value <= 0f || suggestionQuery.Accuracy.Value > 1f)) { suggestionQuery.Accuracy = SuggestionQuery.DefaultAccuracy; } if (suggestionQuery.Accuracy.HasValue == false) { suggestionQuery.Accuracy = SuggestionQuery.DefaultAccuracy; } if (suggestionQuery.Distance.HasValue == false) { suggestionQuery.Distance = StringDistanceTypes.Default; } var definition = database.IndexDefinitionStorage.GetIndexDefinition(indexName); var indexExtensionKey = MonoHttpUtility.UrlEncode(suggestionQuery.Field + "-" + suggestionQuery.Distance + "-" + suggestionQuery.Accuracy); var indexExtension = database.IndexStorage.GetIndexExtensionByPrefix(indexName, indexExtensionKey) as SuggestionQueryIndexExtension; IndexSearcher currentSearcher; using (database.IndexStorage.GetCurrentIndexSearcher(definition.IndexId, out currentSearcher)) { if (currentSearcher == null) { throw new InvalidOperationException("Could not find current searcher"); } var indexReader = currentSearcher.IndexReader; if (indexExtension != null) { return(indexExtension.Query(suggestionQuery, indexReader)); } var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension( database.WorkContext, Path.Combine(database.Configuration.IndexStoragePath, "Raven-Suggestions", indexName, indexExtensionKey), GetStringDistance(suggestionQuery.Distance ?? StringDistanceTypes.Default), indexReader.Directory() is RAMDirectory, suggestionQuery.Field, suggestionQuery.Accuracy ?? 0.5f); database.IndexStorage.SetIndexExtension(indexName, indexExtensionKey, suggestionQueryIndexExtension); long _; var task = Task.Factory.StartNew(() => suggestionQueryIndexExtension.Init(indexReader)); database.Tasks.AddTask(task, new object(), new TaskActions.PendingTaskDescription { Payload = indexName, TaskType = TaskActions.PendingTaskType.SuggestionQuery, StartTime = SystemTime.UtcNow }, out _); // wait for a bit for the suggestions to complete, but not too much (avoid IIS resets) task.Wait(15000, database.WorkContext.CancellationToken); return(suggestionQueryIndexExtension.Query(suggestionQuery, indexReader)); } }
private void InvokeSuggestionIndexing(string name, IndexDefinition definition) { foreach (var suggestion in definition.Suggestions) { var field = suggestion.Key; var suggestionOption = suggestion.Value; if (suggestionOption.Distance == StringDistanceTypes.None) continue; var indexExtensionKey = MonoHttpUtility.UrlEncode(field + "-" + suggestionOption.Distance + "-" + suggestionOption.Accuracy); var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension( WorkContext, Path.Combine(Database.Configuration.IndexStoragePath, "Raven-Suggestions", name, indexExtensionKey), SuggestionQueryRunner.GetStringDistance(suggestionOption.Distance), Database.Configuration.RunInMemory, field, suggestionOption.Accuracy); Database.IndexStorage.SetIndexExtension(name, indexExtensionKey, suggestionQueryIndexExtension); } }
private void LoadExistingSuggestionsExtentions(string indexName, Index indexImplementation) { var suggestionsForIndex = Path.Combine(configuration.IndexStoragePath, "Raven-Suggestions", indexName); if (!Directory.Exists(suggestionsForIndex)) return; try { foreach (var directory in Directory.GetDirectories(suggestionsForIndex)) { IndexSearcher searcher; using (indexImplementation.GetSearcher(out searcher)) { var key = Path.GetFileName(directory); var decodedKey = MonoHttpUtility.UrlDecode(key); var lastIndexOfDash = decodedKey.LastIndexOf('-'); var accuracy = float.Parse(decodedKey.Substring(lastIndexOfDash + 1), CultureInfo.InvariantCulture); var lastIndexOfDistance = decodedKey.LastIndexOf('-', lastIndexOfDash - 1); StringDistanceTypes distanceType; Enum.TryParse(decodedKey.Substring(lastIndexOfDistance + 1, lastIndexOfDash - lastIndexOfDistance - 1), true, out distanceType); var field = decodedKey.Substring(0, lastIndexOfDistance); var extension = new SuggestionQueryIndexExtension( indexImplementation, documentDatabase.WorkContext, Path.Combine(configuration.IndexStoragePath, "Raven-Suggestions", indexName, key), SuggestionQueryRunner.GetStringDistance(distanceType), searcher.IndexReader.Directory() is RAMDirectory, field, accuracy); indexImplementation.SetExtension(key, extension); } } } catch (Exception e) { log.WarnException("Could not open suggestions for index " + indexName + ", resetting the index", e); try { IOExtensions.DeleteDirectory(suggestionsForIndex); } catch (Exception) { // ignore the failure } throw; } }
private void LoadExistingSuggesionsExtentions(string indexName, Index indexImplementation) { var suggestionsForIndex = Path.Combine(configuration.IndexStoragePath, "Raven-Suggestions", indexName); if (!Directory.Exists(suggestionsForIndex)) return; foreach (var directory in Directory.GetDirectories(suggestionsForIndex)) { IndexSearcher searcher; using (indexImplementation.GetSearcher(out searcher)) { var key = Path.GetFileName(directory); var decodedKey = MonoHttpUtility.UrlDecode(key); var lastIndexOfDash = decodedKey.LastIndexOf('-'); var accuracy = float.Parse(decodedKey.Substring(lastIndexOfDash + 1)); var lastIndexOfDistance = decodedKey.LastIndexOf('-', lastIndexOfDash - 1); StringDistanceTypes distanceType; Enum.TryParse(decodedKey.Substring(lastIndexOfDistance + 1, lastIndexOfDash - lastIndexOfDistance - 1), true, out distanceType); var field = decodedKey.Substring(0, lastIndexOfDistance); var extension = new SuggestionQueryIndexExtension( Path.Combine(configuration.IndexStoragePath, "Raven-Suggestions", indexName, key), searcher.IndexReader, SuggestionQueryRunner.GetStringDistance(distanceType), field, accuracy); indexImplementation.SetExtension(key, extension); } } }
private void OpenIndexOnStartup(DocumentDatabase documentDatabase, string indexName) { if (indexName == null) throw new ArgumentNullException("indexName"); startupLog.Debug("Loading saved index {0}", indexName); var indexDefinition = indexDefinitionStorage.GetIndexDefinition(indexName); if (indexDefinition == null) return; Index indexImplementation; bool resetTried = false; while (true) { try { var luceneDirectory = OpenOrCreateLuceneDirectory(indexDefinition, createIfMissing: resetTried); indexImplementation = CreateIndexImplementation(indexName, indexDefinition, luceneDirectory); var suggestionsForIndex = Path.Combine(configuration.IndexStoragePath, "Raven-Suggestions", indexName); if (Directory.Exists(suggestionsForIndex)) { foreach (var directory in Directory.GetDirectories(suggestionsForIndex)) { IndexSearcher searcher; using(indexImplementation.GetSearcher(out searcher)) { var key = Path.GetFileName(directory); var decodedKey = MonoHttpUtility.UrlDecode(key); var lastIndexOfDash = decodedKey.LastIndexOf('-'); var accuracy = float.Parse(decodedKey.Substring(lastIndexOfDash+1)); var lastIndexOfDistance = decodedKey.LastIndexOf('-', lastIndexOfDash - 1); StringDistanceTypes distanceType; Enum.TryParse(decodedKey.Substring(lastIndexOfDistance+1, lastIndexOfDash - lastIndexOfDistance-1), true, out distanceType); var field = decodedKey.Substring(0, lastIndexOfDistance ); var extension = new SuggestionQueryIndexExtension( Path.Combine(configuration.IndexStoragePath, "Raven-Suggestions", indexName, key), searcher.GetIndexReader(), SuggestionQueryRunner.GetStringDistance(distanceType), field, accuracy); indexImplementation.SetExtension(key, extension); } } } break; } catch (Exception e) { if (resetTried) throw new InvalidOperationException("Could not open / create index" + indexName + ", reset already tried", e); resetTried = true; startupLog.WarnException("Could not open index " + indexName + ", forcibly resetting index", e); try { documentDatabase.TransactionalStorage.Batch(accessor => { accessor.Indexing.DeleteIndex(indexName); accessor.Indexing.AddIndex(indexName, indexDefinition.IsMapReduce); }); var indexDirectory = indexName; var indexFullPath = Path.Combine(path, MonoHttpUtility.UrlEncode(indexDirectory)); IOExtensions.DeleteDirectory(indexFullPath); } catch (Exception exception) { throw new InvalidOperationException("Could not reset index " + indexName, exception); } } } indexes.TryAdd(indexName, indexImplementation); }
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 indexExtensionKey = MonoHttpUtility.UrlEncode(suggestionQuery.Field + "-" + suggestionQuery.Distance + "-" + suggestionQuery.Accuracy); var indexExtension = ( database.IndexStorage.GetIndexExtension(indexName, indexExtensionKey) ?? database.IndexStorage.GetIndexExtensionByPrefix(indexName, MonoHttpUtility.UrlEncode(suggestionQuery.Field + "-" + suggestionQuery.Distance)) ?? database.IndexStorage.GetIndexExtensionByPrefix(indexName, MonoHttpUtility.UrlEncode(suggestionQuery.Field)) ) as SuggestionQueryIndexExtension; IndexSearcher currentSearcher; using (database.IndexStorage.GetCurrentIndexSearcher(indexName, out currentSearcher)) { if (currentSearcher == null) { throw new InvalidOperationException("Could not find current searcher"); } var indexReader = currentSearcher.IndexReader; if (indexExtension != null) { return(indexExtension.Query(suggestionQuery, indexReader)); } var suggestionQueryIndexExtension = new SuggestionQueryIndexExtension( database.WorkContext, Path.Combine(database.Configuration.IndexStoragePath, "Raven-Suggestions", indexName, indexExtensionKey), indexReader.Directory() is RAMDirectory, GetStringDistance(suggestionQuery.Distance), suggestionQuery.Field, suggestionQuery.Accuracy); database.IndexStorage.SetIndexExtension(indexName, indexExtensionKey, suggestionQueryIndexExtension); long _; var task = Task.Factory.StartNew(() => suggestionQueryIndexExtension.Init(indexReader)); database.AddTask(task, new object(), out _); // wait for a bit for the suggestions to complete, but not too much (avoid IIS resets) task.Wait(15000, database.WorkContext.CancellationToken); return(suggestionQueryIndexExtension.Query(suggestionQuery, indexReader)); } }