Indexing RavenDB-如何将相同类型的相关文档合并到索引投影中
假设以下类别:Indexing RavenDB-如何将相同类型的相关文档合并到索引投影中,indexing,mapreduce,ravendb,Indexing,Mapreduce,Ravendb,假设以下类别: public class Thing { public string Id { get; set; } public string Title{ get; set; } public string KeyId { get; set; } public CultureInfo Culture { get; set; } } 以及以下结果类: public class ProjectedThing { public string Id { g
public class Thing
{
public string Id { get; set; }
public string Title{ get; set; }
public string KeyId { get; set; }
public CultureInfo Culture { get; set; }
}
以及以下结果类:
public class ProjectedThing
{
public string Id { get; set; }
public string Title{ get; set; }
public string KeyId { get; set; }
public CultureInfo Culture { get; set; }
public IEnumerable<Thing> Things { get; set; }
}
公共类项目内容
{
公共字符串Id{get;set;}
公共字符串标题{get;set;}
公共字符串KeyId{get;set;}
公共文化信息文化{get;set;}
公共IEnumerable Things{get;set;}
}
如何构建保存结果类的索引
我最接近的是以下索引定义:
public class ProjectedThings : AbstractIndexCreationTask<Thing,ProjectedThing>
{
public ProjectedThings()
{
Map = docs => from doc in docs
select new
{
Title = doc.Title,
KeyId = doc.KeyId,
Culture = doc.Culture,
Things = new[] {
new Thing{
Id = doc.Id,
Title = doc.Title,
KeyId = doc.KeyId,
Culture = doc.Culture,
TitlePluralized = doc.TitlePluralized
}
}
};
Reduce = results => from r in results
group r by r.KeyId into g
select new
{
Title = g.FirstOrDefault(x => x.Id == x.KeyId).Title,
KeyId = g.Key,
Culture = g.FirstOrDefault(x => x.Id == x.KeyId).Culture,
Things = from thing in g.SelectMany(x => x.Things).Where(x => x.Id != x.KeyId)
select new
{
Id = thing.Id,
Title = thing.Title,
KeyId = thing.Key,
Culture = thing.Culture
}
};
}
}
公共类项目任务:AbstractIndexCreationTask
{
公共项目事务()
{
Map=docs=>来自文档中的文档
选择新的
{
标题=文件标题,
KeyId=doc.KeyId,
文化=文件文化,
事物=新[]{
新事物{
Id=文件Id,
标题=文件标题,
KeyId=doc.KeyId,
文化=文件文化,
TitlePluralized=doc.TitlePluralized
}
}
};
Reduce=结果=>来自结果中的r
按r.KeyId将r分组为g
选择新的
{
Title=g.FirstOrDefault(x=>x.Id==x.KeyId),
KeyId=g.Key,
区域性=g.FirstOrDefault(x=>x.Id==x.KeyId)。区域性,
Things=来自g.SelectMany中的Things(x=>x.Things)。其中(x=>x.Id!=x.KeyId)
选择新的
{
Id=thing.Id,
头衔,头衔,
KeyId=thing.Key,
文化
}
};
}
}
这几乎是在耍花招,但我无法收集标题、KeyId和文化。只填充Things属性。查看您的代码:
g.FirstOrDefault(x => x.Id == x.KeyId)
我不明白这一点,但这可能总是错误的。
您可能想要:
g.FirstOrDefault().Title,