Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does this RavenDB linq query work

Tags:

linq

ravendb

I was looking at the source code for RacoonBlog trying to find a way in RavenDB to query on a collection contained in a document. I did read about indexes and Map / Reduce and failed to find my answer.

In the PostsController there is an ActionResult called Tag that takes a string parameter and contains the following linq query.

var posts = RavenSession.Query<Post>()
    .Include(x => x.AuthorId)
    .Statistics(out stats)
    .WhereIsPublicPost()
    .Where(post => post.TagsAsSlugs.Any(postTag => postTag == slug))
    .OrderByDescending(post => post.PublishAt)
    .Paging(CurrentPage, DefaultPage, PageSize)
    .ToList();

The Where extension method calls TagsAsSlugs and performs an Any, TagsAsSlugs looks like this.

    public IEnumerable<string> TagsAsSlugs
    {
        get
        {
            if (Tags == null)
                yield break;
            foreach (var tag in Tags)
            {
                yield return SlugConverter.TitleToSlug(tag);
            }
        }
    }

So since the TagsAsSlugs property loops over the collection of tags does the query require that all posts are returned so that each post can have its Tags collection iterated over?

I doubt this is the case since Oren's blog is so fast.

like image 789
W.Jackson Avatar asked Jan 26 '26 13:01

W.Jackson


1 Answers

Jackson, No, that is NOT how it works. We are doing the work during indexing (the TagsAsSlugs is actually computed on save time), and then we save TagsAsSlugs into the index. We query the index for tags that exists there.

In short, we don't do any computation, certainly not on the client side.

like image 156
Ayende Rahien Avatar answered Jan 28 '26 16:01

Ayende Rahien