简体   繁体   中英

RavenDB Map Reduce Transform fold a distinct list of strings

My question is very short and i stucked on it. I have a collection of documents like:

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

And I think that I need to create a map/reduce index to group those values into a list of strings, for example if I have this two documents:

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

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

I need a result of one list with all distinct values like {"Type1","Type2","Type3","Type4"}

I don't know how to do it, I tried several ways but without success.

I forgot to say, I have a large amount of data, like more than 1500 documents for now

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
                                };
    }
}

Hi all i found a solution but i dont know if is the best way to do it, because i still dont have the list of strings only the results, i solved it populating the list of strings via foreach, here is the index creation. If someone can check the code i'll very appreciate it.

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
                                };
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM