Gremlin: get in & out vertex from edgeId including edge properties

Viewed 232

Given edgeId I want to get in & out vertex with edge properties.

I tried the following:

final String edgeId = "edge-nk1-to-nk2";
g.addV("nk_node").property(T.id, "nk1").property("name", "name-nk1").as("n1")
    .addV("nk_node").property(T.id, "nk2").property("name", "name-nk2").as("n2")
    .addE("friend").property(T.id, edgeId).property("name", "edge-name-prop-value").from("n1").to("n2")
    .next();

System.out.println("---------------------------------------------------------------------------------");
final Object usingBothVOutput = g.E(edgeId).as("edge")
    .bothV().as("nodes")
    .select("edge", "nodes").by(valueMap(true))
    .toList();

System.out.println(usingBothVOutput);
System.out.println("--------------------------------------------------------------------------------");
final Path path = g.E(edgeId).as("edge").bothV().path().next();
System.out.println(path);

With output:

---------------------------------------------------------------------------------
[{edge={id=edge-nk1-to-nk2, label=friend, name=edge-name-prop-value}, nodes={id=nk1, label=nk_node, name=[name-nk1]}}, {edge={id=edge-nk1-to-nk2, label=friend, name=edge-name-prop-value}, nodes={id=nk2, label=nk_node, name=[name-nk2]}}]
--------------------------------------------------------------------------------
path[e[edge-nk1-to-nk2][nk1-friend->nk2], v[nk1]]

The first query output has both nodes but has no indication which one is IN and which one is OUT. The path query has the IN and OUT vertex explicitly stated, however, it doesn't have the property of the edge.

1 Answers

If you want to use the path step you can add by:

g.E(edgeId).as("edge").bothV().path().by(valueMap(true))

Also, you can try to use project:

g.E(edgeId).project("out", "in").by(outV().valueMap(true)).by(inV().valueMap(true))
Related