質問

I'm learning RavendDb by using it in a project and trying to do stuff. I don't have any background in SQL/relational db experience, which is why I find it easier to use map reduce and document databases.

I am attempting to make one static index to create an object holding the count the occurrence of 4 conditions fields instead of making 4 static indexes and combining the result after 4 database queries.

Here is the static index:

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

I added the 4 variables to each select new statements to make the index compile and deploy in my unit test. I can't seem to use the AddMap(...) function as i've seen in some examples (i realize i'm just overwriting the Map variable). There are not so many Clients, in the 10s or 100s, but possibly many Products, definitely in the 1000s per client.

Is there a concise way to construct the intent of this index? Or is one map reduce for each field and combining the results in caller code the better way to go?

役に立ちましたか?

解決

MultiMap indexes have a different base class. You would inherit from AbstractMultiMapIndexCreationTask to create a multimap index.

However, what you describe here is not suited for multimap. You use multimap when the data is coming from different source documents, not when the conditions are different. What you need is a single map statement that has your conditional logic inline.

Map = products =>
  from product in products
  select new {
    product.ClientName, 
    NumberProducts = 1,
    NumberProductsWithCondition = product.TestResults.Condition == true ? 1 : 0,
    NumberProductsWithoutCondition = product.TestResults.Condition == false? 0 : 1,
    NumberProductsConditionTestInconclusive = product.TestResults.Condition == null ? 1 : 0
  };
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top