RavenDb: combining data with an index

There is a list of cases in my database:

{ Id: 1, Owner: "guid1", Text: "Question1" } { Id: 2, Owner: "guid1", Text: "Question2" } { Id: 3, Owner: "guid2", Text: "Question3" } 

When requesting data, I would also like to have (in my index, the result) the number of cases, each of which has an owner. Therefore, I created a map / reduce index in this collection:

 public class RelatedCases { public Guid Owner { get; set; } public int Count { get; set; } } public class RelatedCaseIndex : AbstractMultiMapIndexCreationTask<RelatedCases> { public RelatedCaseIndex() { AddMap<CaseDocument> (c => c.Select(a => new { a.Owner, Count = 1 })); Reduce = result => result .GroupBy(a => a.Owner) .Select(a => new { Owner = a.Key, Count = a.Sum(b => b.Count) }); } } 

Now I just don’t know how to create a query to include data from the index. Based on the documentation, I tried something like:

 session.Query<CaseDocument>().Customize(a => a.Include ...) 

or TransformResults on CaseIndex, which is not working properly.

I know that I can simply request a raven to get a list of all related persons in a separate request, but I would like to do it at a time.

+4
source share
2 answers

You cannot query Cases and join the result with a map / reduce index on the fly. It's just not how it works, because each query will work against the index, so what you really ask is combining the two indexes. This is what you need to do in advance.

In other words - put all the information you want to request into your map / down index. Then you can run a query on this index and .Include () the documents that interest you as well.

+3
source

I don’t think you need a MultiMap index, a simple MapReduce index is enough for this.

Then you can query it like this:

 session.Query<RelatedCases, RelatedCaseIndex>(); 

This will return a list of related persons with the owner and counter.

-1
source

Source: https://habr.com/ru/post/1403877/


All Articles