sessionfilteringravendbpage-sizecustompaging

Querying RavenDb with max 30 requests error


Just want to get some ideas from anyone who have encountered similar problems and how did you guys come up with the solution.

Basically, we have around 10K documents stored in RavenDB. And we need the ability to allow users to perform filter and search against those documents. I am aware that there is a maximum of 1024 page size within RavenDb. So in order for the filter and search to work, I need to do my own paging. But my solution gives me the following error:

The maximum number of requests (30) allowed for this session has been reached.

I have tried many different ways of disposing the session by wrapping it around using keyword and also explicitly calling Dispose after every call to RavenDb with no success.

Does anyone know how to get around this issue? what's the best practice for this kind of scenario?

var pageSize = 1024;
var skipSize = 0;
var maxSize = 0;

using (_documentSession)
{
    maxSize = _documentSession.Query<LogEvent>().Count(); 
}                                   

while (skipSize < maxSize)
{
    using (_documentSession)
    {
        var events = _documentSession.Query<LogEvent>().Skip(skipSize).Take(pageSize).ToList();

        _documentSession.Dispose();             

        //building finalPredicate codes..... which i am not providing here....

        results.AddRange(events.Where(finalPredicate.Compile()).ToList());

        skipSize += pageSize;
    }  
}

Solution

  • Raven limits the number of Request (Load, Query, ...) to 30 per Session. This behavior is documented.

    I can see that you dispose the session in your code. But I don't see where you recreating the session. Anyways loading data they way you intend to do is not a good idea.

    We're using indexes and paging and never load more than 1024.

    If you're expecting thousands of documents or your precicate logic doesn't work as an index and you don't care about how long your query will take use the unbounded results API.

    var results = new List<LogEvent>();
    var query = session.Query<LogEvent>();
    
    using (var enumerator = session.Advanced.Stream(query))
    {
        while (enumerator.MoveNext())
        {
            if (predicate(enumerator.Current.Document)) {
                results.Add(enumerator.Current.Document);
            }
        }
    }
    

    Depending on the amount of document this will use a lot of RAM.