KOB
KOB

Reputation: 4545

Gremlin query to find the entire sub-graph that a specific node is connected in any way to

I am brand new to Gremlin and am using gremlin-python to traverse my graph. The graph is made up of many clusters or sub-graphs which are intra-connected, and not inter-connected with any other cluster in the graph.

A simple example of this is a graph with 5 nodes and 3 edges:

I want a query that will return a sub-graph object of all nodes and edges connected (in or out) to the queried node. I can then store this sub-graph as a variable and then run different traversals on it to calculate different things.

This query would need to be recursive as these clusters could be made up of nodes which are many (inward or outward) hops away from each other. There are also many different types of nodes and edges, and they all must be returned.

For example:

I have used both Neo4J with Cypher and Dgraph with GraphQL and found this task quite easy in these two langauges, but am struggling a bit more with understanding gremlin.

EDIT:

From, this question, the selected answer should achieve what I want, but without specifying the edge type by changing .both('created') to just .both().

However, the loop syntax: .loop{true}{true} is invalid in Python of course. Is this loop function available in gremlin-python? I cannot find anything.

EDIT 2:

I have tried this and it seems to be working as expected, I think.

g.V(node_id).repeat(bothE().otherV().simplePath()).emit()

Is this a valid solution to what I am looking for? Is it also possible to include the queried node in this result?

Upvotes: 3

Views: 2553

Answers (1)

noam621
noam621

Reputation: 2856

Regarding the second edit, this looks like a valid solution that returns all the vertices connected to the starting vertex. Some small fixes:

  • you can change the bothE().otherV() to both()
  • if you want to get also the starting vertex you need to move the emit step before the repeat
  • I would add a dedup step to remove all duplicate vertices (can be more than 1 path to a vertex)
g.V(node_id).emit().repeat(both().simplePath()).dedup()

exmaple: https://gremlify.com/jngpuy3dwg9

Upvotes: 2

Related Questions