Limitation of using Union clause for Async Query in Raven DB

158 views
Skip to first unread message

arup chakraborty

unread,
Apr 6, 2015, 8:08:11 AM4/6/15
to rav...@googlegroups.com
 Hi ,
   I am working with RavenDB for our project requirement.

   There are a particular use case here as follows where we have written a Map Reduce index on the data to get the travel count per user account.

       AccountId             TravelCount      
 
        TestA                       5
        TestB                       4
        TestC                       3
        TestD                       6
        TestE                       2
        TestF                       1

     Now we have a requirement to show the top 2 travel count [i.e. greater than current user travel count] and bottom 2 [i.e. lesser than current user travel count] travel count based on the user account id.

     We are trying to perform this whole operation into a single query in Asynchronously Web API.

     public async Task<IList<IEnumerable<UserDataRountCount>>> GetUserRouteCount(string accountId)
        {
           

            return await Session
                .Query<UserDataRountCount>("CountRouteByAccount", true)
                .Customize(c => c.WaitForNonStaleResultsAsOfNow())
                .Where(q => q.AccountId == accountId)
                .Select
                (p=>
                    Session
                        .Query<UserDataRountCount>("CountRouteByAccount", true)
                        .Where(r=> r.RountCount > p.RountCount)
                        .OrderBy(l=> l.RountCount)
                        .Take(1)
                        .ToList()
                        .Union
                        (
                            Session
                            .Query<UserDataRountCount>("CountRouteByAccount", true)
                            .Where(s => s.RountCount <= p.RountCount)
                            .OrderBy(k => k.RountCount)
                            .Take(1)
                            .ToList()
                        )
                 )
                .ToListAsync();
        }

        It always show error that cannot perform sync query in async function.

        Could you please have a look and suggest the best possible way to do this.


Thanks in advance,
Arup

   


 

Oren Eini (Ayende Rahien)

unread,
Apr 6, 2015, 8:15:08 AM4/6/15
to ravendb

Hibernating Rhinos Ltd  

Oren Eini l CEO Mobile: + 972-52-548-6969

Office: +972-4-622-7811 l Fax: +972-153-4-622-7811

 


--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

arup chakraborty

unread,
Apr 7, 2015, 11:12:46 AM4/7/15
to rav...@googlegroups.com
Thanks Eini.
I have ended up re-writing the query like this way.

// get all the data from Map-Reduce Index
var leaderBoardByPoints = await session
                                .Query<LeaderBoardByPoint>(index, isMapReduce)
                                .Customize(c => c.WaitForNonStaleResultsAsOfNow())
                                .ToListAsync();

// Process the data from index , so I need to make 1 DB call on MapReduce index for all the operations
            var pointForCurrentAccount = leaderBoardByPoints
                                .First(e => e.AccountId == accountId)
                                .Point;

            return leaderBoardByPoints
                .Where(u => u.Point > pointForCurrentAccount)
                .OrderBy(d => d.Point)
                .Take(greaterNumbersOfRecords)
                .Union
                (
                    leaderBoardByPoints
                    .Where(u => u.Point <= pointForCurrentAccount)
                    .OrderByDescending(d => d.Point)
                    .Take(lesserNumbersOfRecords)
                )
                .ToList();


Thanks much.
Reply all
Reply to author
Forward
0 new messages