How do you create a map/reduce index with multiple groupings in RavenDB

zonkflut

We are storing a set of documents in Raven.

public class MyDocument
{
  public string Id { get; set; }
  public string DocumentType { get; set; }
  public int ClientId { get; set; }
  public string Status { get; set; }
}

And we want to display a report of Documents grouped by both the ClientId and DocumentType so that looks like:

DocumentType  ClientHasManyOfThese Count Action
------------- -------------------- ----- ---------------------
DocumentType1 Yes                  10    LinkToListOfDocuments
DocumentType1 No                   5     LinkToListOfDocuments
DocumentType2 Yes                  12    LinkToListOfDocuments
DocumentType2 No                   15    LinkToListOfDocuments

I have created the following index but it is only returning the correct results for small numbers of documents.

public class MyDocumentCount
{
  public string DocumentType { get; set; }
  public int ClientId { get; set; }
  public int Count { get; set; }
  public bool MultipleDocumentsForClient { get; set; }
}

public class MyIndex : AbstractIndexCreationTask<MyDocument, MyDocumentCount>
{
  public MyIndex()
  {
    Map = tasks => 
      from task in tasks
      where task.Status = "Show In Report"
      select new MyDocumentCount
      {
        DocumentType = task.DocumentType,
        ClientId = task.ClientId,
        MultipleDocumentsForClient = false,
        Count = 1
      };

    Reduce = results =>
      results.GroupBy(result => new 
      {
        result.DocumentType, 
        result.ClientId
      }).Select(conDocGrp => new MyDocumentCount 
      {
        DocumentType = conDocGrp.Key.DocumentType,
        Count = conDocGrp.Sum(result => result.Count),
        MultipleDocumentsForClient = conDocGrp.Sum(result => result.Count) > 1,
        ClientId = conDocGrp.Key.ClientId
      });

    TransformResults = (database, results) =>
      results.GroupBy(result => new
      {
        result.DocumentType,
        result.MultipleDocumentsForClient
      }).Select(multDocGrp => new
      {
        multDocGrp.Key.DocumentType,
        multDocGrp.Key.MultipleDocumentsForClient,
        Count = multDocGrp.Sum(result => int.Parse(result.Count.ToString(CultureInfo.InvariantCulture))),
        ClientId = 0
      });
  }
}

I believe that it has something to do with the result count limit in Raven when calling:

var results = session.Query<MyDocumentCount, MyIndex>().ToList();

Maybe the limit is applied to the index results before performing the transform?

Could anyone tell me what I am doing wrong and if there is a way to achieve what I am wanting?

We are currently running RavenDB (Server Build 2380).

Thanks.

gaunacode.com

So the basic gist of the problem from what I can gather is that you're trying to aggregate an aggregation. Specifically, you're trying to group by ClientId and DocumentType and then you're trying to aggregate those results by MultipleDocumentsForClient. Your index works on most cases but when the Reduce produces more results than the default RavenDB 'page size' limit, you don't get the desired outputs.

I confirmed that the TransformResults only receives up to the page limit size from RavenDB. You can think of TransformResults as executing on the client side to make sure you don't make any mistakes later. Maybe that's why it was deprecated and we should use Transformers instead.

To solve your problem right now, I think you're doing too much in one index. The transformer part is not really used to transform the results from the query, instead it's being used to aggregate again. If you can't do all the aggregation in the Reduce portion of the index, then I recommend that you try to split the index into two smaller indexes. Perhaps in this case, one index could be for when the client has multiple docs and one could be for when the client has single docs. Then you would have to load both results into memory, which seems to suit your case since you were already using .ToList on your query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a search index by related document field in RavenDb?

From Dev

How do you create and index Apple Watch interface pages

From Dev

How do you create a Postgresql JSONB array in array index?

From Dev

How do you calculate the time to create an index on a large Postgresql table?

From Dev

How do you find the first index of multiple 'motifs' in a sequence?

From Dev

How do you create multiple strokes on text in WPF?

From Dev

How do you create a conditional count across multiple fields?

From Dev

How do you create multiple JLabels with a while loop

From Dev

How to include multiple groupings with accumulators in a Morphia query?

From Dev

How do you create a Rakefile?

From Dev

How do you create a Rakefile?

From Dev

How do you create indicators?

From Dev

How do I write an index to get the total number of objects in a collection across documents in RavenDB?

From Dev

How do I write an index to get the total number of objects in a collection across documents in RavenDB?

From Dev

How do you edit an element by index in a JList?

From Dev

In swift 3, how do you advance an index?

From Dev

How do you read vertex/index buffers?

From Dev

How do you edit an element by index in a JList?

From Dev

How do you find the index of a character in a string?

From Dev

How do you pass index into rowRenderer?

From Dev

How do you create multiple forms on the same page with redux-forms v6?

From Dev

How do you create multiple Spring @RestControllers each with their own thread pool?

From Dev

How do you create a method in C# that takes a multiple set amount of values?

From Dev

How do you create MS Graph open extensions with the same id on multiple users?

From Dev

How do I use Castle Windsor to create a RavenDB session with client version > 3.0.3660?

From Dev

How to create groupings of constants similar to "dynamic enumerations" in Java?

From Dev

How to include missing data for multiple groupings within the time span?

From Dev

How to get multiple disparate row groupings in a tablix to function?

From Dev

How to include missing data for multiple groupings within the time span?

Related Related

  1. 1

    How to create a search index by related document field in RavenDb?

  2. 2

    How do you create and index Apple Watch interface pages

  3. 3

    How do you create a Postgresql JSONB array in array index?

  4. 4

    How do you calculate the time to create an index on a large Postgresql table?

  5. 5

    How do you find the first index of multiple 'motifs' in a sequence?

  6. 6

    How do you create multiple strokes on text in WPF?

  7. 7

    How do you create a conditional count across multiple fields?

  8. 8

    How do you create multiple JLabels with a while loop

  9. 9

    How to include multiple groupings with accumulators in a Morphia query?

  10. 10

    How do you create a Rakefile?

  11. 11

    How do you create a Rakefile?

  12. 12

    How do you create indicators?

  13. 13

    How do I write an index to get the total number of objects in a collection across documents in RavenDB?

  14. 14

    How do I write an index to get the total number of objects in a collection across documents in RavenDB?

  15. 15

    How do you edit an element by index in a JList?

  16. 16

    In swift 3, how do you advance an index?

  17. 17

    How do you read vertex/index buffers?

  18. 18

    How do you edit an element by index in a JList?

  19. 19

    How do you find the index of a character in a string?

  20. 20

    How do you pass index into rowRenderer?

  21. 21

    How do you create multiple forms on the same page with redux-forms v6?

  22. 22

    How do you create multiple Spring @RestControllers each with their own thread pool?

  23. 23

    How do you create a method in C# that takes a multiple set amount of values?

  24. 24

    How do you create MS Graph open extensions with the same id on multiple users?

  25. 25

    How do I use Castle Windsor to create a RavenDB session with client version > 3.0.3660?

  26. 26

    How to create groupings of constants similar to "dynamic enumerations" in Java?

  27. 27

    How to include missing data for multiple groupings within the time span?

  28. 28

    How to get multiple disparate row groupings in a tablix to function?

  29. 29

    How to include missing data for multiple groupings within the time span?

HotTag

Archive