如何在Gremlin中找到两个顶点之间的边缘ID

3
假设我们有一个拥有许多顶点的图形g,并且我们需要找到连接具有ID id1 和 id2 的顶点v1和v2之间的一条边及其边缘ID。
2个回答

7
这只是一个简单的遍历操作:
g.V(id1).bothE().where(otherV().hasId(id2))

由于您没有说明边缘是从v1v2还是反过来,因此我使用了bothE()。当您已知方向时,应使用outE()inE()。此外,当您知道边缘标签时,您应将其提供给此步骤以减少要遍历的边缘数量。

对于TinkerPop的现代图,它看起来像这样:

gremlin> graph = TinkerFactory.createModern()
==>tinkergraph[vertices:6 edges:6]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> g.V().valueMap(true)
==>[name:[marko],label:person,id:1,age:[29]]
==>[name:[vadas],label:person,id:2,age:[27]]
==>[name:[lop],label:software,id:3,lang:[java]]
==>[name:[josh],label:person,id:4,age:[32]]
==>[name:[ripple],label:software,id:5,lang:[java]]
==>[name:[peter],label:person,id:6,age:[35]]
gremlin> g.V(1).bothE().where(otherV().hasId(2)).id()
==>7

并且通过明确的方向和边标签进行优化:
gremlin> g.V(1).outE('knows').where(inV().hasId(2)).id()
==>7

2
如果您正在使用 gremlin-python 与 orientDB,则以下代码段将对您有所帮助。
g.V(from_v_id).outE(label).where(__.inV().where(__.hasId("#" + to_v_id))).next()

感谢您的添加。Gremlin-Python 的文档非常贫乏。如果其他人在使用时能够添加更多关于 Gremlin-Python 的文档,那将会非常有用。对于其他人来说,这也可能很有用:https://groups.google.com/forum/#!forum/gremlin-users - Joshua Wolff
以下是一些针对Gremlin-Python的抽象层,供其他有兴趣的人参考:https://gist.github.com/joshwolff1/b61748f9342a4a3ef597d3d3b72ef027 - Joshua Wolff

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接