2

我的问题很短,我坚持下去。我有一系列文件,例如:

{
...,
"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
                                };
    }
}
4

1 回答 1

2

大家好,我找到了一个解决方案,但我不知道是否是最好的方法,因为我仍然没有字符串列表,只有结果,我解决了它通过 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
                                };
    }
}
于 2013-01-07T15:03:53.130 回答