I'd like to add a new edge between two vertices.
Here is the command I'm using
def graph=ConfiguredGraphFactory.open('graph');
def g = graph.traversal();
graph.addVertex(label, 'Person', 'name', 'Jack', 'entityId', '100');
graph.addVertex(label, 'Person', 'name', 'John', 'entityId', '50');
def v1 = g.V().has('entityId', '100').next();
def v2 = g.V().has('entityId', '50').next();
v1.addEdge('managerOf',v2,'inEntityId', '100', 'outEntityId', '50')
Then whenever I want to get the path I just created:
g.V().outE().inV().path();
It returns:
[
{
"labels": [
[],
[],
[]
],
"objects": [
{
"id": 40968272,
"label": "Person",
"type": "vertex",
"properties": {
"name": [
{
"id": "oe22y-oe3bk-6mmd",
"value": "Jack"
}
],
"entityId": [
{
"id": "oe2h6-oe3bk-6ozp",
"value": "100"
}
]
}
},
{
"id": "1crua2-oe3bk-4is5-oectk",
"label": "managerOf",
"type": "edge",
"inVLabel": "Person",
"outVLabel": "Person",
"inV": 40980584,
"outV": 40968272,
"properties": {
"outEntityId": "50",
"inEntityId": "100"
}
},
{
"id": 40980584,
"label": "Person",
"type": "vertex",
"properties": {
"name": [
{
"id": "1cs5ql-oectk-6mmd",
"value": "John"
}
],
"entityId": [
{
"id": "1cs64t-oectk-6ozp",
"value": "50"
}
]
}
}
]
}
]
Notice that the edge doesn't reflect what I just created. It's inverted! How is it possible? Am I doing anything wrong?
I'm using JanusGraph, in the data browser it also displays the edge with the wrong direction.
I think it's right. You reversed your properties though on your edge. the "outEntityId" should be "100" (Jack) and the "inEntityId" should be "50" (John) just as the edge is inV
and outV
reflects. To say it another way, you created
jack-managerOf->john
for that "managerOf" edge, the edge goes out of "jack", thus the outV
is "jack", and goes into "john", thus the inV
is "john".
Note that you can simplify your vertex/edge addition by going the recommended route of using the Traversal API (aka Gremlin):
gremlin> g.addV('Person').property('name', 'Jack').property('entityId', '100').as('jack').
......1> addV('Person').property('name', 'John').property('entityId', '50').as('john').
......2> addE('managerOf').from('jack').to('john').property('inEntityId', '50').property('outEntityId', '100').iterate()
gremlin> g.V().outE().inV().path().by('name').by(valueMap())
==>[Jack,[inEntityId:50,outEntityId:100],John]