Nullable Field in Where clause for MapReduce

81 views
Skip to first unread message

Mike M

unread,
May 3, 2013, 3:34:29 PM5/3/13
to ravendb
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);
}
}
}
}

Oren Eini (Ayende Rahien)

unread,
May 4, 2013, 8:20:20 AM5/4/13
to ravendb
Thanks, this will be fixed in the next build.
As a work around, do:

                                  where !Equals(student.PersonId, null)



--
You received this message because you are subscribed to the Google Groups "ravendb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Mike M

unread,
May 4, 2013, 11:02:52 PM5/4/13
to rav...@googlegroups.com
Thanks Oren. I was beating my head on how I was going to implement this.

Mike M

unread,
May 22, 2013, 9:57:38 AM5/22/13
to rav...@googlegroups.com
I am having problems with this same type of thing with the nullable field in a group by. I have tried several ways, but haven't found a way that works. I am including a test below. The test doesn't necessarily make sense, but it captures the problem. When trying to use the nullable field in the group by clause in the Reduce (even if it is determined that it won't exist as a null because it is excluded in the Map), it doesn't work. Any help will be appreciated.

    public class CanExcludedNullItems : RavenTestBase
    {
        private class Student
		{
			public string Email { getset; }
            public long? PersonId { getset
; }
            public string Type { getset; }
        }
 
		private class Students_ByEmailDomain : AbstractMultiMapIndexCreationTask<CanExcludedNullItems.Students_ByEmailDomain.Result>
		{
			
public class Result
			{
				public string EmailDomain { getset
; }
                public int GradCount { getset; }
                public int UnderGradCount { getset; }
                public long? PersonId { getset; }
			}
 
			public Students_ByEmailDomain()
			{
				AddMap<Student>(students => from student in students
                                        where student.Type == "Grad"
                                            && !Equals(student.PersonId, null)
				                  select new
				                  {
					                  EmailDomain = student.Email.Split('@').Last(),
                                      GradCount = 1,
                                      UnderGradCount = 0,
                                      PersonId = student.PersonId
				                  });
 
 
                AddMap<Student>(students => from student in students
                                            where student.Type == "Undergrad"
                                                && !Equals(student.PersonId, null)
                                            select new
                                            {
                                                EmailDomain = student.Email.Split('@').Last(),
                                                GradCount = 0,
                                                UnderGradCount = 1,
                                                PersonId = student.PersonId
                                            });
 
				Reduce = results => from result in results
				                    group result by new {
                                      result.PersonId,
                                      result.EmailDomain
                                    }
				                    into g
				                    select new
				                    {
                                        PersonId = g.Key.PersonId,
					                    EmailDomain = g.Key.EmailDomain,
                                        GradCount = g.Sum(r => r.GradCount),
                                        UnderGradCount = g.Sum(r => r.UnderGradCount),
				                    };
			}
		}
 
		[Fact]
		
public void WillSupportLast()
		{
			using (var store = NewDocumentStore())
			{
				using (var
 session = store.OpenSession())
				{
                    session.Store(new CanExcludedNullItems.Student { Email = "sup...@hibernatingrhinos.com", Type = "Undergrad"});
                    session.Store(new CanExcludedNullItems.Student { Email = "sup...@hibernatingrhinos.com", PersonId = 1, Type = "Undergrad" });
                    session.Store(new CanExcludedNullItems.Student { Email = "sup...@hibernatingrhinos.com", PersonId = 1, Type = "Grad" });
					session.SaveChanges();
				}
 
				
new CanExcludedNullItems.Students_ByEmailDomain().Execute(store);
 
				using (var session = store.OpenSession())
				{
					var results = session.Query<CanExcludedNullItems.Students_ByEmailDomain.ResultCanExcludedNullItems.Students_ByEmailDomain
>()
					                     .Customize(customization => customization.WaitForNonStaleResults())
					                     .ToList();
 
                    Debug.Write(store.DatabaseCommands.GetStatistics().Errors);
 
					Assert.Empty(store.DatabaseCommands.GetStatistics().Errors);
					Assert.Equal(1, results.Count);
				}
			}
		}
	}


On Saturday, May 4, 2013 6:20:20 AM UTC-6, Oren Eini wrote:

Oren Eini (Ayende Rahien)

unread,
May 23, 2013, 6:47:54 AM5/23/13
to ravendb
Running this test passes when using 2.5

River moss

unread,
May 24, 2013, 1:22:42 PM5/24/13
to rav...@googlegroups.com
Thanks Oren. When can we expect to use 2.5 as a stable build?

Sent from my iPhone
You received this message because you are subscribed to a topic in the Google Groups "ravendb" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ravendb/bL7BSP0GHVg/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to ravendb+u...@googlegroups.com.

Oren Eini (Ayende Rahien)

unread,
May 24, 2013, 1:27:46 PM5/24/13
to ravendb
Next month
Reply all
Reply to author
Forward
0 new messages