Please create your FREE account or login here

**SELECT A PATH TO SWITCH TO FORMAL LEARNING**

### Progress:

Please login to view your progress.

### Related Content

**Simple Traversals**

**DSE Version: **6.0

Video

Simple traversals is a really good place to start learning about Gremlin, because they are easy to understand. In this unit, you will learn about simple traversals.

Hi, I am Artem Chebotko and this is Simple Traversals.

Simple traversals is a really good place to start learning about Gremlin. They should be easy to understand because simple traversals guide traversers through steps in sequential order and contain no internal or nested traversals.

You can imagine Gremlin, the traverser, guided by a simple traversal, hopping from one vertex to the next, as shown in this illustration.

Let’s look at some of the steps we use to define simple traversals. And of course these steps will be useful in other kinds of traversals as well. They really form a foundation for any of the traversals we are going to work with.

Starting at a vertex, here are some steps we can use to navigate to an adjacent vertex or an incident edge.

“out” instructs a traverser to move to any adjacent vertex that is connected to our current vertex via an outgoing edge with a matching label. Labels that we pass into “out” and all other steps are optional.

“in” tells the traverser to move to an adjacent vertex by traversing an incoming edge.

If you do not care about an edge direction, you can use step “both” to move to adjacent vertices by traversing edges in any direction.

If you want to navigate to an edge, you have outE, inE, and bothE. They work the same as out, in, and both, except they move to an edge rather than a vertex. The reason you may want to go to an edge is usually to access edge properties.

Of course, a picture always helps when we are talking about graphs.

Check this sample graph out. Our traverser (the little gremlin) is on the movie-vertex and there are various places he can go depending on a step we want to use.

For example, “out” will move the traverser to the genre-vertex and the person-vertex and we will end up with two traversers.

If instead we use the “in” step, the traverser will move to the user-vertex.

Similarly, you can see how the traverser would move if other steps are used.

Also, please notice that we are not specifying any labels for these steps in the illustration. This is a valid use case as labels are always optional. We can always add labels as needed.

If our traverser is on an edge, we have a different set of steps that we can use.

The outV step is going to move the traverser to the opposite direction of the edge towards the outgoing vertex. In this case, this is the user-vertex.

inV will move the traverser to the incoming movie-vertex.

And bothV will go to both endpoint of the edge.

Finally, the otherV step is a bit unique here. It implies that the traverser has some state. otherV will move the traverser to the vertex that was not the vertex that the traverser moved to the edge from. For example, if we moved to the edge from movie, otherV will send us to the user. And if we came from the user, otherV will direct us to the movie.

The graph schema we are going to use for all of our examples is based on the KillrVideo domain.

In our graph, we have vertices that represent movies, users, genres and people. These are four types of vertices. They can be related to each other by different types of relationships.

Users may know each other and can rate movies. Movies belong to genres. Movies can have actors, directors, composers, screenwriters, and cinematographers.

And of course we have various vertex properties and one edge property in this schema. User-defined vertex IDs are underlined. We have no meta-properties but we do have one multi-property … that is the production property.

Please take a moment to study this schema as it will help you to follow our traversal examples.

Here is out first simple traversal with the in step.

We want to find titles of all movies with actor Johnny Depp that were released in 2010 or later.

We start with the vertex with label “person” and the name-property Johnny Depp.

We then traverse incoming actor-edges and end up on any movie where Johnny Depp acted in.

We further apply the condition for the movie release year … it must be equal or greater than 2010.

The final step in the traversal get the titles of those movies that satisfy the condition.

In the output, we see “Into the Woods”, “Pirates of the Caribbean: On Stranger Tides”, and “Alice in Wonderland”.

We used this has step a few times already. Now is a good time to take a closer look at “has” as this step is very common. We will see it in all types of traversals, not just simple traversals.

It turns out there are several variations of this step that take different parameters.

First, there is a test whether an element has a certain property key. For example, we may want to check if a vertex has a property title.

Second, there is a test for an equality to a particular value. We may be interested in a vertex with title “Alice in Wonderland”.

Third, we can use this variation with a predicate to test inequalities and more complex conditions.

Fourth, you can even have a nested traversal as a test.

Fifth, you can and should include a label as part of a test whenever possible for better efficiency.

And so forth … you should spend some time learning about other variations including those that are not mentioned here.

This example uses both in and out steps in one traversal.

This traversal looks for directors of Johnny Depp’s movies released in 2010 or later.

You may notice that we took the previous example and made it a bit more interesting.

We start with the person Johnny Depp vertex, move to the movie-vertices where Johnny Depp acted in, limit the movies to only those that were released in 2010 or later, and …

the new part is we traverse outgoing director-edged using the out step to get to people who directed those movies and return their names. We get three name but only two of them are distinct.

Here, to find user ratings for Johnny Depp’s movies released in 2010 or later, we will need to end up on rated-edges and retrieve rating values associated with those ages.

This is achieved by getting to Johnny Depp’s movies from 2010 or later and using step inE to get to the rated-edges.

The result is a bunch of numbers representing user ratings.

Let’s learn more about predicates.

Some of these are really intuitive and don’t require a lot of explanation ...

like equal, not-equal, less-than, less-than-or-equal, greater-than, greater-than-or-equal are usually used to compare values of properties. You can also test equality and inequality of objects in a graph.

“inside” returns true if the incoming number is greater than the first number and less than the second.

“outside” returns true if the incoming number less than the first number or greater than the second.

“between” returns true if the incoming number greater than or equal to the first number and less than the second.

“within” expects to take a list of one or more objects and it returns true if the incoming object is represented in that list.

“without” is like an inverse or negation of “within”... if the incoming object is not in the list, it returns true … if it is in the list, “without” returns false.

So far we only used predicates with the has step but there are other steps like “where” and “is” that also use predicates. Keep this list of predicates handy as they will pop up in many different types of traversals throughout this training.

Now let’s ask a slightly more interesting question of our movie database. Suppose you want to know how old is a typical user who likes but does not love the Johnny Depp movies made in 2010 or later.

Well we can do that by again going to the Johnny Depp person vertex, traversing incoming actor-edges to movies and applying the greater-or-equal-than 2010 predicate on year, moving to incoming rated-edges and making sure that ratings are either 7 or 8, jumping to outgoing user-vertices and outputting user ages.

All steps used in this traversal should be familiar. And even though we call this traversal “simple”, it is really quite useful. You will be surprised how many real-life data retrieval problems can be solved using simple traversals.

It is time for you to be dangerous. Let’s do it!