Skip to content

Instantly share code, notes, and snippets.

@etishor
Created November 6, 2012 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save etishor/4024002 to your computer and use it in GitHub Desktop.
Save etishor/4024002 to your computer and use it in GitHub Desktop.
Definition of a user work item index using multi map reduce
public class UserWorkItemIndex : AbstractMultiMapIndexCreationTask<UserWorkItemIndex.Result>
{
public class Result
{
public string UserId { get; set; }
public string DocumentId { get; set; }
public int Validated { get; set; }
public int Total { get; set; }
public string FieldId { get; set; }
public WorkItemStatus Status { get; set; }
}
public UserWorkItemIndex()
{
AddMap<ExtractionUser>(users => from user in users
from field in user.AssignedFields
select new
{
UserId = user.Id,
FieldId = field,
Status = WorkItemStatus.Ready,
DocumentId = (string)null,
Validated = 0,
Total = 0
});
AddMap<WorkItem>(items => from item in items
select new
{
UserId = (string)null,
FieldId = item.FieldValue.FieldName,
Status = item.Status,
DocumentId = item.DocumentId,
Validated = 0,
Total = 0
});
Reduce = results => results.GroupBy(r => r.FieldId)
.Select(g => new
{
FieldId = g.Key,
Users = g.Where(r => r.UserId != null)
.Select(r => new
{
UserId = r.UserId,
Documents = g.Where(d => d.DocumentId != null)
.Select(d => new
{
DocumentId = d.DocumentId,
Status = d.Status
})
})
})
.SelectMany(g => g.Users, (g, u) => new
{
UserId = u.UserId,
FieldId = g.FieldId,
Documents = u.Documents
})
.SelectMany(r => r.Documents, (r, d) => new
{
UserId = r.UserId,
FieldId = r.FieldId,
DocumentId = d.DocumentId,
Status = d.Status
})
.GroupBy(r => new { UserId = r.UserId, DocumentId = r.DocumentId })
.Select(g => new
{
UserId = g.Key.UserId,
DocumentId = g.Key.DocumentId,
Validated = g.Count(d => d.Status == WorkItemStatus.Validated),
Total = g.Count(),
FieldId = (string)null,
Status = WorkItemStatus.Ready
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment