Gremlin Cayley Query: How to Perform a Multi-Level Query

New to cayley and can't figure out how to render a multi-level query with an unknown number of levels.

For example, let's say all predicates / relationships between nodes are "like". If I have a diagram formed this way.

A --> B

B --> C

C --> D

D --> E

...

I would like to follow the train and display the entire symbol diagram. Which 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 parameters

I have also tried

return g.V(x).As("source").Out("likes").As("target).All()

      

Instead of the second line of code. Just added .All () at the end to complete the request. This returns the query results, but the renderer doesn't show anything. Any guidance on how to show this graph would be greatly appreciated!

+3


source to share


1 answer


I'm not sure if you still need this, but I'll write it anyway, since I had to learn the same from the almost nonexistent example Cayley documentation.

To cross the timeline, Cayley has . FollowRecursive () method defined in the API . The .FollowRecursive method uses something called morphism to figure out how to follow a recursive path. In my opinion, it looks like an abstract path object that encodes a recursive case (sort of like the getLikes function (but not really)). Here is an example of a Gizmo request that should work for a 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 all.

      



To visualize the whole traversal or make some additional requests for 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 useful

+2


source







All Articles