Created
October 26, 2016 13:58
-
-
Save ricardobrandao/7c85ace05e43a2c1190c09f89343e39d to your computer and use it in GitHub Desktop.
RavenDb projection of empty lists
This file contains 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
using System.Collections.Generic; | |
using System.Linq; | |
using Raven.Abstractions.Indexing; | |
using Raven.Client; | |
using Raven.Client.Document; | |
using Raven.Client.Indexes; | |
namespace TestProjections.Console | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var store = new DocumentStore | |
{ | |
Url = "http://localhost.fiddler:8080", | |
DefaultDatabase = "Hello" | |
}.Initialize(true); | |
new Mock_SearchIndex().Execute(store); | |
using (var session = store.OpenSession()) | |
{ | |
//session.Store(new MockPage | |
//{ | |
// Title = "Javascript page", | |
// Tags = new List<string> { "javascript", "webdevelopment" } | |
//}); | |
//session.Store(new MockPage | |
//{ | |
// Title = "C# page", | |
// Tags = new List<string> { "dotnet", "csharp" } | |
//}); | |
//session.Store(new MockBlogPost | |
//{ | |
// Title = "A blogpost about C#", | |
// Tags = new List<string> { "dotnet", "csharp" } | |
//}); | |
var results = session.Query<Mock_SearchIndex.SearchResult, Mock_SearchIndex>() | |
.ProjectFromIndexFieldsInto<Mock_SearchIndex.SearchResult>() | |
.Search(x => x.Title, "C#") | |
.Skip(0) | |
.Take(5) | |
.ToList(); | |
} | |
} | |
public class MockPage | |
{ | |
public string Id { get; set; } | |
public string Title { get; set; } | |
public IEnumerable<string> Tags { get; set; } | |
} | |
public class MockBlogPost | |
{ | |
public string Id { get; set; } | |
public string Title { get; set; } | |
public IEnumerable<string> Tags { get; set; } | |
} | |
public class Mock_SearchIndex : AbstractMultiMapIndexCreationTask<Mock_SearchIndex.SearchResult> | |
{ | |
public class SearchResult | |
{ | |
public string Id { get; set; } | |
public string Title { get; set; } | |
public IEnumerable<string> Tags { get; set; } | |
public string ContentType { get; set; } | |
} | |
public Mock_SearchIndex() | |
{ | |
AddMap<MockBlogPost>(posts => from post in posts | |
select new SearchResult | |
{ | |
Id = post.Id, | |
Title = post.Title, | |
ContentType = "blogPost", | |
Tags = post.Tags | |
}); | |
AddMap<MockPage>(pages => from page in pages | |
select new SearchResult | |
{ | |
Id = page.Id, | |
Title = page.Title, | |
ContentType = "page", | |
// the problem is that we are return a empty list here | |
Tags = new string[] { } | |
}); | |
Index(r => r.Title, FieldIndexing.Analyzed); | |
StoreAllFields(FieldStorage.Yes); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment