user2524707 user2524707 - 1 year ago 271
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)
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 Source

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))
gremlin> g.V(1,2).bothE().where(inV().has(id, within(2,3))).hasLabel('created')
gremlin> vStarts = g.V(1,2).toList().toArray()
gremlin> vEnds = g.V(2,3).toList().toArray()
gremlin> g.V(vStarts).bothE().where(inV().is(within(vEnds))).hasLabel('created')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download