Skip to content

Instantly share code, notes, and snippets.

@etishor
Created November 10, 2012 15:12
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/4051366 to your computer and use it in GitHub Desktop.
Save etishor/4051366 to your computer and use it in GitHub Desktop.
Map reduce index
public class User
{
public string Id { get; set; }
public string[] AssignedFields { get; set; }
}
public class WorkItem
{
public string DocumentId { get; set; }
public string FieldId { get; set; }
public bool Validated { get; set; }
}
public class UserWorkItemIndex : AbstractIndexCreationTask<WorkItem, UserWorkItemIndex.Result>
{
public class Result
{
public string DocumentId { get; set; }
public string[] ValidatedFields { get; set; }
public string[] ReadyFields { get; set; }
}
public UserWorkItemIndex()
{
Map = items => items.Select(i => new
{
DocumentId = i.DocumentId,
ValidatedFields = i.Validated ? new string[] { i.FieldId } : new string[0],
ReadyFields = !i.Validated ? new string[] { i.FieldId } : new string[0]
});
Reduce = result => result
.GroupBy(i => i.DocumentId)
.Select(g => new
{
DocumentId = g.Key,
ValidatedFields = g.SelectMany(i => i.ValidatedFields),
ReadyFields = g.SelectMany(i => i.ReadyFields)
});
}
}
//And the query:
using (var session = store.OpenSession())
{
User user = session.Load<User>("users/1");
var result = session.Query<WorkItem, UserWorkItemIndex>()
.Customize(q => q.WaitForNonStaleResults())
.As<UserWorkItemIndex.Result>()
.Where(d => d.ValidatedFields.Any(f => f.In(user.AssignedFields)))
.ToArray();
Console.WriteLine(JsonConvert.SerializeObject(result, Formatting.Indented));
session.SaveChanges();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment