kunal
kunal

Reputation: 429

outE() not showing edge, but inE() shows the edge in Cosmos DB for Gremlin

I have two vertices in two different logical partitions, (both have different partition key values). I have created an Edge between them. Now when I run the outE() on the source vertex, it returns null, while when I run the inE() on destination Vertex, it returns me the edge with outV as the source vertex.

These are the gremlin queries for the same,

g.addV(‘vertex_label_1’).property(‘id’,’V1’).property(‘pk’,’pkX’)
g.addV(‘vertex_label_2’).property(‘id’,’V2’).property(‘pk’,’pkY’)

Add Edge:

g.V(‘V1’).has(‘pk’,’pkX’).addE(‘edge_label_12’).property(‘id’,’E12’).to(g.V(‘V2’).has(‘pk’,’pkY’))

Now when I run g.V('V1').outE() -> it returns null while when I run g.V('V2').inE() -> it returns the above edge

I am not sure what am I missing here ?

Ref: https://learn.microsoft.com/en-us/answers/questions/1286272/how-to-add-an-edge-between-2-vertices-in-different

Upvotes: 0

Views: 89

Answers (1)

Balaji
Balaji

Reputation: 1768

I run the outE() on the source vertex, it returns null, while when I run the inE() on destination Vertex, it returns me the edge with outV as the source vertex.

As @NotFound suggested to setup bidirectional edges. So, that we can query either side with OutE(). I used that approach to get same Edge while querying with either of the query.

Below are the steps I followed which returns same Edge 'E12':

Add Vertex V1:

  • g.addV('vertex_label_1').property('id', 'V1').property('pk', 'pkX') Creates a vertex with label vertex_label_1, id V1, and partition key pkX.
[
  {
    "id": "V1",
    "label": "vertex_label_1",
    "type": "vertex",
    "properties": {
      "pk": [
        {
          "id": "V1|pk",
          "value": "pkX"
        }
      ]
    }
  }
]

Add Vertex V2:

  • g.addV('vertex_label_2').property('id', 'V2').property('pk', 'pkY') Creates a vertex with label vertex_label_2, id V2, and partition key pkY.
[
  {
    "id": "V2",
    "label": "vertex_label_2",
    "type": "vertex",
    "properties": {
      "pk": [
        {
          "id": "V2|pk",
          "value": "pkY"
        }
      ]
    }
  }
]

Creating a bidirectional relationship between V1 and V2:

  • g.V('V1').has('pk', 'pkX').as('v1') The property pk with the value pkX on vertex V1 is selected, and an alias of v1 is given to it.

  • .V('V2').has('pk', 'pkY').as('v2') The property pk with the value pkY on vertex V2 is selected, and an alias of v2 is given to it.

  • .addE('edge_label_12').property('id', 'E12').from('v1').to('v2') It generates a new edge with the label edge_label_12 and ID E12. It links to the vertex known as v2 after starting from the vertex known as v1.

  • This establishes the first direction of the edge, going from V1 to V2.

  • .addE('edge_label_12').property('id', 'E12').from('v2').to('v1') The same ID E12 is used to construct a second new edge with the label edge_label_12. It links to the vertex v1 after starting from the v2.

  • This establishes the edge's second direction, from V2 to V1.

Query:

g.V('V1').has('pk', 'pkX').as('v1')
  .V('V2').has('pk', 'pkY').as('v2')
  .addE('edge_label_12').property('id', 'E12').from('v1').to('v2')
  .addE('edge_label_12').property('id', 'E12').from('v2').to('v1')

[
  {
    "id": "E12",
    "label": "edge_label_12",
    "type": "edge",
    "inVLabel": "vertex_label_1",
    "outVLabel": "vertex_label_2",
    "inV": "V1",
    "outV": "V2"
  }
]
  • g.V('V1').outE() The V1 vertex outgoing edges are returned by the query. The edge E12 that joins V1 and V2 is accurately returned by the output shown below:
[
  {
    "id": "E12",
    "label": "edge_label_12",
    "type": "edge",
    "inVLabel": "vertex_label_2",
    "outVLabel": "vertex_label_1",
    "inV": "V2",
    "outV": "V1"
  }
]
  • g.V('V2').inE() Returns the incoming edges to vertex V2. The report accurately identifies the edge E12, which joins V1 and V2.
[
  {
    "id": "E12",
    "label": "edge_label_12",
    "type": "edge",
    "inVLabel": "vertex_label_2",
    "outVLabel": "vertex_label_1",
    "inV": "V2",
    "outV": "V1"
  }
]
  • g.V('V2').outE() retrieves all outgoing edges from vertex V2.
[
  {
    "id": "E12",
    "label": "edge_label_12",
    "type": "edge",
    "inVLabel": "vertex_label_1",
    "outVLabel": "vertex_label_2",
    "inV": "V1",
    "outV": "V2"
  }
]
  • g.V('V1').outE() , g.V('V2').inE() and g.V('V2').outE() Returning the same edge E12, suggesting that V1 and V2 are stored in the same partition, regardless of their different partition key values.

Upvotes: 0

Related Questions