[英]Gremlin-Python find all nodes connected by edges
I have a list of node labels as node_labels
, where some of these may exist in a graph.我有一个节点标签列表作为
node_labels
,其中一些可能存在于图中。
node_labels = ['a', 'b', 'c']
Using this I can get the node labels or associated attributes.使用它我可以获得节点标签或相关属性。
g.V().has_label(*node_labels).to_list()
How can I modify the query so that I get only those node(s) where an edge exists (either incoming or outgoing) between the nodes?如何修改查询,以便仅获取节点之间存在边缘(传入或传出)的那些节点?
If I understand properly, said another way, you want any vertex with those labels if that vertex connects to another vertex with one those labels:如果我理解正确,换一种说法,如果该顶点连接到具有这些标签的另一个顶点,则您需要具有这些标签的任何顶点:
g.V().has_label(*node_labels).
filter(both().has_label(*node_labels))
Here's a working example:这是一个工作示例:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().hasLabel('person')
==>v[1]
==>v[2]
==>v[4]
==>v[6]
gremlin> g.V().hasLabel('person').where(both().hasLabel('person'))
==>v[1]
==>v[2]
==>v[4]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.