Recursive Traversals

Recursive traversals allow iterative processing over traversal paths. Prior to trying out branching traversals shown here, you must create the data as described in Simple Traversals.

This recursive traversal example returns the names of vertices that are two outgoing steps from the author vertex named Julia CHILD using the times(2) step. Books, meals, and ingredients are returned by this query. However, if you tried to run this recursive query, you probably discovered that many indexes were required. A person in the food graph must index all edges to any vertex label to execute. Using the repeat() step should be carefully targeted unless exploration is the intended use and the graph is small.

// Return the names of the vertices 2 outgoing steps from the vertex named "Julia CHILD".
// This will likely include cookbooks, meals, and ingredients

g.V().has('name','Julia CHILD').repeat(out()).times(2).valueMap()

The output for this traversal lists each result:

==>{ingred_id=[3001], name=[beef]}
==>{ingred_id=[3002], name=[onion]}
==>{ingred_id=[3003], name=[mashed garlic]}
==>{ingred_id=[3003], name=[mashed garlic]}
==>{ingred_id=[3004], name=[butter]}
==>{ingred_id=[3005], name=[tomato paste]}
==>{type=[lunch], meal_id=[4001]}
==>{type=[lunch], meal_id=[4001]}
==>{ingred_id=[3008], name=[olive oil]}
==>{ingred_id=[3008], name=[olive oil]}
==>{ingred_id=[3010], name=[green beans]}
==>{ingred_id=[3011], name=[tuna]}
==>{publish_year=[1961], name=[The Art of French Cooking, Vol. 1], book_id=[1001], category=[[French, cooking, general]]}
==>{publish_year=[1961], name=[The Art of French Cooking, Vol. 1], book_id=[1001], category=[[French, cooking, general]]}
==>{ingred_id=[3012], name=[tomato]}
==>{ingred_id=[3013], name=[hard-boiled egg]}
==>{ingred_id=[3007], name=[zucchini]}
==>{ingred_id=[3006], name=[eggplant]}
==>{ingred_id=[3009], name=[yellow onion]}

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com