0

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

Kushal Vijay
  • 86
  • 1
  • 11

1 Answers1

3

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

noam621
  • 2,766
  • 1
  • 16
  • 26
  • Thank you for the answer @noam621. It is working for me to get the full tree when the tree is small. For larger tree (Greater height) the server fails to fetch the query. Can you suggest any optimized query for trees with greater height? Up to 10 levels – Kushal Vijay Jun 06 '20 at 06:40
  • How many vertices you trying to fetch? Are you sure you not entering an infinite loop? Try to limit the try height like in the second example or using `simplePath` inside the repeat – noam621 Jun 06 '20 at 14:28
  • I came to know that the repeat can have at max 32 loops. My tree can be more than that. Is there any other option or approach I can follow? Aim: To fetch the complete tree in a hierarchical structure What we have Is root node. – Kushal Vijay Jun 07 '20 at 16:15
  • @KushalVijay 32 loops with 2-3 child verities each time is a huge amount of data (with 2 children per node you will get 2^32-1 nodes). if you really don't have a loop I think you will have to split it to several queries – noam621 Jun 08 '20 at 08:20
  • Yes, I got you on that.I'll pull the tree till 32 loops then run another query for the leaf nodes of the structure fetched. Thanks @noam621 – Kushal Vijay Jun 08 '20 at 16:23
  • Thanks for sharing this, It actually helped me to resolve the error. – Kushal Vijay Oct 06 '20 at 17:16