Search code examples
pythontinkerpop3gremlin-server

TinkerPop: Multi-Level search to multiple - vertex types


Sample Graph: Tinker Modern

Query: Find all direct friends (person Vertex) of Marko and (union) all software in second hop.


Failed attempts:

Generic Query for first level people:

g.V(1).hasLabel("person").repeat(both()).times(1).emit(hasLabel("person")).hasLabel("person").values("name")

Generic Query for second level/hop software:

g.V(1).hasLabel("person").repeat(both()).times(2).emit(hasLabel("software")).hasLabel("software").values("name")

Attempt to merge above two queries:

g.V(1).hasLabel("person").repeat(both()).times(1).emit(hasLabel("person")).hasLabel("person").repeat(both()).times(2).emit(hasLabel("software")).hasLabel("software").values("name")

I didn't really understand how union works, because its not merging data.

g.V(1).union().V(2)
g.V(1).union(V(2))

Best I got till now is, but I want some ability to do like (marko connected to person AND/OR marko connected to software):

gremlin> g.V(1).store('x').V(2).store('y').cap('x', 'y')
==>[x:[v[1]],y:[v[2]]]

Solution

  • This for first level:

    gremlin> g.V(1).hasLabel("person").as("from", "to1", "to2")
                .repeat(both()).times(1).emit(hasLabel("person")).hasLabel("person").as("to1")
                .select("from")
                .repeat(both()).times(1).emit(hasLabel("software")).hasLabel("software").as("to2")
                .project("from", "person", "software")
                .by(select("from").by("name"))
                .by(select("to1").by("name"))
                .by(select("to2").by("name"))
    

    Result:

    ==>[from:marko,person:vadas,software:lop]
    ==>[from:marko,person:josh,software:lop]
    

    For multilevel increse the value of times