Search code examples
graphgremlinamazon-neptune

Neptune Query Returning Duplicate objects


This is how my data in graph db looks. There are three types of labels

  1. Part
  2. SubstitutePart
  3. Tool

And two types of edges

  1. Substitute: Part -> SubstitutePart
  2. Uses: SubstitutePart -> Tool Or Part -> Tool

enter image description here

I am trying to fetch all the vertices with label as 'Tool' whose SubstitutePart Vertex Id is "someId" and I am also traversing to the corresponding Part vertex to get the all the Tool vertices from there as well. So, In-short all the Tool vertices that are directly or indirectly related to a specific SubstitutePart vertex.

My gremlin query that I POST to Neptune db using REST API looks like this:

{
    "gremlin": "g.V('someId').hasLabel('SubstitutePart').as('subPart').outE('uses').inV().as('subTools').in('uses').inE('substitute').outV().outE('uses').inV().as('partTools')"
}

And the response I get from this query is this.

{
    "requestId": "1d256612-1716-8457-b8ac-2442666d174c",
    "status": {
        "message": "",
        "code": 200,
        "attributes": {
            "@type": "g:Map",
            "@value": []
        }
    },
    "result": {
        "data": {
            "@type": "g:List",
            "@value": [
                {
                    "@type": "g:Vertex",
                    "@value": {
                        "id": "1c256612-1716-4b76-b8ac-2472666d17ec",
                        "label": "Tool",
                        "properties": {
                            "number": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": 678255301
                                        },
                                        "value": "2",
                                        "label": "number"
                                    }
                                }
                            ],
                            "versionId": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": -1536929837
                                        },
                                        "value": "1c256612-1716-4b76-b8ac-2472666d17ec",
                                        "label": "versionId"
                                    }
                                }
                            ],
                            "creationDate": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": -1328847462
                                        },
                                        "value": {
                                            "@type": "g:Int64",
                                            "@value": 0
                                        },
                                        "label": "creationDate"
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "@type": "g:Vertex",
                    "@value": {
                        "id": "1c256612-1716-4b76-b8ac-2472666d17ec",
                        "label": "Tool",
                        "properties": {
                            "number": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": 678255301
                                        },
                                        "value": "2",
                                        "label": "number"
                                    }
                                }
                            ],
                            "versionId": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": -1536929837
                                        },
                                        "value": "1c256612-1716-4b76-b8ac-2472666d17ec",
                                        "label": "versionId"
                                    }
                                }
                            ],
                            "creationDate": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": -1328847462
                                        },
                                        "value": {
                                            "@type": "g:Int64",
                                            "@value": 0
                                        },
                                        "label": "creationDate"
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "@type": "g:Vertex",
                    "@value": {
                        "id": "f38e06dc-b42f-4236-a260-082c53b4d09c",
                        "label": "Tool",
                        "properties": {
                            "number": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": -24263995
                                        },
                                        "value": "1",
                                        "label": "number"
                                    }
                                }
                            ],
                            "versionId": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": 1752257459
                                        },
                                        "value": "f38e06dc-b42f-4236-a260-082c53b4d09c",
                                        "label": "versionId"
                                    }
                                }
                            ],
                            "creationDate": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": -1498607962
                                        },
                                        "value": {
                                            "@type": "g:Int64",
                                            "@value": 638125689707921052
                                        },
                                        "label": "creationDate"
                                    }
                                }
                            ]
                        }
                    }
                },
                {
                    "@type": "g:Vertex",
                    "@value": {
                        "id": "f38e06dc-b42f-4236-a260-082c53b4d09c",
                        "label": "Tool",
                        "properties": {
                            "number": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": -24263995
                                        },
                                        "value": "1",
                                        "label": "number"
                                    }
                                }
                            ],
                            "versionId": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": 1752257459
                                        },
                                        "value": "f38e06dc-b42f-4236-a260-082c53b4d09c",
                                        "label": "versionId"
                                    }
                                }
                            ],
                            "creationDate": [
                                {
                                    "@type": "g:VertexProperty",
                                    "@value": {
                                        "id": {
                                            "@type": "g:Int32",
                                            "@value": -1498607962
                                        },
                                        "value": {
                                            "@type": "g:Int64",
                                            "@value": 638125689707921052
                                        },
                                        "label": "creationDate"
                                    }
                                }
                            ]
                        }
                    }
                }
            ]
        },
        "meta": {
            "@type": "g:Map",
            "@value": []
        }
    }
}

The result has 4 vertices and 2 of them are duplicates. It is containing the Tool vertices which are directly linked with Part label. Vertices that are linked with SubstitutePart labels are not returned in the response.

What should be fixed in the query in order to get all Tool vertices that are directly/indirectly linked with SubstitutePart label vertex?


Solution

  • It appears like you're walking the graph and then overlapping the path from which you came. What you really want is a Gremlin query that will spawn traversers [2] and walk both parts of the graph "simultaneously" (or, at least appear as if it is happening in parallel).

    Instead, you may want to do:

    g.V('someId').
    //hasLabel('SubstitutePart').  [1]
    union(
    out('uses'),
    in('substitute').out('uses')
    )
    

    The union will spawn two traversers to "walk" the graph in the opposite directions.

    It also appears that you're using as() labels within your query. These are only needed if you then want to use something like a select() later in the query to fetch whatever items you "labeled" using that as() label.

    [1] There's no need to check for a Label if you're looking up a vertex by an ID. IDs in TinkerPop are unique per every vertex or edge (same goes for Neptune).

    [2] Think of a traverser as a little Gremlin that spawns every time there is a fork in the graph that needs to be traversed. In actuality, this is the query engine keeping track of where in the graph we've been and where we are headed.