[英]What is the Gremlin query that can get me all the vertices either directly or indirectly connected to one specific vertex
I need help with a Gremlin query that can output all the vertices related to one specific vertex A and their cascading related vertices (which means all the vertices related directly or indirectly to A). 我需要一个Gremlin查询的帮助,该查询可以输出与一个特定顶点A及其级联相关顶点相关的所有顶点(这意味着所有顶点直接或间接与A相关)。
For example, in a graph 例如,在图表中
A -> B -> C
D
Running this query on A will give me B and C. 在A上运行此查询将给我B和C.
The solution I have right now is an ugly one: 我现在的解决方案是一个丑陋的解决方案:
gV('A').both(); gV('A').both().both();
etc 等等
Any help would be really appreciated. 任何帮助将非常感激。
Your solution isn't ugly; 你的解决方案并不难看; it only lacks a bit of iteration and an exit condition.
它只缺少一点迭代和退出条件。
Do you require a maximum depth? 你需要最大深度吗? Depending on the shape of your graph, the query you want to execute could be returning all vertices of that graph.
根据图形的形状,您要执行的查询可能会返回该图形的所有顶点。
Assuming a toy modern TinkerGraph created in the Gremlin console: 假设在Gremlin控制台中创建了一个玩具现代TinkerGraph:
gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
This query could be helpful: 此查询可能会有所帮助:
gremlin> g.V(1).repeat(both().simplePath()).emit().times(3).dedup()
==>v[3]
==>v[2]
==>v[4]
==>v[6]
==>v[5]
"Starting from vertex with id=1, traverse the graph in all directions up to a maximum depth of 3 while discarding previously visited paths. The emit()
step ensures that all traversed vertices found along the way, and not just leaves, are returned." “从id = 1的顶点开始,在所有方向上遍历图形,最大深度为3,同时丢弃先前访问过的路径
emit()
步骤确保返回沿途找到的所有遍历顶点,而不仅仅是叶子返回“。
Chances are high that you want to figure out which vertices are linked to that vertex only via specific edges. 您想要确定哪些顶点仅通过特定边链接到该顶点的机会很高。 In such case, you could be passing label(s) to the
both()
step, and/or maybe chain a few filters. 在这种情况下,您可以将标签传递给
both()
步骤,和/或链接一些过滤器。
When developing your query, feel free to chain the path()
step to better understand the output. 在开发查询时,请随意链接
path()
步骤以更好地理解输出。
gremlin> g.V(1).repeat(both().simplePath()).emit().times(3).dedup().path()
==>[v[1],v[3]]
==>[v[1],v[2]]
==>[v[1],v[4]]
==>[v[1],v[3],v[6]]
==>[v[1],v[4],v[5]]
There are other ways to solve this, but this query should get you started and familiarize yourself with basic Gremlin steps and concepts. 还有其他方法可以解决这个问题,但是这个查询应该让您开始并熟悉基本的Gremlin步骤和概念。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.