1

I am indexing 2 different documents as below.

public class User
{
    public UserId UserId { get; set; }
    public string DisplayName { get; set; }
}

public class UserGroup
{
    public UserGroupId UserGroupId { get; set; }

    public string Name { get; set; }
}

//this is for indexing result
public class SecurityObject
{
    public virtual Guid Id { get; set; }

    public virtual string DisplayName { get; set; }

    public virtual SecurityObjectType ObjectType { get; set; }
}

My query is as below. As you can see in Index configuration that I mapped Name property to DisplayName but DisplayName property is null when the returning document is UserGroup. When I review the fetched documents Name property is not mapped to DisplayName property and real value still stays on Name property. How can I correctly transform that Name property to DisplayName property on UserGroup?

_session.Query<SecurityObject, SecurityObjectIndex>()
    .Where(s => s.DisplayName.StartsWith(searchTerm))
    .ProjectFromIndexFieldsInto<SecurityObject>();

And here is my indexing configuration:

public class SecurityObjectIndex
    : AbstractMultiMapIndexCreationTask<SecurityObject>
{
    public override string IndexName
    {
        get
        {
            return "UserAndUserGroup/SecurityObjectIndex";
        }
    }

    public SecurityObjectIndex()
    {
        AddMap<User>(users => from u in users
                              select new
                              {
                                  DisplayName = u.DisplayName,
                                  ObjectType = SecurityObjectType.Users
                              });

        AddMap<UserGroup>(userGroups => from u in userGroups
                                        select new 
                                      { 
                                          DisplayName = u.Name,
                                          ObjectType = SecurityObjectType.Groups
                                      });
    }
}
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Freshblood
  • 6,285
  • 10
  • 59
  • 96

1 Answers1

0

In order to project from index fields, those fields must be stored with the index. Add the following to your index definition:

Store(x=> x.DisplayName, FieldStorage.Yes);
Store(x=> x.ObjectType, FieldStorage.Yes);

Alternatively, you can use this shortcut method:

StoreAllFields(FieldStorage.Yes);

But personally I prefer to mark each one separately.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575