xeroshogun
xeroshogun

Reputation: 1092

Using a back step to get two connected vertexes

If I have 3 vertex's A, B, C, where B has an edge to A and C. Starting with B how can I get values for A and C

g.V("b").out("toC").as("c").out("toA").as("a").select("c", "a").next()

This is what I have but it causes an error because I don't think you can go out to A from C since they aren't connected. I need a way to go back to B first but there is no back step that I have seen.

Upvotes: 0

Views: 56

Answers (2)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14371

Using this graph

gremlin>  g.addV('A').as('a').
......1>    addV('B').as('b').
......2>    addV('C').as('c').
......3>    addE('toA').from('b').to('a').
......4>    addE('toC').from('b').to('c')
==>e[42783][42780-toC->42781]  

You can find the vertices connected to B using

gremlin> g.V().hasLabel('B').out().elementMap()

==>[id:42774,label:A]
==>[id:42776,label:C]  

You can also filter using specific edge labels in cases where there are lots of edges from B and you only want specific ones:

gremlin> g.V().hasLabel('B').out('toA','toC').elementMap()

==>[id:42774,label:A]
==>[id:42776,label:C]   

If you really do need to write the query so that it works the way you showed in the question, then this is one way:

gremlin> g.V().hasLabel('B').as('b').
......1>       out('toA').as('a').
......2>       select('b').
......3>       out('toC').as('c').
......4>       select('a','c').
......5>         by(elementMap())  

==>[a:[id:42779,label:A],c:[id:42781,label:C]] 

Upvotes: 1

PrashantUpadhyay
PrashantUpadhyay

Reputation: 877

You can also try:

    gremlin> g.V().hasLabel('B').
    ......1> outE().hasLabel('toA','toC').
    ......2> inV().elementMap()

    ==>[id:0,label:A]
    ==>[id:2,label:C]

Upvotes: 0

Related Questions