protected void AddMap <TSource>(Expression <Func <IEnumerable <TSource>, IEnumerable> > expression) { _maps.Add(() => { string querySource = typeof(TSource) == typeof(object) ? "docs" : IndexDefinitionHelper.GetQuerySource(Conventions, typeof(TSource)); return(IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TSource, TReduceResult>(expression, Conventions, querySource, translateIdentityProperty: true)); }); }
protected void AddMap <TSource>(Expression <Func <IEnumerable <TSource>, IEnumerable> > map) { if (map == null) { throw new ArgumentNullException(nameof(map)); } _maps.Add(() => { string querySource = typeof(TSource) == typeof(object) ? "docs" : IndexDefinitionHelper.GetQuerySource(Conventions, typeof(TSource), IndexSourceType.Documents); return(IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TSource, TReduceResult>(map, Conventions, querySource, translateIdentityProperty: true)); }); }
/// <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" : IndexDefinitionHelper.GetQuerySource(conventions, typeof(TDocument)); var indexDefinition = new IndexDefinition { Name = _indexName, Reduce = IndexDefinitionHelper.PruneToFailureLinqQueryAsStringToWorkableCode <TDocument, TReduceResult>(Reduce, conventions, "results", translateIdentityProperty: false), LockMode = LockMode, Priority = Priority, OutputReduceToCollection = OutputReduceToCollection }; 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); #pragma warning disable CS0618 // Type or member is obsolete if (conventions.PrettifyGeneratedLinqExpressions) #pragma warning restore CS0618 // Type or member is obsolete { 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); #pragma warning disable CS0618 // Type or member is obsolete indexDefinition.Maps.Add(conventions.PrettifyGeneratedLinqExpressions ? IndexPrettyPrinter.TryFormat(map) : map); #pragma warning restore CS0618 // Type or member is obsolete } indexDefinition.AdditionalSources = AdditionalSources; indexDefinition.Configuration = Configuration; return(indexDefinition); } catch (Exception e) { throw new IndexCompilationException("Failed to create index " + _indexName, e); } }
private string GetQuerySource(DocumentConventions conventions) { return (typeof(TDocument) == typeof(object) || ContainsWhereEntityIs()) ? "docs" : IndexDefinitionHelper.GetQuerySource(conventions, typeof(TDocument), IndexSourceType.Documents); }