1

I am trying to add edges between vertices similar to this question, except there are different properties whose equality I want to consider, e.g. using a TinkerGraph with an index on 'x':

g.addV().property("x", "1").
  addV().property("x", "2").property("y", "1").
  addV().property("x", "3").property("y", "2")

I am trying to add the two edges where x=y so that (x=3,y=2) --link--> (x=2,y=1) --link--> (x=1) using something like:

g.V().as("a").
  V().as("b").
  where(has("x", select("a").values("y"))).
  addE("link").from("a").to("b")

However this particular query creates more edges then I was expecting (all vertices with 'y' are connected to all other vertices).

Any help forming the appropriate where-clause would be greatly appreciated.

Jeremy Gustie
  • 451
  • 4
  • 6

1 Answers1

4

When debugging Gremlin you sometimes need to remove some step so that you look back at what is being returned from earlier parts of the pipeline. Note what the first part of your traversal is returning:

gremlin> g.V().as("a").
......1>   V().as("b")
==>v[0]
==>v[2]
==>v[5]
==>v[0]
==>v[2]
==>v[5]
==>v[0]
==>v[2]
==>v[5]

You can see why you end up with a lot more edges that you'd like. I started filtering some of those away with this:

gremlin> g.V().has('x').as('a').
......1>   V().has('y').as('b').
......2>   where('a',neq('b'))
==>v[2]
==>v[5]
==>v[5]
==>v[2]

You only care about vertices that have an "x" property for "a" and a "y" property for "b" and since you don't want those vertices matching on themselves you also want to cancel those out with where('a',neq('b')).

From there, the remaining where() clause is quite similar to the one in the question your referenced:

gremlin> g.V().has('x').as('a').
......1>   V().has('y').as('b').
......2>   where('a', neq('b')).
......3>   where('a', eq('b')).by('x').by('y').
......4>   addE('link').from('a').to('b')
==>e[8][0-link->2]
==>e[9][2-link->5]

thus:

gremlin> g.V().has('x','1').outE('link').inV().outE('link').inV().path().by('x').by(label)
==>[1,link,2,link,3]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135