简体   繁体   中英

Gremlin query to get all the directly as well as indirectly related vertices

I have a graph database in which there is a root vertex with some "id= Xyz".This vertex is related to 3 more vertices with an edge having relationship of a "child". Now these 3 vertices themselves have 2 connected vertices each with the same relationship as "child".

I want to get the info of all the directly or indirectly connected vertices keeping the nested structure maintained. The JSON output should be in the nested form for indirect vertices.

Can we do this?

And what to do if the depth of tree increase to n Please help

Not sure how you want your data to look like, but you can do this in several ways:

Using path for a full tree:

g.V().hasLabel('root').emit().repeat(out()).path()

If you want only the two levels:

g.V().hasLabel('root').emit().repeat(out()).times(2).path()

You can also use project step if you want specific data structure:

g.V().hasLabel('root').project('v', 'c').
    by(id).
    by(out().project('v', 'c').by(id).
        by(out().id().fold()).fold())

example: https://gremlify.com/at

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM