Created
November 10, 2012 15:12
-
-
Save etishor/4051366 to your computer and use it in GitHub Desktop.
Map reduce index
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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