Graphs in RavenDB: Recursive Queries

DZone 's Guide to

Graphs in RavenDB: Recursive Queries

Take a look at the next post in this series of graphs in RavenDB. Explore recursive queries and look at an example.

· Database Zone ·
Free Resource

imageGraph queries, as I discussed them so far, give you the ability to search for patterns. Above, you can see the family tree of the royal family of Great Britain going back a few hundred years. That makes for an interesting subject for practicing graph queries.

A good example we might want to ask is who is the royal grandparent of Elizabeth II. We can do that using:


This is great and nicely demonstrates how we can scan for specific patterns in the graph. However, it is limited by its rigidity. For example, let’s say that I want to find someone in the family tree and I’m not sure about the exact nature of the relationship?

“We are not amused” comes to mind, but off the top of my head and without consulting the chart, I don’t think that I would be able to figure it out. Luckily, I don’t have to, I can ask RavenDB to be so kind and tell me.


Note the use of the recursive element here. We are asking RavenDB to start in a particular document and go up the parents, trying to find an unamused royal. The recursion portion of the query can be zero to six steps in size and should abort as soon as we have any match. Following the zero to six parents, there should be a parent that is both a royal an unamused.

The Cypher syntax for what they call variable length queries is reminiscent of regular expressions, and I don’t mean that in a complementary manner. Looking at the query above, you might have noticed that there is a distinct difference between it and the first one. The recursive query will go up the Parents link, regardless of whatever that parent is royal or not. RavenDB Graph Queries has what I believe to be a unique feature. The recursive pattern isn’t limited to a single step and can be as complex as you like.

For example, let’s ensure that we are only going to go up the chain of the royal parents.


The recursive element has a few knows that you can tweak. The minimum and maximum distance, for example, are obvious examples, but the results criteria for the recursion is also interesting. In this query, we use the shortest, instead of the lazy. This will make RavenDB work a bit harder and find the shortest recursive path that matches the query, whereas lazy stops on the first one that matches. The following options are available:

  • Lazy — stop on the first pattern that matches. Good for: “Am I related to Victoria?”
  • Shortest — find the shortest path that matches the pattern. Good for: “How am I related to Victoria?”
  • Longest — find the longest path that matches the pattern. Good for: “For how many generations has Victoria’s family been royals?”
  • All — find all the paths that match the pattern. Good for if you have multiple paths in your ancestry to Victoria.
database, graph database, ravendb, ravendb tutorial, recursive queries, tutorial

Published at DZone with permission of Oren Eini , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}