Skip to content

Instantly share code, notes, and snippets.

@pdegenhardt
Created February 24, 2013 05:26
Show Gist options
  • Save pdegenhardt/5022737 to your computer and use it in GitHub Desktop.
Save pdegenhardt/5022737 to your computer and use it in GitHub Desktop.
Illustrates issue in doing simple transform using nullable property.
static class NullableInIndex
{
public class Person
{
public string Name { get; set; }
public string NickName { get; set; }
public DateTime? Birthday { get; set; }
}
public class PersonQuery
{
public string Query { get; set; }
}
public class PersonBrief
{
public string NickName { get; set; }
public DateTime? Birthday { get; set; }
}
public class Person_PersonBrief : AbstractIndexCreationTask<Person>
{
public Person_PersonBrief()
{
Map = docs => from doc in docs
select new { Query = new object[] { doc.Name, doc.NickName } };
TransformResults = (db,docs) =>
from doc in docs
select new { NickName = doc.NickName, Birthday = doc.Birthday };
Index("Query", FieldIndexing.Analyzed);
}
}
public static void Main()
{
var store = new DocumentStore { Url = "http://localhost:8080" };
store.Initialize();
store.ExecuteIndex(new Person_PersonBrief());
using (var session = store.OpenSession())
{
session.Store(new Person { Name = "Phil", NickName = "p1", Birthday = DateTime.Now.AddYears(-33) });
session.Store(new Person { Name = "John", NickName = "j1", Birthday = DateTime.Now.AddYears(-22) });
session.Store(new Person { Name = "Jamie", NickName = "j2" });
session.SaveChanges();
}
using (var session = store.OpenSession())
{
var results = session.Query<PersonQuery, Person_PersonBrief>()
.Where(x => x.Query.StartsWith("J"))
.AsProjection<PersonBrief>()
.ToList();
foreach(var item in results)
{
Console.WriteLine("{0} {1}", item.NickName, item.Birthday);
}
}
Console.ReadLine();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment