/// <summary> /// Toes the index definition. /// </summary> public IndexDefinition ToIndexDefinition(DocumentConventions conventions, bool validateMap = true) { if (Map == null && validateMap) { throw new InvalidOperationException( string.Format("Map is required to generate an index, you cannot create an index without a valid Map property (in index {0}).", _indexName)); } try { if (Reduce != null) { IndexDefinitionHelper.ValidateReduce(Reduce); } string querySource = (typeof(TDocument) == typeof(object) || ContainsWhereEntityIs()) ? "docs" : "docs." + conventions.GetCollectionName(typeof(TDocument)); var indexDefinition = new IndexDefinition { Name = _indexName, Reduce = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TDocument, TReduceResult>(Reduce, conventions, "results", translateIdentityProperty: false), LockMode = LockMode, Priority = Priority, OutputReduceToCollection = OutputReduceToCollection }; var indexes = ConvertToStringDictionary(Indexes); var stores = ConvertToStringDictionary(Stores); var analyzers = ConvertToStringDictionary(Analyzers); var suggestionsOptions = ConvertToStringSet(SuggestionsOptions).ToDictionary(x => x, x => true); var termVectors = ConvertToStringDictionary(TermVectors); var spatialOptions = ConvertToStringDictionary(SpatialIndexes); if (conventions.PrettifyGeneratedLinqExpressions) { indexDefinition.Reduce = IndexPrettyPrinter.TryFormat(indexDefinition.Reduce); } foreach (var indexesString in IndexesStrings) { if (indexes.ContainsKey(indexesString.Key)) { throw new InvalidOperationException("There is a duplicate key in indexes: " + indexesString.Key); } indexes.Add(indexesString); } foreach (var storeString in StoresStrings) { if (stores.ContainsKey(storeString.Key)) { throw new InvalidOperationException("There is a duplicate key in stores: " + storeString.Key); } stores.Add(storeString); } foreach (var analyzerString in AnalyzersStrings) { if (analyzers.ContainsKey(analyzerString.Key)) { throw new InvalidOperationException("There is a duplicate key in analyzers: " + analyzerString.Key); } analyzers.Add(analyzerString); } foreach (var termVectorString in TermVectorsStrings) { if (termVectors.ContainsKey(termVectorString.Key)) { throw new InvalidOperationException("There is a duplicate key in term vectors: " + termVectorString.Key); } termVectors.Add(termVectorString); } foreach (var spatialString in SpatialIndexesStrings) { if (spatialOptions.ContainsKey(spatialString.Key)) { throw new InvalidOperationException("There is a duplicate key in spatial indexes: " + spatialString.Key); } spatialOptions.Add(spatialString); } ApplyValues(indexDefinition, indexes, (options, value) => options.Indexing = value); ApplyValues(indexDefinition, stores, (options, value) => options.Storage = value); ApplyValues(indexDefinition, analyzers, (options, value) => options.Analyzer = value); ApplyValues(indexDefinition, termVectors, (options, value) => options.TermVector = value); ApplyValues(indexDefinition, spatialOptions, (options, value) => options.Spatial = value); ApplyValues(indexDefinition, suggestionsOptions, (options, value) => options.Suggestions = value); if (Map != null) { var map = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TDocument, TReduceResult>( Map, conventions, querySource, translateIdentityProperty: true); indexDefinition.Maps.Add(conventions.PrettifyGeneratedLinqExpressions ? IndexPrettyPrinter.TryFormat(map) : map); } indexDefinition.AdditionalSources = AdditionalSources; return(indexDefinition); } catch (Exception e) { throw new IndexCompilationException("Failed to create index " + _indexName, e); } }
/// <summary> /// Toes the index definition. /// </summary> public virtual TIndexDefinition ToIndexDefinition(DocumentConventions conventions, bool validateMap = true) { try { if (Reduce != null) IndexDefinitionHelper.ValidateReduce(Reduce); var indexDefinition = new TIndexDefinition { Name = _indexName, Reduce = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode<TDocument, TReduceResult>(Reduce, conventions, "results", translateIdentityProperty: false), LockMode = LockMode, Priority = Priority, OutputReduceToCollection = OutputReduceToCollection, PatternForOutputReduceToCollectionReferences = PatternReferencesCollectionName, PatternReferencesCollectionName = PatternReferencesCollectionName }; if (PatternForOutputReduceToCollectionReferences != null) indexDefinition.PatternForOutputReduceToCollectionReferences = ConvertPatternForOutputReduceToCollectionReferencesToString(PatternForOutputReduceToCollectionReferences); var indexes = ConvertToStringDictionary(Indexes); var stores = ConvertToStringDictionary(Stores); var analyzers = ConvertToStringDictionary(Analyzers); var suggestionsOptions = ConvertToStringSet(SuggestionsOptions).ToDictionary(x => x, x => true); var termVectors = ConvertToStringDictionary(TermVectors); var spatialOptions = ConvertToStringDictionary(SpatialIndexes); foreach (var indexesString in IndexesStrings) { if (indexes.ContainsKey(indexesString.Key)) throw new InvalidOperationException("There is a duplicate key in indexes: " + indexesString.Key); indexes.Add(indexesString); } foreach (var storeString in StoresStrings) { if (stores.ContainsKey(storeString.Key)) throw new InvalidOperationException("There is a duplicate key in stores: " + storeString.Key); stores.Add(storeString); } foreach (var analyzerString in AnalyzersStrings) { if (analyzers.ContainsKey(analyzerString.Key)) throw new InvalidOperationException("There is a duplicate key in analyzers: " + analyzerString.Key); analyzers.Add(analyzerString); } foreach (var termVectorString in TermVectorsStrings) { if (termVectors.ContainsKey(termVectorString.Key)) throw new InvalidOperationException("There is a duplicate key in term vectors: " + termVectorString.Key); termVectors.Add(termVectorString); } foreach (var spatialString in SpatialIndexesStrings) { if (spatialOptions.ContainsKey(spatialString.Key)) throw new InvalidOperationException("There is a duplicate key in spatial indexes: " + spatialString.Key); spatialOptions.Add(spatialString); } ApplyValues(indexDefinition, indexes, (options, value) => options.Indexing = value); ApplyValues(indexDefinition, stores, (options, value) => options.Storage = value); ApplyValues(indexDefinition, analyzers, (options, value) => options.Analyzer = value); ApplyValues(indexDefinition, termVectors, (options, value) => options.TermVector = value); ApplyValues(indexDefinition, spatialOptions, (options, value) => options.Spatial = value); ApplyValues(indexDefinition, suggestionsOptions, (options, value) => options.Suggestions = value); indexDefinition.AdditionalSources = AdditionalSources; indexDefinition.Configuration = Configuration; ToIndexDefinition(indexDefinition, conventions); return indexDefinition; } catch (Exception e) { throw new IndexCompilationException("Failed to create index " + _indexName, e); } }