0

I'm trying to fetch a specific node in my graph by its id with its outgoing nodes and edges in a specified depth. The resulting JSON should make it easy to determine which edge belongs to which node. Preferably it should have the form of a tree:

[rootNode, outgoingEdges[], outgoingNodes[
   outgoingNode1, outgoingEdges[], outgoingNodes[
      outgoingNode11, outgoingEdges[], outgoingNodes[]
   ],
   [outgoingNode2, outgoingEdges[], outgoingNodes[...]],
   [outgoingNode3, outgoingEdges[], outgoingNodes[...]]
]

Where "outgoingNode1" and "outgoingNode2" are the outgoing nodes of "rootNode" and "outgoingNode11" is the outgoing node of "outgoingNode1". In this example, the depth of the traversal is 2, because of the two outward jumps from the "rootNode". This depth should be freely adjustable.

I have tried fetching this data using:

g.V('id').aggregate('node').
  outE().aggregate('edges1').
  inV().aggregate('refs1').
  outE().aggregate('edges2').
  inV().aggregate('refs2').
  cap('node','edges1','refs1','edges2','refs2')

This approach returns all the necessary data, but the format of the JSON answer requires some client side calculations to match the connected nodes because the nodes and edges are in seperate arrays. I'm trying to let the server do as much of the work as possible and avoid any unnecessary client side processing of the JSON answer.

I'm not that advanced in gremlin queries and am wondering if there is a simpler way to achieve this.

SOLUTION:

I ended up using this query:

g.V('id').repeat(outE().inV()).times(2).tree()
  • The simplest way to do this is to just use `path` and still do a little client side data wrangling. If you prefer the structure shown in your question come back from the query itself, some combination of `group` and `project` steps is likely the way to go. Do you by chance have a small sample graph you can add to the question so that people can give you tested answers? The `tree` step might also be an option here as your JSON is sort of a tree like structure. – Kelvin Lawrence Jun 20 '22 at 15:06
  • @KelvinLawrence thanks for the quick reply. The tree step seems like the right way to go. – Saleh Omar Jun 21 '22 at 15:13
  • Ok thanks for letting me know - I'm going to convert my comment to an answer in case others find your question. – Kelvin Lawrence Jun 21 '22 at 15:57

1 Answers1

0

The simplest way to do this is to just use path and still do a little client side data wrangling.

If you prefer that the structure shown in your question come back from the query itself, some combination of group and project steps is likely the way to go.

However, the tree step might also be an option here as your JSON is sort of a tree like structure.

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38