2

我有以下 RavenDB 索引。

public class MyDomain_WeeklyPerYear : AbstractIndexCreationTask<Entities.MyDomain, MyDomain_YearlyTotals.Result>
{
    public class Result
    {
        public int ProcessWeek { get; set; }
        public int ProcessYear { get; set; }
        public int Total { get; set; }
        public object Weeks { get; set; }
        public string Workspace { get; set; }
    }

    public MyDomain_WeeklyPerYear()
    {
        Map = data => data
            .Select(x => new
            {
                x.ProcessWeek,
                x.ProcessYear,
                x.Workspace,
                Total = 1,
                Weeks = (List<object>) null
            });

        Reduce = results => results
            .GroupBy(x => x.ProcessYear)
            .OrderBy(x => x.Key)
            .Select(x => new
            {
                ProcessWeek = 0,
                ProcessYear = x.Key,
                Workspace = (string) null,
                Total = 0,
                Weeks = x
                    .GroupBy(y => y.ProcessWeek)
                    .OrderBy(y => y.Key)
                    .Select(y => new
                    {
                        Week = y.Key,
                        Total = y.Sum(z => z.Total)
                    })
                    .ToList()
            });
    }
}

当我使用以下查询运行它时,我得到以下结果。

var data = await Session
    .Query<MyDomain_WeeklyPerYear.Result, MyDomain_WeeklyPerYear>()
    .ToListAsync();
[
    {
        "processWeek": 0,
        "processYear": 2012,
        "total": 0,
        "weeks": [
            {
                "Week": 1,
                "Total": 8
            },
            {
                "Week": 49,
                "Total": 4
            },
            {
                "Week": 50,
                "Total": 6
            },
            {
                "Week": 51,
                "Total": 11
            },
            {
                "Week": 52,
                "Total": 4
            }
        ],
        "workspace": null
    },
    {
        "processWeek": 0,
        "processYear": 2013,
        "total": 0,
        "weeks": [
            {
                "Week": 0,
                "Total": 0
            }
        ],
        "workspace": null
    },
    {
        "processWeek": 0,
        "processYear": 2014,
        "total": 0,
        "weeks": [
            {
                "Week": 0,
                "Total": 0
            }
        ],
        "workspace": null
    },
    {
        "processWeek": 0,
        "processYear": 2015,
        "total": 0,
        "weeks": [
            {
                "Week": 0,
                "Total": 0
            }
        ],
        "workspace": null
    },
    {
        "processWeek": 0,
        "processYear": 2016,
        "total": 0,
        "weeks": [
            {
                "Week": 0,
                "Total": 0
            }
        ],
        "workspace": null
    },
    {
        "processWeek": 0,
        "processYear": 2017,
        "total": 0,
        "weeks": [
            {
                "Week": 0,
                "Total": 0
            }
        ],
        "workspace": null
    },
    {
        "processWeek": 0,
        "processYear": 2018,
        "total": 0,
        "weeks": [
            {
                "Week": 0,
                "Total": 0
            }
        ],
        "workspace": null
    },
    {
        "processWeek": 0,
        "processYear": 2019,
        "total": 0,
        "weeks": [
            {
                "Week": 2,
                "Total": 14
            },
            {
                "Week": 3,
                "Total": 30
            },
            {
                "Week": 4,
                "Total": 42
            },
            {
                "Week": 5,
                "Total": 52
            },
            {
                "Week": 6,
                "Total": 51
            },
            {
                "Week": 7,
                "Total": 39
            },
            {
                "Week": 8,
                "Total": 53
            },
            {
                "Week": 9,
                "Total": 47
            },
            {
                "Week": 10,
                "Total": 52
            }
        ],
        "workspace": null
    }
]

数据的结构是我想要的正确 JSON 结构,但它似乎遗漏了很多数据。我在 2012 年和 2019 年得到结果(这是正确的)。但其他年份是空的,尽管我 100% 确定它们包含的数据比 2012 年和 2019 年要多得多。

当我将 reduce 更改为以下内容时(删除了第二个.Select()):

Reduce = results => results
    .GroupBy(x => x.ProcessYear)
    .OrderBy(x => x.Key)
    .Select(x => new
    {
        ProcessWeek = 0,
        ProcessYear = x.Key,
        Workspace = (string) null,
        Total = 0,
        Weeks = x
            .GroupBy(y => y.ProcessWeek)
            .OrderBy(y => y.Key)
            .ToList()
    });

结果,它给出了如此奇怪的结构(参见 2013 年的一小部分内容,因为完整的 JSON 非常长)。

[
    {
        ... other years
    },
    {
        "processWeek": 0,
        "processYear": 2013,
        "total": 0,
        "weeks": [
            {
                "ProcessWeek": 0,
                "ProcessYear": 2013,
                "Workspace": null,
                "Total": 0,
                "Weeks": [
                    [
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 7,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        }
                    ],
                    [
                        {
                            "ProcessWeek": 12,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 12,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                        {
                            "ProcessWeek": 12,
                            "ProcessYear": 2013,
                            "Workspace": "my-workspace",
                            "Total": 1,
                            "Weeks": null
                        },
                    ]
                ]
            }
        ]
    },
    {
        ... other years
    }
]

如您所见,2013 年有数据,即使在第一个 JSON 结果中它没有显示它,但它有一个我没想到的奇怪结构。我认为这可能就是为什么它没有在第一次减少中显示 2013 年的数据。

我的问题:

  1. 为什么它在第一次减少时给了我正确的结构和一些正确的数据(在 2012 年和 2019 年)但错过了其他年份?
  2. 为什么它给了我奇怪的 JSON 结构,在第二个 reduce 中,显示其他年份有数据?
  3. 如何从第一个 JSON 示例中获取结构中的所有数据?

我已经在这个问题上停留了很长一段时间,我们将不胜感激。

RavenDB Client: 4.1.4
RavenDB Server: 4.1.3
4

1 回答 1

2

问题是您在reduce 中创建假数据,但reduce 被多次调用。你必须能够递归地处理它。尝试这样的事情:

public MyDomain_WeeklyPerYear()
{
    Map = data => data
        .Select(x => new
        {
            x.ProcessYear,
            Weeks = new [] {
                new {Week = x.ProcessWeek, Total = 1}
            }
        });

    Reduce = results => results
        .GroupBy(x => x.ProcessYear)
        .Select(x => new
        {
            ProcessYear = x.Key,
            Weeks = x.SelectMany(y=>y.Weeks)
                .GroupBy(y => y.ProcessWeek)
                .Select(y => new
                {
                    Week = y.Key,
                    Total = y.Sum(z => z.Total)
                })
        });
}
于 2019-03-03T09:11:17.417 回答