RavenDB Map Reduce Transform折叠一个不同的字符串列表



我的问题很短,我卡在上面了。我有一个文档集合,如:

{
...,
"Type": [
    "Type1",
    "Type2",
    "Type3",
    "Type4"
  ],
...
}

我认为我需要创建一个map/reduce索引,将这些值分组到字符串列表中,例如,如果我有这两个文档:

{
...,
"Type": [
    "Type1",
    "Type3"
  ],
...
}
{
...
"Type": [
    "Type2",
    "Type3",
    "Type4"
  ],
...
}

我需要一个包含所有不同值的列表的结果,例如{"Type1","Type2","Type3","Type4"}

我不知道怎么做,我试了好几种方法都没有成功。

我忘了说,我有大量的数据,目前有超过1500个文档

public class ProjectTypeIndex : AbstractIndexCreationTask<Project, ProjectTypeIndex.ReduceResult>
{
    public class ReduceResult
    {
        public string ProjectType { get; set; }
    }
    public ProjectTypeIndex()
    {
        Map = projects => from project in projects
                                   select new
                                       {
                                           project.Type
                                       };
        Reduce = results => from result in results
                            group result by result.Type into g 
                            select new
                                {
                                    ProjectType = g.Key
                                };
    }
}

嗨,我找到了一个解决方案,但我不知道这是最好的方法,因为我仍然没有字符串列表只有结果,我解决了它通过foreach填充字符串列表,这里是索引创建。如果有人能检查一下代码,我会很感激的。

public class ProjectTypeIndex : AbstractIndexCreationTask<Project, ProjectTypeIndex.ReduceResult>
{
    public class ReduceResult
    {
        public string ProjectType { get; set; }
    }
    public ProjectTypeIndex()
    {
        Map = projects => from project in projects
                                   from type in project.Type
                                   select new
                                       {
                                           ProjectType = type
                                       };
        Reduce = results => from result in results
                            group result by result.ProjectType into g
                            select new
                                {
                                    ProjectType = g.Key
                                };
    }
}

相关内容

  • 没有找到相关文章

最新更新