Search code examples
gremlintinkerpopjanusgraph

Gremlin - connect vertices a and b where a.x is within b.y values


Sample graph def:

g.addV("foo").property("x", "a")
g.addV("bar").property(set, "y", "a").property(set, "y", "b").property(set, "y", "c")

I need to add an edge b/w foo and bar vertices where bar.y CONTAINS foo.x

I'm trying this query, but it throws an error

gremlin> g.V().hasLabel("bar").as("bar").V().hasLabel("foo").as("foo").where("foo", eq("bar")).by("x").by("y").addE("contains").from("bar").to("foo").Iterate()
Multiple properties exist for the provided key, use Vertex.properties(y)

I've tried many variations but cannot get it to work. Please help!


Solution

  • Here is one approach you can try:

    gremlin> g.V().hasLabel("bar").as("bar").
    ......1>   V().hasLabel("foo").as("foo").
    ......2>   where(within("bar")).by('x').by(values('y').fold())    
    
    ==>v[0]