嵌套的可变长度数组上的RavenDB Map / Reduce / Transform

时间:2013-07-04 18:12:38

标签: mapreduce ravendb ravenhq

我是RavenDB的新手,到目前为止我很喜欢它。我有一个剩余的索引要为我的项目创建。

问题

我有数以千计的调查回复(即“Submissions”),每个提交都有一系列特定问题的答案(即“Answers”),每个答案都有一个选择的选项(即“Values”)。

以下是单个Submission的基本外观:

{
  "SurveyId": 1,
  "LocationId": 1,
  "Answers": [
    {
      "QuestionId": 1,
      "Values": [2,8,32],
      "Comment": null
    },
    {
      "QuestionId": 2,
      "Values": [4],
      "Comment": "Lorem ipsum"
    },
    ...more answers...
  ]
}

更多问题:我必须能够按SurveyId,LocationId,QuestionId,Creation Date进行过滤。据我所知,这是在查询时完成的...我只需要确保转换结果中存在这些属性(或者是减少结果?还是两者都有?)。如果我是对的,那么这不是一个问题。

必填结果

每个调查每个问题需要一个对象,它给出了每个选项的总和。希望它是自我解释的:

[
    {
        SurveyId: 1,
        QuestionId: 1,
        NumResponses: 976,
        NumComments: 273,
        Values: {
            "1": 452, // option 1 selected 452 times
            "2": 392, // option 2 selected 392 times
            "4": 785  // option 4 selected 785 times
        }
    },
    {
        SurveyId: 1,
        QuestionId: 2,
        NumResponses: 921,
        NumComments: 46,
        Values: {
            "1": 325,
            "2": 843,
            "4": 119,
            "8": 346,
            "32": 524
        }
    },
    ...
]

我的尝试

我没有走得太远,我认为this post正朝着正确的道路前进,但它并没有帮助我获得值列表。我已经搜索过并搜索过但无法找到任何关于嵌套数组的方向。这是我到目前为止:

MAP:

from submission in docs.Submissions
from answer in submission.Answers
where answer.WasSkipped != true && answer.Value != null
select new {
    SubmissionDate = submission["@metadata"]["Last-Modified"],
    SurveyId = submission.SurveyId,
    LocationId = submission.LocationId,
    QuestionId = answer.QuestionId,
    Value = answer.Value
}

REDUCE:

??

TRANSFORM:

from result in results
from answer in result.Answers
where answer.WasSkipped != true && answer.Value != null
select new {
    SubmissionDate = result["@metadata"]["Last-Modified"],
    SurveyId = result.SurveyId,
    LocationId = result.LocationId,
    QuestionId = answer.QuestionId,
    Value = answer.Value
}

对于它的价值,它托管在RavenHQ上。

已经很久了,我一直在研究这个问题并且无法做到这一点。任何帮助我获得所需结果的帮助都非常感谢!

1 个答案:

答案 0 :(得分:6)

假设您的C#类看起来像这样:

public class Submission
{
    public int SurveyId { get; set; }
    public int LocationId { get; set; }
    public IList<Answer> Answers { get; set; }
}

public class Answer
{
    public int QuestionId { get; set; }
    public int[] Values { get; set; }
    public string Comment { get; set; }
}

如果您运行的是RavenDB 2.5.2637或更高版本,您现在可以使用字典结果类型:

public class Result
{
    public int SurveyId { get; set; }
    public int QuestionId { get; set; }
    public int NumResponses { get; set; }
    public int NumComments { get; set; }
    public Dictionary<int, int> Values { get; set; }
}

如果您之前正在运行任何内容(包括2.0版本),那么您将无法使用字典,但您可以改为使用IList<KeyValuePair<int,int>>

这是索引:

public class TestIndex : AbstractIndexCreationTask<Submission, Result>
{
    public TestIndex()
    {
        Map = submissions =>
              from submission in submissions
              from answer in submission.Answers
              select new
              {
                  submission.SurveyId,
                  answer.QuestionId,
                  NumResponses = 1,
                  NumComments = answer.Comment == null ? 0 : 1,
                  Values = answer.Values.ToDictionary(x => x, x => 1)
                  //Values = answer.Values.Select(x => new KeyValuePair<int, int>(x, 1))
              };

        Reduce = results =>
                 from result in results
                 group result by new { result.SurveyId, result.QuestionId }
                 into g
                 select new
                 {
                     g.Key.SurveyId,
                     g.Key.QuestionId,
                     NumResponses = g.Sum(x => x.NumResponses),
                     NumComments = g.Sum(x => x.NumComments),
                     Values = g.SelectMany(x => x.Values)
                               .GroupBy(x => x.Key)
                               .ToDictionary(x => x.Key, x => x.Sum(y => y.Value))
                               //.Select(x => new KeyValuePair<int, int>(x.Key, x.Sum(y => y.Value)))
                 };
    }
}

(不需要转换步骤。)

如果您不能使用2.5.2637或更高版本,请将.ToDictionary行替换为它们下方的注释行,并在结果类中使用IList<KeyValuePair<int,int>>

允许地图/缩减中的词典的修复程序基于您的帖子帮助识别的this issue。谢谢!