Is it possible to create RavenDB map-reduce index that contains generic type via IndexCreation.CreateIndexes?

77 Views Asked by At

I have an index:


public class TotalsIndex<TClass> : AbstractIndexCreationTask<TClass, Totals> where TClass : class, IClass

I get an error: Cannot create an instance of Raven.Client.Documents.Indexes.AbstractIndexCreationTask1[TDocument] because Type.ContainsGenericParameters is true.'`

when use IndexCreation.CreateIndexes(assembly, store);

Is it possible to create RavenDB map-reduce index that contains generic type via IndexCreation.CreateIndexes?

On the other hand new TotalsIndex<TClass>().Execute(documentStore); works

1

There are 1 best solutions below

2
Danielle On BEST ANSWER

The common way to define a Map or a Map-Reduce index is without the generic on the index class name.

e.g., see this Map-Reduce index example:
(taken from https://demo.ravendb.net/demos/csharp/static-indexes/map-reduce-index)

public class Employees_ByCountry : 
    AbstractIndexCreationTask<Employee, Employees_ByCountry.IndexEntry>
{
    public class IndexEntry
    {
        public string Country { get; set; }
        public int CountryCount { get; set; }
    }
            
    public Employees_ByCountry()
    {
        Map = employees => from employee in employees
            select new IndexEntry
            {
               Country = employee.Address.Country,
               CountryCount = 1
            };
                
        Reduce = results => from result in results
            group result by result.Country into g
            select new IndexEntry
            {
                Country = g.Key,
                CountryCount = g.Sum(x => x.CountryCount)
            };
    }
}

You can then deploy the index by:

new Employees_ByCountry().Execute(store);

or by:

IndexCreation.CreateIndexes(new[] { new Employees_ByCountry() }, store);

or - as you wanted - by:

IndexCreation.CreateIndexes(assembly, store);

or by:
Sending a PutIndexesOperation on the store


See Creating & Deploying:
https://ravendb.net/docs/article-page/6.0/csharp/indexes/creating-and-deploying