Getting IndexCompilationException in RavenDB when trying to create a recursive index by code

Viewed 39

I'm quite new to RavenDB and I'm having problems creating some indexes by code, especially recursive ones.

I have a ravendb instance which has Users and Groups collections. User has a property BelongsTo which is an array of the group ids of the groups the user belongs to directly, and Groups also has a BelongsTo property with the groups to which that group belongs to directly. So:

If Group X belongs to Group Z, and User X1 belongs to Group X, I have:

Group: {
  Id: 'Z',
  BelongsTo: []
}

Group: {
  Id: 'X',
  BelongsTo: ['Z']
}

User: {
  Id: 'X1',
  BelongsTo: ['X']
}

I want to have an index that gets me, for each user, all the groups he belongs to, whether directly or indirectly. There can be an hierarchy of groups, the only rule is that this hierarchy cannot be circular (we cannot have group A belonging to group B, and group B belonging to group A).

In the example above I would have the Index return something like:

{
  UserId: 'X1',
  Name: 'xpto',
  BelongsTo: ['X', 'Z']
}

I tried creating an index to do so using Recurse, just like in the following Url:

https://ayende.com/blog/185537-A/recursive-indexing-in-ravendb

Here's the C# code for my index:

public class User_BelongedGroups : AbstractIndexCreationTask<User>
    {
        public User_BelongedGroups()
        {
            Map = users =>
            from u in users
            select new
            {
                UserId = u.Id,
                u.Name,
                BelongsTo = u.BelongsTo.Select(g => Recurse(g, gId => LoadDocument<Group>(gId).BelongsTo))
            };
        }
    }

When I try to create this index, I get an IndexCompilationException. When I look into the InnerException I get the following text:

IndexCompilationException: Failed to compile index User/BelongedGroups

using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Lucene.Net.Documents;
using Raven.Client.Documents.Indexes;
using Raven.Server.Documents.Indexes.Static;
using Raven.Server.Documents.Indexes.Static.Linq;
using Raven.Server.Documents.Indexes.Static.Extensions;

namespace Raven.Server.Documents.Indexes.Static.Generated
{
    public class Index_User_BelongedGroups : StaticIndexBase
    {
        IEnumerable Map_0(IEnumerable<dynamic> docs)
        {
            foreach (var u in docs)
            {
                yield return new
                {
                    UserId = Id(u),
                    Name = u.Name,
                    BelongsTo = ((IEnumerable<dynamic>)u.BelongsTo).Select((Func<dynamic, dynamic>)(g => this.Recurse<string, string>(g, gId => (this.LoadDocument(gId, "Groups")).BelongsTo)))
                }

                ;
            }
        }

        public Index_User_BelongedGroups()
        {
            this.AddMap("Users", this.Map_0);
            this.AddReferencedCollection("Users", "Groups");
            this.OutputFields = new System.String[] { "UserId", "Name", "BelongsTo" };
        }
    }
}

(26,111): error CS0308: The non-generic method 'AbstractStaticIndexBase.Recurse(object, Func<dynamic, dynamic>)' cannot be used with type arguments
, IndexDefinitionProperty='', ProblematicText=''   at Raven.Server.Documents.Indexes.Static.IndexCompiler.CompileInternal(String originalName, String cSharpSafeName, MemberDeclarationSyntax class, IndexDefinition definition) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Indexes\Static\IndexCompiler.cs:line 221
   at Raven.Server.Documents.Indexes.Static.IndexCompiler.Compile(IndexDefinition definition) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Indexes\Static\IndexCompiler.cs:line 149
   at Raven.Server.Documents.Indexes.Static.IndexCompilationCache.GenerateIndex(IndexDefinition definition, RavenConfiguration configuration, IndexType type) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Indexes\Static\IndexCompilationCache.cs:line 133
   at Raven.Server.Documents.Indexes.Static.IndexCompilationCache.<>c__DisplayClass2_0.<GetDocumentsIndexInstance>b__1() in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Indexes\Static\IndexCompilationCache.cs:line 47
   at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
   at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
   at System.Lazy`1.CreateValue()
   at Raven.Server.Documents.Indexes.Static.IndexCompilationCache.GetDocumentsIndexInstance(IndexDefinition definition, RavenConfiguration configuration, IndexType type) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Indexes\Static\IndexCompilationCache.cs:line 56
   at Raven.Server.Documents.Indexes.IndexStore.ValidateStaticIndex(IndexDefinition definition) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Indexes\IndexStore.cs:line 750
   at Raven.Server.Documents.Indexes.IndexStore.CreateIndexInternal(IndexDefinition definition, String raftRequestId, String source) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Indexes\IndexStore.cs:line 635
   at Raven.Server.Documents.Handlers.Admin.AdminIndexHandler.PutInternal(Boolean validatedAsAdmin) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Handlers\Admin\AdminIndexHandler.cs:line 91
   at Raven.Server.Documents.Handlers.Admin.AdminIndexHandler.Put() in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Documents\Handlers\Admin\AdminIndexHandler.cs:line 33
   at Raven.Server.Routing.RequestRouter.HandlePath(RequestHandlerContext reqCtx) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\Routing\RequestRouter.cs:line 196
   at Raven.Server.RavenServerStartup.RequestHandler(HttpContext context) in C:\Builds\RavenDB-Stable-5.1\51010\src\Raven.Server\RavenServerStartup.cs:line 242

What can I be doing wrong here?

0 Answers
Related