I have some items that can be nullable. I want to MapReduce these
items, but for the index that I am looking at, I want to only count
and include in the index the items that do not have null in the field
that I am looking at. Keep in mind that there are other MapReduce
indexes that use these documents that don't care if that field is
null, this index however does. I have created a test below that
demonstrates what I am essentially trying to do in a very simple
manner. Any advise is appreciated. Thanks.
public class CanExcludedNullItems : RavenTestBase
{
private class Student
{
public string Email { get; set; }
public long? PersonId { get; set; }
}
private class Students_ByEmailDomain :
AbstractIndexCreationTask<CanExcludedNullItems.Student,
CanExcludedNullItems.Students_ByEmailDomain.Result>
{
public class Result
{
public string EmailDomain { get; set; }
public int Count { get; set; }
}
public Students_ByEmailDomain()
{
Map = students => from student in students
where student.PersonId != null
select new
{
EmailDomain = student.Email.Split('@').Last(),
Count = 1,
};
Reduce = results => from result in results
group result by result.EmailDomain
into g
select new
{
EmailDomain = g.Key,
Count = g.Sum(r => r.Count),
};
}
}
[Fact]
public void WillSupportLast()
{
using (var store = NewDocumentStore())
{
using (var session = store.OpenSession())
{
session.Store(new CanExcludedNullItems.Student
{ Email = "
sup...@hibernatingrhinos.com" });
session.Store(new CanExcludedNullItems.Student
{ Email = "
sup...@hibernatingrhinos.com", PersonId = 1 });
session.SaveChanges();
}
new CanExcludedNullItems.Students_ByEmailDomain().Execute(store);
using (var session = store.OpenSession())
{
var results =
session.Query<CanExcludedNullItems.Students_ByEmailDomain.Result,
CanExcludedNullItems.Students_ByEmailDomain>()
.Customize(customization =>
customization.WaitForNonStaleResults())
.ToList();
Assert.Empty(store.DatabaseCommands.GetStatistics().Errors);
Assert.Equal(1, results.Count);
}
}
}
}