2
votes

I have the following code:

public class WorkOrderByUserId : AbstractMultiMapIndexCreationTask<WorkOrderByUserId.Result>
{
    public WorkOrderByUserId()
    {
        this.AddMap<WorkOrder>(items
            => from item in items
                select new Result
                {
                    OwnerId = item.OwnerId,
                    WorkOrder = new WorkOrderLookupDto
                    {
                        Id = item.Id,
                        Name = item.EventName
                    },
                    WorkOrders = null
                });

        this.AddMap<Invoice>(items
            => from item in items
                select new Result
                {
                    OwnerId = item.WorkOrder.OwnerId,
                    WorkOrder = new WorkOrderLookupDto
                    {
                        Id = item.WorkOrder.Id,
                        Name = item.WorkOrder.EventName
                    },
                    WorkOrders = null
                });

        this.Reduce = results => from result in results
            group result by result.OwnerId
            into g
            select new Result
            {
                OwnerId = g.Key,
                WorkOrders = g.Select(x => x.WorkOrder),
                WorkOrder = null
            };

        this.Indexes.Add(x => x.OwnerId, FieldIndexing.Default);
    }

    public class Result
    {
        public string OwnerId { get; set; }

        public IEnumerable<WorkOrderLookupDto> WorkOrders { get; set; }

        public WorkOrderLookupDto WorkOrder { get; set; }
    }
}

It gets me very close to where I want to be, however I seem to be missing something and I'm losing information and I'm not sure why.

Using the Map/Reduce Visualizer I notice the MAP is displaying the Results (i.e. WorkOrders is populated, and WorkOrder is null)

Map/Reduce Visualizer

As this point I was expecting to have a bunch of items with NULL WorkOrders and a valid WorkOrder, which I expected to reduce down into the WorkOrders collection.

When I look at the final reduce in the visualizer, I notice my WorkOrder is NULL and my WorkOrders are missing entirely.

Reduce

And when I look at the FINAL result of the Index I see what I'm looking for, just without the actual data I'm looking for.

enter image description here

What do I need to change to get my final WorkOrders to NOT be NULL?

1

1 Answers

0
votes

I was able to get my desired result using the following code:

public class WorkOrderByUserId : AbstractMultiMapIndexCreationTask<WorkOrderByUserId.Result>
{
    public WorkOrderByUserId()
    {
        this.AddMap<WorkOrder>(items
            => from item in items
                select new Result
                {
                    OwnerId = item.OwnerId,
                    WorkOrders = new[]
                    {
                        new WorkOrderLookupDto
                        {
                            Id = item.Id,
                            Name = item.EventName
                        }
                    }
                });

        this.AddMap<Invoice>(items
            => from item in items
                select new Result
                {
                    OwnerId = item.WorkOrder.OwnerId,
                    WorkOrders = new[]
                    {
                        new WorkOrderLookupDto
                        {
                            Id = item.WorkOrder.Id,
                            Name = item.WorkOrder.EventName
                        }
                    }
                });

        this.Reduce = results => from result in results
            group result by result.OwnerId
            into g
            select new Result
            {
                OwnerId = g.Key,
                WorkOrders = g.SelectMany(x => x.WorkOrders)
            };

        this.Indexes.Add(x => x.OwnerId, FieldIndexing.Default);
    }

    public class Result
    {
        public string OwnerId { get; set; }

        public IEnumerable<WorkOrderLookupDto> WorkOrders { get; set; }
    }
}