Search code examples
gremlinjanusgraphtinkerpop

Gremlin query to list out all the edges by the same label


I tried different queries matching my request but unable to find it. Sorry of this is a duplicate. I have two vertices Vertex1 and Vertex2. There is an edge connecting from Vertex1 to Vertex2 with label relation properties {name: "Test1"}. Similarly there is one more edge connecting from Vertex1 to Vertex2 with the same label as relation but properties as {name: "Test2"}. The attached picture shows the vertices and its relation. enter image description here

How to list all the relations?

Query1: g.V('Vertex1').outE().hasLabel('relation') => List only the first relation i.e. the relation with properties {name: "Test1"}.

Query2: g.V('Vertex1').outE().as("c").select("c").by(valueMap()).toList() => List out all the relations including the edge with label "relation".

If I tweak little bit to include the label name to query as g.V('Vertex1').outE('relation').as("c").select("c").by(valueMap()).toList() => then again getting only the first edge. I am trying to get just the properties of the edge with label "relation" and its properties,like

{id=Edge1-ID1, label=relation, name=Test1}

{id=Edge1-ID2, label=relation, name=Test2}

Solution

  • I'm not sure what's wrong but your first attempt is essentially the right one given Gremlin syntax:

    gremlin> g.addV().property(id,'Vertex1').as('v1').
    ......1>   addV().property(id,'Vertex2').as('v2').
    ......2>   addE('relation').from('v1').to('v2').property('name','Test1').
    ......3>   addE('relation').from('v1').to('v2').property('name','Test2').iterate()
    gremlin> g.V('Vertex1').outE('relation')
    ==>e[0][Vertex1-relation->Vertex2]
    ==>e[1][Vertex1-relation->Vertex2]
    gremlin> g.V('Vertex1').outE('relation').values('name')
    ==>Test1
    ==>Test2
    

    My only thought here is that perhaps you aren't fully iterating your traversal? You did a toList() in your other examples, but not the first.