我正在通过在项目中使用 RavendDb 并尝试做一些事情来学习它。我没有任何 SQL/关系数据库经验背景,这就是为什么我发现使用 map reduce 和文档数据库更容易。
我试图创建一个静态索引来创建一个对象,该对象保存 4 个条件字段的出现次数,而不是创建 4 个静态索引并在 4 个数据库查询后组合结果。
这是静态索引:
public class Client_ProductDeploymentSummary : AbstractIndexCreationTask<Product, ClientProductDeploymentResult>
{
public Client_ProductDeploymentSummary()
{
Map = products =>
from product in products
select new {
product.ClientName,
NumberProducts = 1,
NumberProductsWithCondition = 0,
NumberProductsWithoutCondition = 0,
NumberProductsConditionTestInconclusive = 0
};
Map = products =>
from product in products
where product.TestResults.Condition == true
select new
{
product.ClientName,
NumberProducts = 0,
NumberProductsWithCondition = 1,
NumberProductsWithoutCondition = 0,
NumberProductsConditionTestInconclusive = 0
};
Map = products =>
from product in products
where product.TestResults.Condition == false
select new
{
product.ClientName,
NumberProducts = 0,
NumberProductsWithCondition = 0,
NumberProductsWithoutCondition = 1,
NumberProductsConditionTestInconclusive = 0
};
Map = products =>
from product in products
where product.TestResults.Condition == null
select new
{
product.ClientName,
NumberProducts = 0,
NumberProductsWithCondition = 0,
NumberProductsWithoutCondition = 0,
NumberProductsConditionTestInconclusive = 1
};
Reduce = results =>
from result in results
group result by result.ClientName
into g
select new ClientProductDeploymentResult()
{
ClientName = g.Key,
NumberProducts = g.Sum(x => x.NumberProducts),
NumberProductsWithCondition = g.Sum(x => x.NumberProductsWithCondition),
NumberProductsWithoutCondition = g.Sum(x => x.NumberProductsWithoutCondition),
NumberProductsConditionTestInconclusive = g.Sum(x => x.NumberProductsConditionTestInconclusive)
};
}
}
我将 4 个变量添加到每个 select new 语句中,以使索引在我的单元测试中编译和部署。正如我在一些示例中看到的那样,我似乎无法使用 AddMap(...) 函数(我意识到我只是覆盖了 Map 变量)。没有那么多客户,在 10 或 100 中,但可能有很多产品,每个客户肯定在 1000 中。
有没有一种简洁的方法来构建这个索引的意图?还是每个字段使用一个 map reduce 并将结果组合到调用者代码中是更好的方法?