Hierarchy Match

Just as it is possible to match against individual fields it is also possible to match against the hierarchy and relationships that existing within your content tree.  So lets say we want to get the latest 5 posts from our blog.  We might do something like this:

{'type':'post','parent':'8234sd875sdf6578','count':5,'orderBy':'-publishedAt'}

This would just be using a basic field match against the parent id.  This is rather inconvenient because the id is a bit ugly.  Well, we have the option of doing a relationship match as well:

{'type':'post','parent':{'type':'feed','title':'Blog'},'count':5,'orderBy':'-publishedAt'}

That is much better.  HiFi is doing a recursive query.  This would return 5 posts, ordered by newest first from ANY feed with the title matching Blog.  In most cases, this would just be one blog, but it would also work if you had two feeds with the title "Blog".

It is possible to combine all of the hierarchy matches if you like.  For example, here is a query that shows the latest 5 posts with a parent with the url '/blog', that have comments, that are related to the '/development' category:

{'type':'post','parent':{'type':'feed','url':'/blog'},'child':{'type':'comment'},'related':{'to':category.id,'relation':'category'},'count':5,'orderBy':'-publishedAt'}

Child

Using the child hierarchy match, you can make sure that the results you get back have a certain type of child. Too abstract? A great example is wanting to fetch the newest blog posts that have comments.  Here is how you would do this:

{'type':'post','child':{'type':'comment'},'count':5,'orderBy':'-publishedAt'}

As you can imagine, this can make your queries quite powerful. Just like with 'parent' it is possible to make the subquery as complex as you like.  Here is an example getting the newest posts that have a comment written by Joel or Josh.

{'type':'post','child':{'type':'comment','author':['Joel Sutherland','Josh Lockhart']},'count':5,'orderBy':'-publishedAt'}

Parent

This is probably the most common hierarchy match that will get used in your templates.  When you use a parent hierarchy match, you essentially saying "find me items whose parent looks like this".  A really common example would be to get the latest three blog posts to show on the homepage.  Here is how you might do it:

{'type':'post','parent':{'type':'feed','url':'/blog'},'count':5,'orderBy':'-publishedAt'}

This will get the newest five posts whose parent is a feed with the url '/blog'.  When you are putting a subobject in the parent field, there are no additional rules.  You can do any kind of match.  A great example of this is grabbing the latest items from EITHER a blog OR a news section.  Here is how you would do that:

{'type':'page','parent':{'type':'content','url':['/blog','/news']},'count':5,'orderBy':'-publishedAt'}

I am now getting the newest five pages with a parent that has '/blog' or '/news' as the url.  Notice that in both my primary query and my subquery I made the types more generic.  It is easy to forget to add the correct types to your query, so remember to pay attention to this.