user2524707 user2524707 - 1 month ago 19
Java Question

Gremlin get Edge with specific start and end node

I am using Gremlin to handle Titan Graph.
And I am trying to find a way to get a very specific relationship.

I have the label, the properties and a list of possible start and endNodes.

And I want all relationships matching this.

I have this already to get all relationships matching label and property:

GraphTraversal<Edge, Edge> tempOutput = g.E().hasLabel(relationshipStorage.getId());

if(relationshipStorage.getProperties() != null)
{
for (Map.Entry<String, Object> entry : relationshipStorage.getProperties().entrySet())
{
if (tempOutput == null)
{
break;
}
tempOutput = tempOutput.has(entry.getKey(), entry.getValue());
}
}


But I didn't find a way to get it with a specific start and endNode.
I don't want to get multiple edges between two nodes.
I only want one edge with the specific vertices.

Answer

See the Between Vertices recipe and just extend from there. For example, let's say you wanted to find edges between two vertices with ids 1 and 2. Let's further assume you only wanted "knows" edges with a "weight" property greater than 0.0.

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V(1).bothE().where(otherV().hasId(2)).hasLabel('knows').has('weight',gt(0.0))
==>e[7][1-knows->2]
gremlin> g.V(1,2).bothE().where(inV().has(id, within(2,3))).hasLabel('created')
==>e[9][1-created->3]
gremlin> vStarts = g.V(1,2).toList().toArray()
==>v[1]
==>v[2]
gremlin> vEnds = g.V(2,3).toList().toArray()
==>v[2]
==>v[3]
gremlin> g.V(vStarts).bothE().where(inV().is(within(vEnds))).hasLabel('created')
==>e[9][1-created->3]