示例#1
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");

			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);
			}
		}
示例#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 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);
			}
		}
示例#3
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 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));
            }
        }
示例#4
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 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();
            }
        }
示例#5
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 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);
			}
		}
示例#7
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");
            }

            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));
            }
        }
示例#8
0
        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);
            }
        }
示例#9
0
		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;
		    }
		}
示例#10
0
		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);
				}
			}
		}
示例#11
0
		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);
		}
示例#12
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 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));
            }
        }