Reputation: 31
New to cayley and have cannot figure out how to display a multi level query with unknown amount of levels.
For example Lets say all predicates/relations between nodes are "likes". If I have a graph shaped this way.
A --> B
B --> C
C --> D
D --> E
...
I would like to follow the train and display the entire likes graph. What query would I use? This is what I have tried.
function getLikes(x) {
return g.V(x).As("source").Out("likes").As("target)
}
getLikes("A").ForEach( function(d) {
g.Emit(getLikes(d))
})
This only returns
{
"result": [
{
"All": {},
"And": {},
"As": {},
"Back": {},
... And all other path options
I have also tried
return g.V(x).As("source").Out("likes").As("target).All()
Instead of the second line of code. Just added the .All() at the end to finalize the query. This returns query results, but the visualization tool shows nothing. Any guidance on how to show this like graph would be much appreciated!
Upvotes: 3
Views: 814
Reputation: 732
I'm not sure if you still need this, but I'm going to write anyway as I had to learn the same thing from almost non-existent example based documentation on Cayley.
To traverse a graph, Cayley has a .FollowRecursive() method defined in the API. The method .FollowRecursive uses something called a Morphism to figure out how to follow the path recursively. From my understanding, it seems like an abstract path object that encodes the recursive case (kind-of like your getLikes function (but not quite)). Here's an example of Gizmo query that should work for complete graph/chain traversal.
var path = g.M().Out("edge_name_here");
var start_node = "begin";
//Path query
//Note that FollowRecursive expects a Morphism path
g.V(start_node).FollowRecursive(path).All() //That's all.
To visualize the entire traversal or to do some further querying on each vertex use the .ForEach() construct (see API)
g.V(start_node).FollowRecursive(path).ForEach( function(v){
//In here we can do further querying or more vaguely put "stuff"
g.V(v.id).Out(chain_pred).ForEach( function(t){
var node = { //This source, target formulation works for visualization for eg.
source: v.id,
target: t.id
}
g.Emit(node) //This throws the output into the final result bucket
})
})
I hope someone in the universe finds it helpful
Upvotes: 2