Raven4 - Query help - Find Tags In Posts

166 views
Skip to first unread message

Andy Knight

unread,
Aug 6, 2018, 4:26:26 PM8/6/18
to RavenDB - 2nd generation document database
Hi all,
Just a simple problem. Given the following code, I'm trying to create queries such as:
1. Find all posts that have a specific tag
2. List all tags and count of posts for each.

public class Post
{
    public List<string> Tags {get;set;}
    // shortened for brevity
}

So I looked through Oren's old posts such as this one:

but if I write the following simple query in Raven studio, it fails:

from Posts 
as p
select tag in p.Tags
select new {tag, count = 1}

"Raven.Server.Documents.Queries.Parser.QueryParser+ParseException: 3:13 Expected end of query but got: in"

Whats the correct syntax to extract all items in a sub collection so that I can group and filter them?

regards

Andy

Oren Eini (Ayende Rahien)

unread,
Aug 7, 2018, 2:46:10 AM8/7/18
to ravendb
Hi,
The issue is a bit complex. Because you want two separate things, and they deal with the data differently.
The first query, first all posts with a specific tag:

from Posts where Tags = $tag

This will find all the documents with the right tag. The output of this is the the full documents that has this tag.

For getting count of posts in a specific tag, use:

from Posts
group by Tags[]
where Tags[] = $tag
select key() as Tag, count() as Count

This will use "Tags[]" to have a separate value per tag list in the post and create a map/reduce index behind the scenes to maintain this

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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andy Knight

unread,
Dec 6, 2018, 1:25:59 PM12/6/18
to RavenDB - 2nd generation document database
Sorry to drag up an old post, I've been off project.
I'm trying to convert the above count query into a compiled index. I have the following:

    public class PostTagsCountIndex : AbstractIndexCreationTask<Post>
    {
        public class Result
        {
            public string Tag { get; set; }
            public int Count { get; set; }
        }

        public PostTagsCountIndex()
        {
            Map = posts => from p in posts
                          from t in p.Tags
                          select new { Tag = t, Count = 1 };

            Reduce = results => from result in results
                                group result by result into g
                                select new Result
                                {
                                    Tag = g.Key,
                                    Count = g.Sum()
                                };

        }
    }

Its doesnt want to convert g.Key (Post) into a string. But should g.Key be the grouped Tag string?

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Arkadiusz Palinski

unread,
Dec 7, 2018, 7:34:18 AM12/7/18
to rav...@googlegroups.com
Since you're trying to get the Count per Tag you should group by result.Tag

from result in results
                                group result by result.Tag into g
                                select new Result
                                {
                                    Tag = g.Key,
                                    Count = g.Sum()
                                };
--
Arkadiusz Paliński
Team Leader   /   Hibernating Rhinos LTD
Support:  sup...@ravendb.net
  

Grisha Kotler

unread,
Dec 7, 2018, 8:52:48 AM12/7/18
to rav...@googlegroups.com
Also missing:
 
- public class PostTagsCountIndex : AbstractIndexCreationTask<Post, PostTagsCountIndex.Result>
- Count = g.Sum(x => x.Count)

Grisha Kotler
Core Team Developer   /   Hibernating Rhinos LTD
Skype:  grisha.kotler
Support:  sup...@ravendb.net

Andy Knight

unread,
Dec 7, 2018, 1:16:01 PM12/7/18
to RavenDB - 2nd generation document database
Thankyou both, that worked perfectly.
Andy
Reply all
Reply to author
Forward
0 new messages